Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Multiple ways of query in Elasticsearch

2025-02-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "multiple ways of query in Elasticsearch". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. DSL query

Query DSL:Domain Specified Language, a domain-specific language

1. Query all the data under an index

Grammar

GET / {index} / _ search {"query": {"match_all": {}

Example

Input: GET / staffs/_search {"query": {"match_all": {} output {"took": 1, the time spent "timed_out": false, whether the timeout "_ shards": {"total": 1, a total of several shared "successful": 1, several successful "skipped": 0, "failed": 0} "hits": {"total": {"value": 4, the total number of results of the query "relation": "eq"}, "max_score": 1.0, "hits": [{"_ index": "staffs", the name of the index "_ type": "_ doc", "_ id": "1a" The primary key "_ score" corresponding to the data is the matching score of document for the relevance of a search. The more relevant, the more matching. The score is also high "_ source": {data details "name": "shu xian sheng", "age": 28, "phone": "15711111111", "posittion": "java kaifa", "hobby": ["lanqiu", "zuqiu" "tubu"]}}, {"_ index": "staffs", "_ type": "_ doc", "_ id": "Nq_96G0Bs8sg-pU7kn0S", "_ score": 1.0, "_ source": {"name": "wang xiao san" "age": 21, "phone": "15722222222", "posittion": "web kaifa", "hobby": ["yumaoqiu", "zuqiu", "taiqiu"]}}, {"_ index": "staffs" "_ type": "_ doc", "_ id": "2", "_ score": 1.0, "_ source": {"name": "lixiansheng", "age": 25, "phone": "15733333333", "posittion": "android" "hobby": ["paobu", "zuqiu", "tubu"]}}, {"_ index": "staffs", "_ type": "_ doc", "_ id": "3", "_ score": 1.0 "_ source": {"name": "maxiaoshuan", "age": 23, "phone": "15744444444", "posittion": "ios", "hobby": ["paobu", "yumaoqiu" "lanqiu"]}]} 2. The name of the query contains xiao that is sorted backwards by age

Grammar

GET {index} / _ search {"query": {"match": {"FIELD": "TEXT" FIELD: field name TEXT: condition}}, "sort": [{"FIELD": {FIELD: field name "order": "desc"}]}}

Example

GET staffs/_search {"query": {"match": {"name": "xiao"}}, "sort": [{"age": {"order": "desc"}}]} {"took": 1, "timed_out": false, "_ shards": {"total": 1, "successful": 1 "skipped": 0, "failed": 0}, "hits": {"total": {"value": 2, "relation": "eq"}, "max_score": null, "hits": [{"_ index": "staffs", "_ type": "_ doc" "_ id": "3", "_ score": null, "_ source": {"name": "ma xiao shuai", "age": 23, "phone": "15744444444", "posittion": "ios", "hobby": ["paobu", "yumaoqiu" "lanqiu"]}, "sort": [23]}, {"_ index": "staffs", "_ type": "_ doc", "_ id": "Nq_96G0Bs8sg-pU7kn0S", "_ score": null "_ source": {"name": "wang xiao san", "age": 21, "phone": "1572222222222", "posittion": "web kaifa", "hobby": ["yumaoqiu", "zuqiu", "taiqiu"]} "sort": [21]}} 3. Paging query data

Grammar

GET {index} / _ search {"query": {"match_all": {}}, "from": 1, start the query from which page, 0: represents the first page, 1: represents the second page "size": 1 number of entries displayed on each page}

Example

GET staffs/_search {"query": {"match_all": {}}, "from": 1, "size": 1} {"took": 1, "timed_out": false, "_ shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0}, "hits": {"total": {"value": 4 "relation": "eq"}, "max_score": 1. 0, "hits": [{"_ index": "staffs", "_ type": "_ doc", "_ id": "Nq_96G0Bs8sg-pU7kn0S", "_ score": 1. 0 "_ source": {"name": "wang xiao san", "age": 21, "phone": "1572222222222", "posittion": "web kaifa", "hobby": ["yumaoqiu", "zuqiu" "taiqiu"]}]}} 4. Search for those whose names contain xian sheng and are older than 25

Grammar

GET {index}} / _ search {"query": {"bool": {"must": [{"match": {"FIELD": "TEXT"}]}}, "post_filter": {"range": {"FIELD": {"gte": 10}

Example

GET staffs/_search {"query": {"bool": {query "must": [{"match": {"name": "xian sheng"} "post_filter": {"range": {"age": {"gt": 25} actually there are two, but one is 25, so there is only one {"took": 1, "timed_out": false, "_ shards": {"total": 1, "successful": 1, "skipped": 0. "failed": 0}, "hits": {"total": {"value": 1, "relation": "eq"}, "max_score": 1.3862944, "hits": [{"_ index": "staffs", "_ type": "_ doc", "_ id": "1a" "_ score": 1.3862944, "_ source": {"name": "shu xian sheng", "age": 28, "phone": "15711111111", "posittion": "java kaifa", "hobby": ["lanqiu", "zuqiu" "tubu"]}]}} 5.full-text search (full text search)

Example

GET staffs/_search {"query": {"posittion": {"java kaifa"}} {"took": 1, "timed_out": false, "_ shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0}, "hits": {"total": {"value": 2 "relation": "eq"}, "max_score": 1.6694658, "hits": [{"_ index": "staffs", "_ type": "_ doc", "_ id": "1a", "_ score": 1.6694658, "_ source": {"name": "shu xian sheng" "age": 28, "phone": "15711111111", "posittion": "java kaifa", "hobby": ["lanqiu", "zuqiu", "tubu"]}}, {"_ index": "staffs" "_ type": "_ doc", "_ id": "Nq_96G0Bs8sg-pU7kn0S", "_ score": 0.60996956, "_ source": {"name": "wang xiao san", "age": 21, "phone": "15722222222", "posittion": "web kaifa" "hobby": ["yumaoqiu", "zuqiu", "taiqiu"]} 6.phrase search (phrase search)

Contrary to full-text search, full-text retrieval will disassemble the input search string and match the meaning in the inverted index. as long as it can match any disassembled word, it can be returned as a result.

Phrase search requires that the search string entered must be exactly the same in the specified field text. Erasure can be counted as a match before it can be returned as a result.

Grammar

GET staffs/_search {"query": {"match_phrase": {"FIELD": "PHRASE"}

Example

GET staffs/_search {"query": {"posittion": {"java kaifa"} {"took": 30, "timed_out": false, "_ shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0} "hits": {"total": {"value": 1, "relation": "eq"}, "max_score": 1.6694657, "hits": [{"_ index": "staffs", "_ type": "_ doc", "_ id": "1a", "_ score": 1.6694657 "_ source": {"name": "shu xian sheng", "age": 28, "phone": "15711111111", "posittion": "java kaifa", "hobby": ["lanqiu", "zuqiu" "tubu"]}]}} 7.highlight search (highlight search results)

Grammar

GET {index} / _ search {"query": {"match": {"FIELD": "TEXT"}, "highlight": {"fields": {"FIELD": {}

Example

GET staffs/_search {"query": {"match": {"posittion": "kaifa"}, "highlight": {"fields": {"posittion": {} {"took": 54, "timed_out": false, "_ shards": {"total": 1, "successful": 1, "skipped": 0 "failed": 0}, "hits": {"total": {"value": 2, "relation": "eq"}, "max_score": 0.60996956, "hits": [{"_ index": "staffs", "_ type": "_ doc", "_ id": "1a" "_ score": 0.60996956, "_ source": {"name": "shu xian sheng", "age": 28, "phone": "15711111111", "posittion": "java kaifa", "hobby": ["lanqiu", "zuqiu" "tubu"]}, "highlight": {"posittion": ["java kaifa"]}}, {"_ index": "staffs", "_ type": "_ doc", "_ id": "Nq_96G0Bs8sg-pU7kn0S" "_ score": 0.60996956, "_ source": {"name": "wang xiao san", "age": 21, "phone": "1572222222222", "posittion": "web kaifa", "hobby": ["yumaoqiu", "zuqiu" "taiqiu"]}, "highlight": {"posittion": ["web kaifa"]} inverted index

In fact, when the data is saved, the query index has already been created, and the inverted index is used.

For example, the posittion field is disassembled first, and then an inverted index is created.

The disassembly is based on the selected word splitter.

The data primary key java1akaifa1a,Nq_96G0Bs8sg-pU7kn0Sweb1a corresponding to the disassembled word Nq_96G0Bs8sg-pU7kn0Sios3android4 queries all indexes GET _ cat/indices?vhealth status index uuid pri rep docs.count docs.deleted store.size pri.store.sizegreen open. Kibana _ task_manager_1 Qyl1MousQLq5FyMOCBO4nw 1 0 20 30.5kb 30.5kbgreen open. APM-agent-configuration qPsz40bsQxW_Zcd_4pkJJg 100 0 0283b 283bgreen open. Kibana _ 1 yYdsQgxWQ0utXN-Ulhm5ew 1 09 0 35.4kb 35.4kbyellow open staffs uoo38LYwRB2PzxupYjJ66Q 1 140 17.9kb 17.9kb "multiple query methods in Elasticsearch" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report