elasticsearch在具有多个值的feild中提升查询

时间:2017-06-06 08:00:24

标签: elasticsearch solr-boost

我在elasticsearch索引中有一些文档。这是示例文档

DOC1

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

DOC2

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

文档3

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

我想查询具有值“hi”和“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”的文件,然后是那些有“你好”的文件。提前谢谢!

1 个答案:

答案 0 :(得分:1)

要为较大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"
        }
    }
}