elasticsearch,Java,elasticsearch" /> elasticsearch,Java,elasticsearch" />

Java BoolQuery&x27的目的是什么;s";“过滤器”;在弹性搜索?

Java BoolQuery&x27的目的是什么;s";“过滤器”;在弹性搜索?,java,elasticsearch,Java,elasticsearch,我阅读了文件,根据它,这就是目的 过滤器 "query": { "bool": { "adjust_pure_negative": true, "boost": 1, "filter": [ { "bool": { "must": [ { "term": { "deleted": {

我阅读了文件,根据它,这就是目的

过滤器

"query": {
    "bool": {
      "adjust_pure_negative": true,
      "boost": 1,
      "filter": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "deleted": {
                    "value": "0",
                    "boost": 1
                  }
                }
              },
              {
                "match": {
                  "isPrivate": {
                    "query": true
                  }
                }
              }
            ],
            "should": [
              {
                "term": {
                  "isPrivate": {
                    "value": "false",
                    "boost": 1
                  }
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "createdBy": {
                          "value": "1742991596",
                          "boost": 1
                        }
                      }
                    },
                    {
                      "term": {
                        "isPrivate": {
                          "value": "true",
                          "boost": 1
                        }
                      }
                    }
                  ],
                  "adjust_pure_negative": true,
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ]
    }
  }
子句(查询)必须出现在匹配的文档中。然而不同 必须忽略查询的分数。过滤子句是 在过滤器上下文中执行,这意味着评分被忽略,并且 缓存时考虑子句

同样来自BoolQueryBuilder类:

   /**
     * Adds a query that <b>must</b> appear in the matching documents but will
     * not contribute to scoring. No {@code null} value allowed.
     */
    public BoolQueryBuilder filter(QueryBuilder queryBuilder) {
        if (queryBuilder == null) {
            throw new IllegalArgumentException("inner bool query clause cannot be null");
        }
        filterClauses.add(queryBuilder);
        return this;
    }
使用过滤器进行查询

"query": {
    "bool": {
      "adjust_pure_negative": true,
      "boost": 1,
      "filter": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "deleted": {
                    "value": "0",
                    "boost": 1
                  }
                }
              },
              {
                "match": {
                  "isPrivate": {
                    "query": true
                  }
                }
              }
            ],
            "should": [
              {
                "term": {
                  "isPrivate": {
                    "value": "false",
                    "boost": 1
                  }
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "createdBy": {
                          "value": "1742991596",
                          "boost": 1
                        }
                      }
                    },
                    {
                      "term": {
                        "isPrivate": {
                          "value": "true",
                          "boost": 1
                        }
                      }
                    }
                  ],
                  "adjust_pure_negative": true,
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ]
    }
  }

在您的情况下,您肯定应该使用
bool/filter
,因为您没有任何有助于评分的约束,所有约束都是yes/no匹配项,并且通过使用
filter
您可以从过滤器缓存中获益(在使用must时您不需要)

因此,一定要使用filter选项,但稍作修改(您根本不需要,而且您的布尔逻辑没有正确地转换为bool查询):

综上所述:

  • =或条件
  • 必须
    =和条件(需要评分时)
  • 过滤器
    =和条件(当不需要评分和/或当您希望从过滤器缓存中获益时)
  • 奖励:
    不得
    =非条件

谢谢你,Val,你能不能给我解释一下过滤器和[必须/应该]之间的区别?另外,当我得到是/否答案时,情况并不总是如此。我也在使用“匹配查询”来解决评分问题,在这种情况下,你有什么建议。你能举个例子吗?评分只有在使用全文查询时才有意义,这是您的情况吗?使用
match
查询并不意味着使用计分。只是用我的数据集测试了你的示例,它给了我错误的结果,比应该的结果大得多,我怀疑的原因是因为你使用的是
should[bool[1和2],bool[should[1或2]
在我的情况下是错误的,我需要的正好相反:
must[bool[1*和*2],bool[should[1或2]]
但尝试此操作会得到0个结果是的,我确实有全文查询。
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "filter": [
        {
          "term": {
            "deleted": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "term": {
                  "isPrivate": {
                    "value": "false",
                    "boost": 1
                  }
                }
              },
              {
                "bool": {
                  "filter": [
                    {
                      "term": {
                        "createdBy": {
                          "value": "1742991596",
                          "boost": 1
                        }
                      }
                    },
                    {
                      "term": {
                        "isPrivate": {
                          "value": "true",
                          "boost": 1
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}