Python 在Elasticsearch中,根据字段值对记录进行排序
我在记录模式中有一个字段Python 在Elasticsearch中,根据字段值对记录进行排序,python,
elasticsearch,mapping,ranking,Python,
elasticsearch,Mapping,Ranking,我在记录模式中有一个字段分布,如下所示: ... "distribution": { "properties": { "availability": { "type": "keyword" } } } ... 我想对记录进行排序,使其distribution.availability==“ondemand”低于其他记录
分布
,如下所示:
...
"distribution": {
"properties": {
"availability": {
"type": "keyword"
}
}
}
...
我想对记录进行排序,使其distribution.availability==“ondemand”
低于其他记录
我查看了Elasticsearch,但找不到一种方法来降低这类记录在索引时间中的分数,使其在搜索结果中显得更低
我怎样才能做到这一点,任何指向相关源代码的指针也就足够了
更多信息:
在查询时间中,在python客户端的帮助下,我完全省略了这些ondemand
记录,如下所示:
from elasticsearch_dsl.query import Q
_query = Q("query_string", query=query_string) & ~Q('match', **{'availability.keyword': 'ondemand'})
boosting_query = Q(
"boosting",
positive=Q("match_all"),
negative=Q(
"bool", filter=[Q({"term": {"distribution.availability.keyword": "ondemand"}})]
),
negative_boost=0.5,
)
if query_string:
_query = Q("query_string", query=query_string) & boosting_query
else:
_query = Q() & boosting_query
现在,我想包括这些记录,但我想把它们放在比其他记录低的位置
如果在索引时间中无法实现类似的功能,请建议如何使用python客户端在查询时间中实现
应用llermaly的建议后,python客户端查询如下所示:
from elasticsearch_dsl.query import Q
_query = Q("query_string", query=query_string) & ~Q('match', **{'availability.keyword': 'ondemand'})
boosting_query = Q(
"boosting",
positive=Q("match_all"),
negative=Q(
"bool", filter=[Q({"term": {"distribution.availability.keyword": "ondemand"}})]
),
negative_boost=0.5,
)
if query_string:
_query = Q("query_string", query=query_string) & boosting_query
else:
_query = Q() & boosting_query
EDIT2:增强查询的版本
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
from elasticsearch_dsl import Q
client = Elasticsearch()
q = Q('boosting', positive=Q("match_all"), negative=Q('bool', filter=[Q({"term": {"test.available.keyword": "ondemand"}})]), negative_boost=0.5)
s = Search(using=client, index="test_parths007").query(q)
response = s.execute()
print(response)
for hit in response:
print(hit.meta.score, hit.test.available)
编辑:只需阅读你需要在索引时间完成的内容
Elasticsearch不推荐在5.0上增加索引时间
您可以在查询时使用
摄取文档
POST test_parths007/_doc
{
"name": "doc1",
"test": {
"available": "ondemand"
}
}
POST test_parths007/_doc
{
"name": "doc1",
"test": {
"available": "higherscore"
}
}
POST test_parths007/_doc
{
"name": "doc2",
"test": {
"available": "higherscore"
}
}
查询(索引时间)
响应
POST test_parths007/_search
{
"query": {
"boosting": {
"positive": {
"match_all": {}
},
"negative": {
"term": {
"test.available.keyword": "ondemand"
}
},
"negative_boost": 0.5
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_parths007",
"_type" : "_doc",
"_id" : "VMdY7XcB50NMsuQPelRx",
"_score" : 1.0,
"_source" : {
"name" : "doc2",
"test" : {
"available" : "higherscore"
}
}
},
{
"_index" : "test_parths007",
"_type" : "_doc",
"_id" : "Vcda7XcB50NMsuQPiVRB",
"_score" : 1.0,
"_source" : {
"name" : "doc1",
"test" : {
"available" : "higherscore"
}
}
},
{
"_index" : "test_parths007",
"_type" : "_doc",
"_id" : "U8dY7XcB50NMsuQPdlTo",
"_score" : 0.5,
"_source" : {
"name" : "doc1",
"test" : {
"available" : "ondemand"
}
}
}
]
}
}
对于更高级的操作,您可以检查谢谢,但我需要在索引时间而不是查询时间进行操作。有什么特别的原因需要在索引时间进行操作吗?Elasticsearch不赞成在5.0上增加索引时间,更多信息,也许我可以向您推荐另一种方法。很抱歉反应太晚,但我知道不赞成增加索引时间,我正在寻找其他方法,可以帮助提高除
ondemand
之外的其他记录的分数,或者为ondemand
。我可以在查询时执行此操作,但我使用elasticsearch\u dsl.query
Q
在python中查询elasticsearch实例。如果你知道在那里有什么办法,那就太好了。现在我明白了。我可以用python dsl格式重现boosting查询。我将编辑我的答案。请让我知道它是否有用