在Elasticsearch中,根据字段值对记录进行排序

2024-09-20 22:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在记录模式中有一个字段distribution,如下所示:

...
"distribution": {
    "properties": {
        "availability": {
            "type": "keyword"
         } 
     }
}
...

我想对distribution.availability == "ondemand"比其他记录低的记录进行排名

我查看了Elasticsearchdocs,但找不到一种方法来降低这类记录在索引时间中的分数,使其在搜索结果中显得更低

我怎样才能做到这一点,任何指向相关源代码的指针也就足够了

更多信息:

在python客户端的帮助下,我在查询时间中完全省略了这些ondemand记录,如下所示:

from elasticsearch_dsl.query import Q

_query = Q("query_string", query=query_string) & ~Q('match', **{'availability.keyword': 'ondemand'})

现在,我想包括这些记录,但我想把它们放在比其他记录低的位置

如果在索引时间中无法实现类似的功能,请建议如何使用python客户端在查询时间中实现

应用llermaly的建议后,python客户端查询如下所示:

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

Tags: 客户端stringmatch记录时间模式propertiesquery
1条回答
网友
1楼 · 发布于 2024-09-20 22:21:27

EDIT2elasticsearch-dsl-py版本的boosting查询

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上增加索引时间 https://www.elastic.co/guide/en/elasticsearch/reference/7.11/mapping-boost.html

您可以使用Boosting query在查询时实现这一点

摄取文档

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"
          }
        }
      }
    ]
  }
}

有关更高级的操作,您可以检查Function Score Query

相关问题 更多 >

    热门问题