Elasticsearch在Feild中提高查询,具有多重价值



我在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":{}
}

我想查询feild1具有" hi"one_answers" hello"的feild1,如果两个都在场,则该文档应首先出现,如果有的话,那就应该在那之后出现。例如:结果应按照Doc1,doc3,doc2的顺序进行。我尝试了Boost查询。但这不是我想要的顺序重新调整。这是我正在尝试的查询。

{
    "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":{}
}

用功能分数查询添加额外的_score for Field1

的较大尺寸
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"
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新