在Elasticsearch中,查詢索引文檔的方法多種多樣,這里列舉了6種常見的查詢方法,其中包括:
-
簡單查詢(String Query)
- 這是最基本的全文搜索,只需在URL后面附加查詢字符串即可。例如,對索引
my_index
中的所有文檔執(zhí)行模糊匹配查詢:
GET my_index/_search { "query": { "match": { "field_name": "your_search_term" } } }
- 這是最基本的全文搜索,只需在URL后面附加查詢字符串即可。例如,對索引
-
Match Query
- 類似于簡單查詢,但提供了更多的控制選項(xiàng),比如精確匹配、模糊匹配等。例如:
GET my_index/_search { "query": { "match": { "title": { "query": "search term", "operator": "and" // or "or" for a more relaxed matching } } } }
-
Term Query
- 用于精確匹配字段的特定值,尤其是在非分析字段上。
GET my_index/_search { "query": { "term": { "status.keyword": "active" // 使用.keyword后綴避免對非分析字段進(jìn)行分詞 } } }
-
Bool Query
- 復(fù)合查詢,允許組合多個(gè)查詢條件,包括must(必須滿足)、should(最好滿足)、must_not(必須不滿足)等子句。
GET my_index/_search { "query": { "bool": { "must": [ { "match": { "title": "search term" } }, { "range": { "date": { "gte": "2022-01-01" } } } ], "must_not": [ { "term": { "status": "archived" } } ] } } }
-
Range Query
- 用于查詢字段值在某個(gè)范圍內(nèi)的文檔。
GET my_index/_search { "query": { "range": { "age": { "gte": 18, "lte": 65 } } } }
-
Aggregation Queries文章來源:http://www.zghlxwxcb.cn/news/detail-848756.html
- 聚合查詢不是用來尋找特定文檔的,而是用來做數(shù)據(jù)匯總統(tǒng)計(jì)。例如,計(jì)算某個(gè)字段的不同值的數(shù)量,或者按字段值分組統(tǒng)計(jì)數(shù)據(jù)。
GET my_index/_search { "aggs": { "age_buckets": { "terms": { "field": "age", "size": 10 } } } }
以上是Elasticsearch中幾種基本的查詢方式,實(shí)際上還有更多的查詢類型和組合方式,如Wildcard Query、Prefix Query、Fuzzy Query、Regexp Query等等,可以根據(jù)實(shí)際需求選擇合適的查詢方法。文章來源地址http://www.zghlxwxcb.cn/news/detail-848756.html
到了這里,關(guān)于ElasticSearch 實(shí)戰(zhàn):ES查詢索引文檔的6種方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!