我正在使用PyElasticsearch
(elasticsearch python客户端库)。我正在搜索这样的字符串,例如《今日印度经济时报》 Arvind Kejriwal ,这给了我合理的结果。我希望可以在搜索查询中进一步增加第一个单词的权重。我怎样才能做到这一点?
res = es.search(index="article-index", fields="url", body={
"query": {
"query_string": {
"query": "keywordstr",
"fields": [
"text",
"title",
"tags",
"domain"
]
}
}
})
我正在使用上面的命令立即搜索。
最佳答案
将给定查询分为多个术语。在您的示例中,将是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 ...]
}
}
您可以使用 bool(boolean) 查询的不同选项。例如,您要在结果中显示3个术语中的任何一个,则查询将像
{
"bool" : {
"should" : [q1, q2,q3 ...]
},
"minimum_should_match" : 3,
}
关于elasticsearch - 与第一个搜索词相对应的Elastic Search Boost查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22476315/