1

我的文档索引如下:

{
    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'],
        }
    }
}
4

3 回答 3

2

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

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

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

于 2013-03-05T14:08:20.210 回答
0

执行 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
  }
}
于 2013-03-05T09:16:06.640 回答
0

下面是创建您可以使用的查询的代码。我用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中示例的链接

于 2013-03-05T08:50:34.287 回答