在布尔查询中,评分函数用于对文档进行评分,并根据评分对文档进行排序。以下是几种常见的评分函数及其代码示例。
from elasticsearch import Elasticsearch
# 创建 Elasticsearch 实例
es = Elasticsearch()
# 构建查询语句
query = {
"query": {
"bool": {
"must": [
{"term": {"field1": "value1"}},
{"term": {"field2": "value2"}}
]
}
}
}
# 执行查询
res = es.search(index="your_index", body=query)
# 输出评分结果
for hit in res['hits']['hits']:
print(hit['_score'])
from elasticsearch import Elasticsearch
# 创建 Elasticsearch 实例
es = Elasticsearch()
# 构建查询语句
query = {
"query": {
"match": {"field1": "value1"}
}
}
# 执行查询
res = es.search(index="your_index", body=query)
# 输出评分结果
for hit in res['hits']['hits']:
print(hit['_score'])
from elasticsearch import Elasticsearch
# 创建 Elasticsearch 实例
es = Elasticsearch()
# 构建查询语句
query = {
"query": {
"bool": {
"should": [
{"match": {"field1": {"query": "value1", "boost": 2}}},
{"match": {"field2": {"query": "value2", "boost": 1}}}
]
}
}
}
# 执行查询
res = es.search(index="your_index", body=query)
# 输出评分结果
for hit in res['hits']['hits']:
print(hit['_score'])
以上是几种常见的布尔查询中的评分函数及其代码示例。根据具体的需求,可以选择适合的评分函数来对文档进行评分和排序。
下一篇:布尔查询中的弹性平均聚合