0

我在弹性搜索索引中有一些文档。这是示例文档

文档1

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

文档2

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

文档3

{
"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”的文件。提前致谢!

4

1 回答 1

2

要为较大的文档添加额外的提升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":{}
}

使用函数 score 查询为 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"
        }
    }
}
于 2017-06-07T06:58:48.393 回答