如何编写包含所有包含,一个包含,完全包含和不包含操作的Elasticsearch查询? [英] How to write an Elasticsearch query involving contains-all-of, contains-one-of, contains-exactly and not-contains operations?

查看:417
本文介绍了如何编写包含所有包含,一个包含,完全包含和不包含操作的Elasticsearch查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的文档:

{
'body': '',
'date': '',
}

我想获取符合以下条件的文档:

I want to get documents with these conditions:


  • 正文包含以下所有内容:['a','b','c']

  • 并包含以下其中之一:['d','e','f']

  • ,并且完全包含以下短语:['gh i','jk l']

  • 并且不包含:['m','n']

  • body contains all of: ['a', 'b', 'c']
  • and contains one of: ['d', 'e', 'f']
  • and contains exactly these phrases: ['g h i', 'j k l']
  • and not contains: ['m', 'n']

如何创建此查询?

推荐答案

您需要使用布尔查询。重要的是要注意,对于您的完全包含这些短语,其工作方式取决于您对主体字段应用的分析器。

You need to use bool queries. Important to note that for your "contains exactly these phrases" how that works depends on what analyzers you have applied to the body field.

https://www.elastic.co/guide/zh-CN/elasticsearch/ reference / 5.6 / query-dsl-bool-query.html

例如:

{
  "query": {
    "bool": {
      "must": [
        {"match": {"body": "a"}},
        {"match": {"body": "b"}},
        {"match": {"body": "c"}},
        {"match_phrase": {"body": "g h i"}},
        {"match_phrase": {"body": "j k l"}}
      ],
      "should": [
        {"match": {"body": "d"}},
        {"match": {"body": "e"}},
        {"match": {"body": "f"}}
      ],
      "must_not": [
        {"match": {"body": "m"}},
        {"match": {"body": "n"}}
      ]
    }
  }
}

这篇关于如何编写包含所有包含,一个包含,完全包含和不包含操作的Elasticsearch查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆