elasticsearch 使用过滤器根据嵌套字段值修改elasticsearch分数,elasticsearch,elasticsearch" /> elasticsearch 使用过滤器根据嵌套字段值修改elasticsearch分数,elasticsearch,elasticsearch" />

elasticsearch 使用过滤器根据嵌套字段值修改elasticsearch分数

elasticsearch 使用过滤器根据嵌套字段值修改elasticsearch分数,elasticsearch,elasticsearch,Elasticsearch版本:7.11 我的索引结构如下: PUT /my-test-index { "mappings": { "properties": { "brand": { "type": "text" }, "id": {

Elasticsearch版本:7.11

我的索引结构如下:

PUT /my-test-index
{
    "mappings": {
        "properties": {
            "brand": {
                "type": "text"
            },
            "id": {
                "type": "keyword"
            },
            "availabilityId": {
                "type": "integer"
            },
            "priorities": {
                "type": "nested",
                "properties": {
                    "typeId": {
                        "type": "keyword"
                    },
                    "value": {
                        "type": "integer"
                    }
                }
            },
            "title": {
                "type": "text"
            }
        }
    }
}
例如,使用此数据:

POST /my-test-index/_doc
{
    "id": 1,
    "brand": "Milk One",
    "availabilityId": 1,
    "title": "Great Value 2% Reduced-Fat Milk, 0.5 Gallon, 64 Fl. Oz",
    "priorities": [
        {
            "typeId": 1,
            "value": 3000
        },
        {
            "typeId": 2,
            "value": 4000
        }
    ]
}

POST /my-test-index/_doc
{
    "id": 2,
    "brand": "Milky Two",
    "availabilityId": 2,
    "title": "Great Value 3.5% Milk Fat Whole Milk, 64 Fl Oz",
    "priorities": [
        {
            "typeId": 1,
            "value": 1000
        },
        {
            "typeId": 2,
            "value": 3000
        }
    ]
}
    
POST /my-test-index/_doc
{
    "id": 3,
    "brand": "Nesty",
    "availabilityId": 3,
    "title": "Great Value 1% Low-Fat Milk, 128 Fl Oz"
}

POST /my-test-index/_doc
{
    "id": 4,
    "brand": "No milk",
    "availabilityId": 4,
    "title": "Almond Breeze Vanilla Almondmilk",
    "priorities": [
        {
            "typeId": 1,
            "value": 6000
        },
        {
            "typeId": 2,
            "value": 2000
        }
    ]
}

我想:

  • 按可用性ID(1、2、3)筛选的文档
  • 按标题、品牌进行多重匹配
  • 用log1p乘以priorities.value修改分数,但只能使用type=1。如果优先级为空,则将其设置为6000
  • 按分数排序
  • 我的问题是:

    POST /my-test-index/_search?typed_keys=true
    {
        "query": {
            "bool": {
                "filter": [
                    {
                        "bool": {
                            "filter": [
                                {
                                    "terms": {
                                        "availabilityId": [
                                            "1",
                                            "2",
                                            "3"
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                ],
                "must": [
                    {
                        "bool": {
                            "should": [
                                {
                                    "multi_match": {
                                        "fields": [
                                            "title^100",
                                            "brand^15"
                                        ],
                                        "fuzziness": 0,
                                        "minimum_should_match": "2<80%",
                                        "query": "milk",
                                        "type": "most_fields"
                                    }
                                }
                            ]
                        }
                    }
                ],
                "should": [
                    {
                        "nested": {
                            "path": "priorities",
                            "query": {
                                "function_score": {
                                    "query": {
                                        "match": {
                                            "priorities.typeId": "1"
                                        }
                                    },
                                    "functions": [
                                        {
                                            "field_value_factor": {
                                                "field": "priorities.value",
                                                "modifier": "log1p", 
                                                "missing": 6000
                                            }
                                        }
                                    ]
                                }
                            }
                        }
                    }
                ]
            }
        },
        "sort": [
            {
                "_score": {
                    "order": "desc"
                }
            },
            {
                "_id": {
                    "order": "asc"
                }
            }
        ]
    }
    
    POST/my test index/\u search?键入的\u keys=true
    {
    “查询”:{
    “布尔”:{
    “过滤器”:[
    {
    “布尔”:{
    “过滤器”:[
    {
    “条款”:{
    “可用性ID”:[
    "1",
    "2",
    "3"
    ]
    }
    }
    ]
    }
    }
    ],
    “必须”:[
    {
    “布尔”:{
    “应该”:[
    {
    “多重匹配”:{
    “字段”:[
    “标题^100”,
    “品牌^15”
    ],
    “模糊性”:0,
    
    “最小应匹配”:“2我使用函数脚本\u分数解决了我的问题:

    POST /my-test-index/_search?typed_keys=true
    {
        "query": {
            "function_score": {
                "functions": [
                    {
                      "script_score": {
                        "script" : """double priority1 = 6000; if(params._source["priorities"] != null){for (int i = 0; i < params._source["priorities"].length; ++i){ if(params._source["priorities"][i].typeId == 1) priority1 = params._source["priorities"][i].value;}} return Math.log10(priority1 + 1);"""
                      }
                    }
                ],
                "query": {
                    "bool": {
                        "filter": [
                            {
                                "bool": {
                                    "filter": [
                                        {
                                            "terms": {
                                                "availabilityId": [
                                                    "1",
                                                    "2",
                                                    "3"
                                                ]
                                            }
                                        }
                                    ]
                                }
                            }
                        ],
                        "must": [
                            {
                                "bool": {
                                    "should": [
                                        {
                                            "multi_match": {
                                                "fields": [
                                                    "title^100",
                                                    "brand^15"
                                                ],
                                                "fuzziness": 0,
                                                "minimum_should_match": "2<80%",
                                                "query": "milk",
                                                "type": "most_fields"
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            }
        },
        "sort": [
            {
                "_score": {
                    "order": "desc"
                }
            },
            {
                "_id": {
                    "order": "asc"
                }
            }
        ]
    }
    
    POST/my test index/\u search?键入的\u keys=true
    {
    “查询”:{
    “功能评分”:{
    “职能”:[
    {
    “脚本_分数”:{
    “脚本”:“”“双优先级1=6000;if(params._source[“priorities”!=null){for(int i=0;i