【问题标题】:How do I construct this elasticsearch query object?如何构造这个 elasticsearch 查询对象?
【发布时间】:2013-02-19 14:05:10
【问题描述】:

我的文档索引如下:

{
    title: "stuff here",
    description: "stuff here",
    keywords: "stuff here",
    score1: "number here",
    score2: "number here"
}

我想执行以下查询:

  • 使用标题、描述和关键字字段来匹配文本术语。
  • 不必完全匹配。例如。如果有人搜索“我有一个大鼻子”,并且其中一个文档标题中有“鼻子”但没有“大”,则仍应返回此文档。

编辑:我尝试了这个查询并且它有效。有人可以确认这是否是正确的方法吗?谢谢。

{
    query:{
        'multi_match':{
            'query': q,
            'fields': ['title^2','description', 'keywords'],
        }
    }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你的方式绝对是你要走的路!

    multi_match 查询通常是您希望向最终用户公开的查询,而 query_string 类似,但也更强大和更危险,因为它公开了 lucene query syntax。经验法则:如果不需要,不要使用查询字符串。

    此外,搜索多个字段很容易,只需提供您想要搜索的字段列表,就像您所做的那样,无需bool query

    【讨论】:

      【解决方案2】:

      下面是创建您可以使用的查询的代码。我用 c# 编写了它,但它可以以相同的方式在其他语言中工作。

      您需要做的是创建一个 BooleanQuery 并设置它的至少 1 个条件必须匹配。然后使用 Occur.SHOULD 枚举值为要检查的每个文档字段添加条件:

      BooleanQuery searchQuery = new BooleanQuery();
      searchQuery.SetMinimumNumberShouldMatch(1);
      
      QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "title", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
      Query titleQuery = parser.Parse("I have a big nose");
      searchQuery.Add(titleQuery, BooleanClause.Occur.SHOULD);
      
      parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "description", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
      Query descriptionQuery = parser.Parse("I have a big nose");
      searchQuery.Add(titleQuery, BooleanClause.Occur.SHOULD);
      
      parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "keywords", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
      Query keywordsQuery = parser.Parse("I have a big nose");
      searchQuery.Add(titleQuery, BooleanClause.Occur.SHOULD);
      
      Query queryThatShouldBeExecuted.Add(searchQuery, BooleanClause.Occur.MUST);
      

      这里是 java http://www.javadocexamples.com/java_source/org/apache/lucene/search/TestBooleanMinShouldMatch.java.html 示例的链接

      【讨论】:

      • 问题是关于弹性搜索的。我知道这是相关的 lucene 代码,即使您在这里并不需要布尔查询,因为您可以使用 lucene multifieldqueryparser。无论如何,我真的不认为这个答案在使用弹性搜索时很有用。
      【解决方案3】:

      执行 HTTP Post 请求的相应 JSON 对象如下:

      {
        "bool": {
          "should": [
            {
              "query_string": {
                "query": "I have a big nose",
                "default_field": "title"
              }
            },
            {
              "query_string": {
                "query": "I have a big nose",
                "default_field": "description"
              }
            },
            {
              "query_string": {
                "query": "I have a big nose",
                "default_field": "keywords"
              }
            }
          ],
          "minimum_number_should_match": 1,
          "boost": 1
        }
      }
      

      【讨论】:

      • Multimatch 是 query_string 查询的包装器,两者都有效。您可能想删除标题字段上的 ^2,除非您想提升该字段。
      • 有没有办法使用 bool 进行自定义评分?例如...我想做 score1 * score2 * weight。你能告诉我如何将它合并到布尔中吗?谢谢
      • 到目前为止我还没有处理过分数,但我认为如果你用 custom_score_queries 替换 query_string 查询,它也会起作用。
      • @MeiSign multi_match 不是查询字符串的包装。查询字符串被解析并公开了 lucene 语法(强大但有潜在危险),而匹配查询被分析但更简单且危险性更小。除非你需要别的东西,否则去匹配或多重匹配!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多