elasticsearch - 如何在Elasticsearch中与filter和must_not一起编写至少N个词查询?

标签 elasticsearch elasticsearch-query

我要匹配满足以下所有条件的文档:

  • author ==“tom”
  • status!=“已删除”
  • 至少两个f1-f4字段的与给定值
  • 匹配

    (所有字段均为keyword)
    {"size":24,
    "query":{
      "bool":{
        "filter":[{"term":{"author":{"value":"tom","boost":1.0}}}],
        "must_not":[{"term":{"status":{"value":"deleted","boost":1.0}}}],
        "should":[
          {"term":{"f1":{"value":"v1","boost":1.0}}},
          {"term":{"f2":{"value":"v2","boost":1.0}}},
          {"term":{"f3":{"value":"v3","boost":1.0}}},
          {"term":{"f4":{"value":"v4","boost":1.0}}}
          ],
          "minimum_should_match":"2",
          "boost":1.0
      }}
    }
    

    更新和摘要

    我上面发布的查询实际上是正确的,但是我的es提供程序安装了一个有问题的自定义插件,该插件执行“查询优化”,导致所有“minimum_should_match”都被忽略了。如果您遇到相同的问题并且找不到任何线索,也许您应该检查是否安装了任何可疑插件

    最佳答案

    您的查询是正确的,您只需要删除"adjust_pure_negative"标志或将其更改为false。

    简而言之,如果将标志设置为true,elastic将“忽略”您的所有查询,仅使用must_not进行过滤。 source

    您也可以删除boost:1,因为默认值为1,这使其变得多余。

    编辑:我的测试

        await client.index({index: 'test', id: 5, type: 'test', body: {author: "george", status: "deleted", f1: "v1", f2: "v2"}});
        await client.index({index: 'test', id: 6, type: 'test', body: {author: "george", status: "x", f1: "v1",}});
        await client.index({index: 'test', id: 7, type: 'test', body: {author: "george", status: "u", f1: "v1", f2: "v2"}});
        await client.index({index: 'test', id: 8, type: 'test', body: {author: "george", status: "q", f1: "v1", f4: "v4"}});
        await client.index({index: 'test', id: 9, type: 'test', body: {author: "george", status: "1", f3: "v3"}});
        let x = await client.search({
            index: 'test',
            body:
                {"size":24,
                    "query":{
                        "bool":{
                            "filter":[{"term":{"author":{"value":"george","boost":1.0}}}],
                            "must_not":[{"term":{"status":{"value":"deleted","boost":1.0}}}],
                            "must":[{
                                "bool":{
                                    "should":[
                                        {"term":{"f1":{"value":"v1","boost":1.0}}},
                                        {"term":{"f2":{"value":"v2","boost":1.0}}},
                                        {"term":{"f3":{"value":"v3","boost":1.0}}},
                                        {"term":{"f4":{"value":"v4","boost":1.0}}}],
                                    "minimum_should_match":"2",
                                    "adjust_pure_negative":false,
                                    "boost":1.0}}
                            ],
                            "adjust_pure_negative":false,
                            "boost":1.0}}},
        });
    

    结果:
    2次符合预期:
    [
      {
        "_index": "test",
        "_type": "test",
        "_id": "7",
        "_score": 0.5753642,
        "_source": {
          "author": "george",
          "status": "u",
          "f1": "v1",
          "f2": "v2"
        }
      },
      {
        "_index": "test",
        "_type": "test",
        "_id": "8",
        "_score": 0.47000366,
        "_source": {
          "author": "george",
          "status": "q",
          "f1": "v1",
          "f4": "v4"
        }
      }
    ]
    

    关于elasticsearch - 如何在Elasticsearch中与filter和must_not一起编写至少N个词查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56002126/

    相关文章:

    elasticsearch - Elasticsearch-搜索通配符(包含字符串)和tf-idf分数

    elasticsearch - Elasticsearch脚本化指标聚合中的繁重计算负载

    elasticsearch - 对Elastic Search中的索引进行搜索查询时引起不同结果的时区

    java - Elasticsearch 索引包含并以搜索开头

    python - 使用 python elasticsearch 在 AWS Elasticsearch 上搜索失败返回 411

    java - 通过curl 的Elasticsearch REST api 无法从java.runtime.exec 运行

    elasticsearch - 在基巴纳使用Logstash

    ElasticSearch Nested types with List Inside 嵌套类型

    elasticsearch - 从特定值开始的Elasticsearch查询

    mysql - 如何在我的 elasticsearch 查询中附加时间戳范围?