赞
踩
在Elasticsearch中,查询索引文档的方法多种多样,这里列举了6种常见的查询方法,其中包括:
简单查询(String Query)
my_index
中的所有文档执行模糊匹配查询:GET my_index/_search
{
"query": {
"match": {
"field_name": "your_search_term"
}
}
}
Match Query
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后缀避免对非分析字段进行分词
}
}
}
Bool Query
GET my_index/_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "search term" } },
{ "range": { "date": { "gte": "2022-01-01" } } }
],
"must_not": [
{ "term": { "status": "archived" } }
]
}
}
}
Range Query
GET my_index/_search
{
"query": {
"range": {
"age": {
"gte": 18,
"lte": 65
}
}
}
}
Aggregation Queries
GET my_index/_search
{
"aggs": {
"age_buckets": {
"terms": {
"field": "age",
"size": 10
}
}
}
}
以上是Elasticsearch中几种基本的查询方式,实际上还有更多的查询类型和组合方式,如Wildcard Query、Prefix Query、Fuzzy Query、Regexp Query等等,可以根据实际需求选择合适的查询方法。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。