ES常用多条件查询

ES常用多条件查询

1. bool 查询

含义:使用 mustfilter 条件,必须匹配 field1 的值为 value1,并且 field2 的值为 value2
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "field1": "value1" } }
      ],
      "filter": [
        { "term": { "field2": "value2" } }
      ]
    }
  }
}'

2. must_not 查询

含义:排除 field1 的值为 value1 的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must_not": [
        { "term": { "field1": "value1" } }
      ]
    }
  }
}'

3. should 查询

含义:至少满足一个条件,field1 的值为 value1field2 的值为 value2
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "should": [
        { "term": { "field1": "value1" } },
        { "term": { "field2": "value2" } }
      ],
      "minimum_should_match": 1
    }
  }
}'

4. range 查询

含义:范围查询,field1 的值在 10 到 20 之间。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "range": {
      "field1": {
        "gte": 10,
        "lte": 20
      }
    }
  }
}'

5. exists 查询

含义:查询 field1 存在的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "exists": {
      "field": "field1"
    }
  }
}'

6. prefix 查询

含义:前缀查询,field1 的值以 val 开头。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "prefix": {
      "field1": "val"
    }
  }
}'

7. wildcard 查询

含义:通配符查询,field1 的值以 val 开头。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "wildcard": {
      "field1": "val*"
    }
  }
}'

8. regexp 查询

含义:正则表达式查询,field1 的值匹配正则表达式 val.*
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "regexp": {
      "field1": "val.*"
    }
  }
}'

9. fuzzy 查询

含义:模糊查询,field1 的值接近 value1
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "fuzzy": {
      "field1": {
        "value": "value1",
        "fuzziness": "AUTO"
      }
    }
  }
}'

10. ids 查询

含义:ID 查询,查询 ID 为 1, 2, 3 的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "ids": {
      "values": ["1", "2", "3"]
    }
  }
}'

11. terms 查询

含义:多值匹配查询,field1 的值为 value1, value2, 或 value3
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "terms": {
      "field1": ["value1", "value2", "value3"]
    }
  }
}'

12. match 查询

含义:匹配查询,field1 的值为 value1
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "field1": "value1"
    }
  }
}'

13. match_phrase 查询

含义:短语匹配查询,field1 的值为 value1 value2
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_phrase": {
      "field1": "value1 value2"
    }
  }
}'

14. multi_match 查询

含义:多字段匹配查询,field1field2 的值为 value1
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "multi_match": {
      "query": "value1",
      "fields": ["field1", "field2"]
    }
  }
}'

15. constant_score 查询

含义:常量评分查询,field1 的值为 value1,并且提升评分为 1.2。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "constant_score": {
      "filter": {
        "term": { "field1": "value1" }
      },
      "boost": 1.2
    }
  }
}'

16. dis_max 查询

含义:不相交最大查询,field1 的值为 value1field2 的值为 value2,并且使用 tie_breaker 为 0.7。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "dis_max": {
      "queries": [
        { "term": { "field1": "value1" } },
        { "term": { "field2": "value2" } }
      ],
      "tie_breaker": 0.7
    }
  }
}'

17. function_score 查询

含义:函数评分查询,field1 的值为 value1,并且 field2 的值为 value2 时,权重为 2,评分模式为乘法。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "function_score": {
      "query": { "match": { "field1": "value1" } },
      "functions": [
        {
          "filter": { "term": { "field2": "value2" } },
          "weight": 2
        }
      ],
      "boost_mode": "multiply"
    }
  }
}'

18. nested 查询

含义:嵌套查询,nested_field.field1 的值为 value1
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "nested": {
      "path": "nested_field",
      "query": {
        "match": { "nested_field.field1": "value1" }
      }
    }
  }
}'

19. has_child 查询

含义:子文档查询,查询子文档类型为 child_typefield1 的值为 value1 的父文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "has_child": {
      "type": "child_type",
      "query": {
        "match": { "field1": "value1" }
      }
    }
  }
}'

20. has_parent 查询

含义:父文档查询,查询父文档类型为 parent_typefield1 的值为 value1 的子文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "has_parent": {
      "parent_type": "parent_type",
      "query": {
        "match": { "field1": "value1" }
      }
    }
  }
}'

21. geo_distance 查询

含义:地理距离查询,查询距离特定地理位置(lat, lon)在 200km 以内的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "geo_distance": {
      "distance": "200km",
      "location": {
        "lat": 40,
        "lon": -70
      }
    }
  }
}'

22. geo_bounding_box 查询

含义:地理边界框查询,查询在特定边界框内的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "geo_bounding_box": {
      "location": {
        "top_left": {
          "lat": 40.73,
          "lon": -74.1
        },
        "bottom_right": {
          "lat": 40.01,
          "lon": -71.12
        }
      }
    }
  }
}'

23. geo_polygon 查询

含义:地理多边形查询,查询在特定多边形内的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "geo_polygon": {
      "location": {
        "points": [
          { "lat": 40, "lon": -70 },
          { "lat": 30, "lon": -80 },
          { "lat": 20, "lon": -90 }
        ]
      }
    }
  }
}'

24. script 查询

含义:脚本查询,使用自定义脚本查询文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "script": {
      "script": {
        "source": "doc['field1'].value > params.value",
        "params": {
          "value": 5
        }
      }
    }
  }
}'

25. more_like_this 查询

含义:相似文档查询,查询与给定文本相似的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "more_like_this": {
      "fields": ["field1", "field2"],
      "like": "text to find similar documents",
      "min_term_freq": 1,
      "max_query_terms": 12
   好的,继续输出剩余的多条件查询操作。

### 25. `more_like_this` 查询

#### 含义:相似文档查询,查询与给定文本相似的文档。

```sh
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "more_like_this": {
      "fields": ["field1", "field2"],
      "like": "text to find similar documents",
      "min_term_freq": 1,
      "max_query_terms": 12
    }
  }
}'

26. percolate 查询

含义:预处理查询,查询与给定文档匹配的查询。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "field1": "value1",
        "field2": "value2"
      }
    }
  }
}'

27. span_term 查询

含义:跨度术语查询,查询 field1 的值为 value1 的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "span_term": {
      "field1": "value1"
    }
  }
}'

28. span_near 查询

含义:跨度邻近查询,查询 field1 的值为 value1value2,并且它们之间的距离不超过 5 个词。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "span_near": {
      "clauses": [
        { "span_term": { "field1": "value1" } },
        { "span_term": { "field1": "value2" } }
      ],
      "slop": 5,
      "in_order": true
    }
  }
}'

29. span_or 查询

含义:跨度或查询,查询 field1 的值为 value1value2 的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "span_or": {
      "clauses": [
        { "span_term": { "field1": "value1" } },
        { "span_term": { "field1": "value2" } }
      ]
    }
  }
}'

30. span_not 查询

含义:跨度非查询,查询 field1 的值为 value1,但不包含 value2 的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "span_not": {
      "include": { "span_term": { "field1": "value1" } },
      "exclude": { "span_term": { "field1": "value2" } }
    }
  }
}'

31. span_containing 查询

含义:跨度包含查询,查询包含 field1 的值为 value1 的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "span_containing": {
      "little": { "span_term": { "field1": "value1" } },
      "big": { "span_term": { "field2": "value2" } }
    }
  }
}'

32. span_within 查询

含义:跨度内查询,查询 field1 的值为 value1,并且在 field2 的值为 value2 的范围内。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "span_within": {
      "little": { "span_term": { "field1": "value1" } },
      "big": { "span_term": { "field2": "value2" } }
    }
  }
}'

33. span_first 查询

含义:跨度首查询,查询 field1 的值为 value1,并且在文档的前 3 个词内。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "span_first": {
      "match": { "span_term": { "field1": "value1" } },
      "end": 3
    }
  }
}'

34. exists 查询

含义:存在查询,查询 field1 存在的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "exists": {
      "field": "field1"
    }
  }
}'

35. prefix 查询

含义:前缀查询,查询 field1 的值以 value 为前缀的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "prefix": {
      "field1": "value"
    }
  }
}'

36. wildcard 查询

含义:通配符查询,查询 field1 的值匹配 val* 的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "wildcard": {
      "field1": "val*"
    }
  }
}'

37. regexp 查询

含义:正则表达式查询,查询 field1 的值匹配正则表达式 value.* 的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "regexp": {
      "field1": "value.*"
    }
  }
}'

38. fuzzy 查询

含义:模糊查询,查询 field1 的值为 value1,允许有编辑距离为 2 的模糊匹配。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "fuzzy": {
      "field1": {
        "value": "value1",
        "fuzziness": 2
      }
    }
  }
}'

39. type 查询

含义:类型查询,查询文档类型为 type1 的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "type": {
      "value": "type1"
    }
  }
}'

40. terms_set 查询

含义:术语集查询,查询 field1 的值包含在给定的术语集中的文档。
curl -X GET "localhost:9200/index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "terms_set": {
      "field1": {
        "terms": ["value1", "value2", "value3"],
        "minimum_should_match_field": "required_matches"
      }
    }
  }
}'
  • 18
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ES (Elasticsearch) 是由 Elasticsearch 公司提供的开源全文搜索及分析引擎,它基于 Lucene,用于构建高性能、分布式的大数据处理系统。ES 支持实时查询,并能高效地处理大量的日志、事件流和其他实时数据源。下面是一些常用ES 查询语句及其解释: ### 1. **基本搜索查询** 基础的全文搜索语句,查找包含指定关键字的所有文档。 ```json GET /index_name/_search { "query": { "match": { "field_name": "keyword" } } } ``` 这里 `index_name` 是索引名称,`field_name` 是需要搜索的字段名,`keyword` 是你要搜索的关键字。 ### 2. **范围查询** 用于查询特定范围内值的记录。 ```json GET /index_name/_search { "query": { "range": { "numeric_field": { "gt": 5, "lt": 10 } } } } ``` 在这个例子中,我们查询 `numeric_field` 字段大于5且小于10的记录。 ### 3. **聚合查询** 聚合查询用于对结果集进行统计汇总。 ```json GET /index_name/_search { "aggs": { "my_aggregation": { "terms": { "field": "category", "size": 10 }, "aggs": { "average_price": { "avg": { "field": "price" } } } } } } ``` 这个查询会将所有文档按照 `category` 字段分组,并计算每个类别下平均价格。 ### 4. **过滤查询** 用于进一步缩小搜索结果的条件筛选。 ```json GET /index_name/_search { "query": { "bool": { "must": [ {"term": { "status": "active" }}, {"range": {"timestamp": { "gte": "2021-01-01T00:00:00Z", "lte": "2021-12-31T23:59:59Z" }}} ] } } } ``` 此查询将只返回状态为“active”且时间戳在2021年之间的文档。 ### 5. **高亮显示查询** 用于突出显示查询匹配到的结果。 ```json GET /index_name/_search { "highlight": { "pre_tags": ["<strong>"], "post_tags": ["</strong>"], "fields": { "title": {} } }, "query": { "match": { "title": "keyword" } } } ``` 这将在搜索结果的 `title` 字段中高亮显示匹配的关键字。 ### 相关问题: 1. **如何优化 ES 性能?** 2. **ES 的索引是如何工作的?** 3. **在实际项目中如何安全地利用 ES 进行大规模数据检索?**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值