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

How to use multiple search methods to retrieve data in ElasticSearch

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces how to use a variety of search methods to retrieve data in ElasticSearch, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

1. Query string search query (1), full query GET http://{{es-host}}/ecommerce/produce/_search

Results:

{"took": 38, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 3 "max_score": 1. 0, "hits": [{"_ index": "ecommerce", "_ type": "produce", "_ id": "2" "_ score": 1. 0, "_ source": {"name": "jiajieshi yagao", "desc": "youxiao fangzhu" "price": 25, "producer": "jiajieshi producer" "tags": ["fangzhu"]}} {"_ index": "ecommerce", "_ type": "produce", "_ id": "1", "_ score": 1.0 "_ source": {"name": "gaolujie yagao", "desc": "gaoxiao meibai", "price": 30 "producer": "gaolujie producer", "tags": ["meibai" "fangzhu"]}}, {"_ index": "ecommerce" "_ type": "produce", "_ id": "3", "_ score": 1.0, "_ source": {"name": "zhonghua yagao" "desc": "caoben zhiwu", "price": 40, "producer": "zhonghua producer" "tags": ["qingxin"]}

Explanation:

Took identifies the time spent on retrieval, whether the time_out timed out in milliseconds _ shards data is split into 5 shards, so for search requests Will hit all the primary shard (or one of its replica shard can also be) total retrieves 5 fragments successful successfully 5 hits.total retrieved the total number of records hits.max_score similarity score up to 1 hits.hits data set hits.hits._index index name hits.hits._type type hits.hits._score data item content (2), Search according to the product name Arrange GET http://{{es-host}}/ecommerce/produce/_search?q=name:yagao&sort=price:desc by price flashback

Results:

{"took": 71, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 3 "max_score": null, "hits": [{"_ index": "ecommerce", "_ type": "produce", "_ id": "3" "_ score": null, "_ source": {"name": "zhonghua yagao", "desc": "caoben zhiwu", "price": 40 "producer": "zhonghua producer", "tags": ["qingxin"]} "sort": [40]}, {"_ index": "ecommerce" "_ type": "produce", "_ id": "1", "_ score": null, "_ source": {"name": "gaolujie yagao" "desc": "gaoxiao meibai", "price": 30, "producer": "gaolujie producer" "tags": ["meibai", "fangzhu"]} "sort": [30]}, {"_ index": "ecommerce" "_ type": "produce", "_ id": "2", "_ score": null, "_ source": {"name": "jiajieshi yagao" "desc": "youxiao fangzhu", "price": 25, "producer": "jiajieshi producer" "tags": ["fangzhu"]} "sort": [25]}} 2. Query DSL query

DSL:Domain Specified Language. Domain-specific language

Http request body: request body, you can use json format to build query syntax, more convenient, you can build a variety of complex syntax, certainly much more powerful than query string search.

(1) full query GET http://{{es-host}}/ecommerce/produce/_search{ "query": {"match_all": {}

Results:

{"took": 23, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 3 "max_score": 1. 0, "hits": [{"_ index": "ecommerce", "_ type": "produce", "_ id": "2" "_ score": 1. 0, "_ source": {"name": "jiajieshi yagao", "desc": "youxiao fangzhu" "price": 25, "producer": "jiajieshi producer" "tags": ["fangzhu"]}} {"_ index": "ecommerce", "_ type": "produce", "_ id": "1", "_ score": 1.0 "_ source": {"name": "gaolujie yagao", "desc": "gaoxiao meibai", "price": 30 "producer": "gaolujie producer", "tags": ["meibai" "fangzhu"]}}, {"_ index": "ecommerce" "_ type": "produce", "_ id": "3", "_ score": 1.0, "_ source": {"name": "zhonghua yagao" "desc": "caoben zhiwu", "price": 40, "producer": "zhonghua producer" "tags": ["qingxin"]} (2), The full query returns the specified attribute value GET http://{{es-host}}/ecommerce/produce/_search{ "query": {"match_all": {}} "_ source": ["name", "price"]}

Return the result:

{"took": 57, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 3, "max_score": 1 "hits": [{"_ index": "ecommerce", "_ type": "produce", "_ id": "2", "_ score": 1, "_ source": {"price": 25 "name": "jiajieshi yagao"}, {"_ index": "ecommerce", "_ type": "produce", "_ id": "1", "_ score": 1 "_ source": {"price": 30, "name": "gaolujie yagao"}}, {"_ index": "ecommerce", "_ type": "produce", "_ id": "3" "_ score": 1, "_ source": {"price": 40 "name": "zhonghua yagao"}]}} (3), Search by product name and arrange GET http://{{es-host}}/ecommerce/produce/_search{ "query" by price backwards: {"match": {"name": "yagao"}} "sort": {"price": "desc"}}

Results:

{"took": 384, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 3 "max_score": null, "hits": [{"_ index": "ecommerce", "_ type": "produce", "_ id": "3" "_ score": null, "_ source": {"name": "zhonghua yagao", "desc": "caoben zhiwu", "price": 40 "producer": "zhonghua producer", "tags": ["qingxin"]} "sort": [40]}, {"_ index": "ecommerce" "_ type": "produce", "_ id": "1", "_ score": null, "_ source": {"name": "gaolujie yagao" "desc": "gaoxiao meibai", "price": 30, "producer": "gaolujie producer" "tags": ["meibai", "fangzhu"]} "sort": [30]}, {"_ index": "ecommerce" "_ type": "produce", "_ id": "2", "_ score": null, "_ source": {"name": "jiajieshi yagao" "desc": "youxiao fangzhu", "price": 25, "producer": "jiajieshi producer" "tags": ["fangzhu"]} "sort": [25]}} 3. Query filter query

Search for items whose product name contains yagao and whose price is more than 30 yuan

GET http://{{es-host}}/ecommerce/produce/_search{ "query": {"bool": {"must": {"match": {"name": "yagao" }} "filter": {"range": {"price": {"gt": "30"} }}}

Return the result:

{"took": 88, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 1, "max_score": 0.25811607 "hits": [{"_ index": "ecommerce", "_ type": "produce", "_ id": "3", "_ score": 0.25811607, "_ source": {"name": "zhonghua yagao" "desc": "caoben zhiwu", "price": 40, "producer": "zhonghua producer", "tags": ["qingxin"]} 4. Full-text search query

Prepare the data:

PUT http://{{es-host}}/ecommerce/produce/4{ "name": "special yagao", "desc": "special meibai", "price": "50", "producer": "special yagao producer" "tags": ["meibai"]} http://{{es-host}}/ecommerce/produce/_search{ "query": {"match": {"producer": "yagao producer"}

When searching, "yagao producer" will be divided into "yagao" and "producer" as long as any one of them matches the matching data.

Result

{"took": 20, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 4, "max_score": 0.70293105 "hits": [{"_ index": "ecommerce", "_ type": "produce", "_ id": "4", "_ score": 0.70293105, "_ source": {"name": "special yagao" "desc": "special meibai", "price": "50", "producer": "special yagao producer", "tags": ["meibai"]} {"_ index": "ecommerce", "_ type": "produce", "_ id": "1", "_ score": 0.25811607, "_ source": {"name": "gaolujie yagao" "desc": "gaolujie meibai", "price": 30, "producer": "gaolujie producer", "tags": ["meibai", "fangzhu"]}} {"_ index": "ecommerce", "_ type": "produce", "_ id": "3", "_ score": 0.25811607, "_ source": {"name": "zhonghua yagao" "desc": "caoben zhiwu", "price": 40, "producer": "zhonghua producer", "tags": ["qingxin"]}} {"_ index": "ecommerce", "_ type": "produce", "_ id": "2", "_ score": 0.1805489, "_ source": {"name": "jiajieshi yagao" "desc": "youxiao fangzhu", "price": 25, "producer": "jiajieshi producer", "tags": ["fangzhu"]}

The most matching correlation score is high and will be at the top of the list.

5. Phrase search query

No more word segmentation for the words to be searched

GET http://{{es-host}}/ecommerce/produce/_search{ "query": {"match_phrase": {"producer": "yagao producer"}

Results:

{"took": 33, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 1, "max_score": 0.70293105 "hits": [{"_ index": "ecommerce", "_ type": "produce", "_ id": "4", "_ score": 0.70293105, "_ source": {"name": "special yagao" "desc": "special meibai", "price": "50", "producer": "special yagao producer" "tags": ["meibai"]} 6. Highlight search query GET http://{{es-host}}/ecommerce/produce/_search{ "query": {"match": {"name": "yagao"}} "highlight": {"fields": {"name": {}

Results:

{"took": 16, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "failed": 0}, "hits": {"total": 4, "max_score": 0.25811607 "hits": [{"_ index": "ecommerce", "_ type": "produce", "_ id": "1", "_ score": 0.25811607, "_ source": {"name": "gaolujie yagao" "desc": "gaolujie meibai", "price": 30, "producer": "gaolujie producer", "tags": ["meibai", "fangzhu"]} "highlight": {"name": ["gaolujie yagao"]}}, {"_ index": "ecommerce", "_ type": "produce" "_ id": "3", "_ score": 0.25811607, "_ source": {"name": "zhonghua yagao", "desc": "caoben zhiwu", "price": 40, "producer": "zhonghua producer" "tags": ["qingxin"]}, "highlight": {"name": ["zhonghua yagao"]}} {"_ index": "ecommerce", "_ type": "produce", "_ id": "2", "_ score": 0.16358379, "_ source": {"name": "jiajieshi yagao" "desc": "youxiao fangzhu", "price": 25, "producer": "jiajieshi producer", "tags": ["fangzhu"]} "highlight": {"name": ["jiajieshi yagao"]}}, {"_ index": "ecommerce", "_ type": "produce" "_ id": "4", "_ score": 0.16358379, "_ source": {"name": "special yagao", "desc": "special meibai", "price": "50", "producer": "special yagao producer" "tags": ["meibai"]} "highlight": {"name": ["special yagao"]} on how to use multiple search methods to retrieve data in ElasticSearch is shared here. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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