gpt4 book ai didi

elasticsearch - 具有多个值的字段中的Elasticsearch Boost查询

转载 作者:行者123 更新时间:2023-12-03 00:55:54 25 4
gpt4 key购买 nike

我在elasticsearch索引中有一些文档。这是样本文件

DOC1

{
"feild1":["hi","hello","goodmorning"]
"feild2":"some string"
"feild3":{}
}

DOC2
{
"feild1":["hi","goodmorning"]
"feild2":"some string"
"feild3":{}
}

DOC3
{
"feild1":["hi","hello"]
"feild2":"some string"
"feild3":{}
}

我想查询具有值“hi”和“hello”的feild1(如果同时存在),则该文档应首先出现(如果存在),则应在此之后。
例如:
结果应按DOC1,DOC3,DOC2的顺序排列。我尝试了提升查询。但是它没有按照我想要的顺序进行调整。这是我正在尝试的查询。
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"avail_status": true
}
},
{
"bool": {
"should": [
{
"constant_score": {
"filter": {
"terms": {
"feild1": [
"hi"
]
}
},
"boost": 20
}
},
{
"constant_score": {
"filter": {
"terms": {
"feild1": [
"hello"
]
}
},
"boost": 18
}
}
]
}
}
]
}
}
}

首先是给我那些带有“hi”的文档,然后是那些带有“hello”的文档。提前致谢!

最佳答案

要为具有更大field1的文档增加额外的提升,您可以添加funtion_score脚本分数。

映射

{
"mappings": {
"document_type" : {
"properties": {
"field1" : {
"type": "text",
"fielddata": true
},
"field2" : {
"type": "text"
},
"field3" : {
"type": "text"
}
}
}
}
}

索引文件
POST custom_score_index1/document_type

{
"feild1":["hi","hello","goodmorning"],
"feild2":"some string",
"feild3":{}
}

POST custom_score_index1/document_type

{
"feild1":["hi","goodmorning"],
"feild2":"some string",
"feild3":{}
}

POST custom_score_index1/document_type

{
"feild1":["hi","hello"],
"feild2":"some string",
"feild3":{}
}

使用函数得分的查询为field1增加了更大的_score
POST custom_score_index1/document_type/_search
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [{
"match_phrase": {
"avail_status": true
}
},
{
"bool": {
"should": [{
"constant_score": {
"filter": {
"terms": {
"feild1": [
"hi"
]
}
},
"boost": 20
}
},
{
"constant_score": {
"filter": {
"terms": {
"feild1": [
"hello"
]
}
},
"boost": 18
}
}
]
}
}
]
}
},
"functions": [{
"script_score": {
"script": {
"inline": "_score + 10000 * doc['field1'].length"
}
}
}],
"score_mode": "sum",
"boost_mode": "sum"
}
}
}

关于elasticsearch - 具有多个值的字段中的Elasticsearch Boost查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44384445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com