0

我正在使用PyElasticsearch(elasticsearch python 客户端库)。我正在搜索像Arvind Kejriwal India Today Economic Times这样的字符串,这给了我合理的结果。我希望我可以在搜索查询中更多地增加第一个词的权重。我怎样才能做到这一点?

res = es.search(index="article-index", fields="url", body={
  "query": {
    "query_string": {
      "query": "keywordstr",
      "fields": [
        "text",
        "title",
        "tags",
        "domain"
      ]
    }
  }
})

我现在正在使用上述命令进行搜索。

4

3 回答 3

2

将给定的查询拆分为多个术语。在您的示例中,它将是 Arvind、Kejriwal... 现在为每个给定的术语形成查询字符串查询(或字段查询或任何其他符合需要的查询)。查询字符串查询将如下所示 http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-query-string-query.html

{
    "query_string" : {
        "default_field" : "content",
        "query" : "<one of the given term>",
        "boost": <any number>
    }
}

现在你有多个像上面一样的查询,具有不同的提升值(取决于哪个具有更高的权重)。使用 BOOL 查询将所有这些查询组合成一个查询。http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html 如果您希望所有术语都出现在结果中,查询将是这样的。

{
    "bool" : {
        "must" :  [q1, q2, q3 ...]
    }
}

您可以使用布尔查询的不同选项。例如,您希望结果中出现 3 个术语中的任何一个,然后查询将类似于

{
    "bool" : {
        "should" :  [q1, q2,q3 ...]
    },
    "minimum_should_match" : 3,
}
于 2014-03-18T15:42:41.543 回答
0

理论上:

  1. 使用 api 拆分成术语
  2. 查询具有不同提升的术语
于 2014-03-18T12:53:22.117 回答
0

Lucene 查询语法可以解决问题。谢谢

http://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Boosting%20a%20Term

于 2014-03-18T17:42:44.463 回答