首页 > 解决方案 > Elasticsearch 过滤器术语不匹配,但应该

问题描述

我对 Elasticsearch 6.8 有一些问题:

我的过滤器查询与类别中的字符串“Empty”不匹配

("category.category:Empty doesn't match id 3991")

所有 category.category 都具有用于调试此问题的相同值。

但它应该与完全相同的术语一样工作,作为文本字段存储在 Elastic 中,当我尝试过滤通道字段“13thstreethd”但类别字段没有过滤器匹配时,它完美匹配,我不知道为什么!

这是映射:

{
    "broadcasttest": {
        "aliases": {},
        "mappings": {
            "doc": {
                "properties": {
                    "category": {
                        "properties": {
                            "category": {
                                "type": "text"
                            }
                        }
                    },
                    "channel": {
                        "properties": {
                            "channel": {
                                "type": "text"
                            }
                        }
                    },
                    "genre": {
                        "properties": {
                            "genre": {
                                "type": "text"
                            }
                        }
                    },
                    "title": {
                        "type": "text"
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1563430845642",
                "number_of_shards": "1",
                "number_of_replicas": "0",
                "uuid": "Y1V-LEQoQuW2tMZmnTXDxw",
                "version": {
                    "created": "6080099"
                },
                "provided_name": "broadcasttest"
            }
        }
    }
}

一份文件:

            {
                "_index": "broadcasttest",
                "_type": "doc",
                "_id": "3933239",
                "_source": {
                    "title": "Law & Order: Special Victims Unit",
                    "category": [
                        {
                            "category": "Empty"
                        }
                    ],
                    "genre": [
                        {
                            "genre": "Crime"
                        }
                    ],
                    "channel": [
                        {
                            "channel": "13thstreethd"
                        }
                    ]
                }
            }

http://127.0.0.1:9200/broadcasttest/doc/3933239/_explain

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "category.category": {
                            "query": "Empty"
                        }
                    }
                }

            ],
            "filter": [
                        {
                            "term": {
                                "category.category": "Empty"
                            }
                        }

            ]

        }
    }
}

返回结果:

{
    "_index": "broadcasttest",
    "_type": "doc",
    "_id": "3933239",
    "matched": false,
    "explanation": {
        "value": 0,
        "description": "Failure to meet condition(s) of required/prohibited clause(s)",
        "details": [
            {
                "value": 0.000034882705,
                "description": "weight(category.category:empty in 3991) [PerFieldSimilarity], result of:",
                "details": [
                    {
                        "value": 0.000034882705,
                        "description": "score(doc=3991,freq=1.0 = termFreq=1.0\n), product of:",
                        "details": [
                            {
                                "value": 0.000034882705,
                                "description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
                                "details": [
                                    {
                                        "value": 14333,
                                        "description": "docFreq",
                                        "details": []
                                    },
                                    {
                                        "value": 14333,
                                        "description": "docCount",
                                        "details": []
                                    }
                                ]
                            },
                            {
                                "value": 1,
                                "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:",
                                "details": [
                                    {
                                        "value": 1,
                                        "description": "termFreq=1.0",
                                        "details": []
                                    },
                                    {
                                        "value": 1.2,
                                        "description": "parameter k1",
                                        "details": []
                                    },
                                    {
                                        "value": 0.75,
                                        "description": "parameter b",
                                        "details": []
                                    },
                                    {
                                        "value": 1,
                                        "description": "avgFieldLength",
                                        "details": []
                                    },
                                    {
                                        "value": 1,
                                        "description": "fieldLength",
                                        "details": []
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "value": 0,
                "description": "no match on required clause (category.category:Empty)",
                "details": [
                    {
                        "value": 0,
                        "description": "category.category:Empty doesn't match id 3991",
                        "details": []
                    }
                ]
            }
        ]
    }
} 

标签: elasticsearch

解决方案


您应该为对象数组定义mappingsas nested,然后编写一个nested匹配查询

{
    "broadcasttest": {
        "aliases": {},
        "mappings": {
            "doc": {
                "properties": {
                    "category": {
                        "type": "nested",  <=== see this change
                        "properties": {
                            "category": {
                                "type": "text"
                            }
                        }
                    },
                    "channel": {
                         "type": "nested",  <=== see this change
                        "properties": {
                            "channel": {
                                "type": "text"
                            }
                        }
                    },
                    "genre": {
                         "type": "nested",   <=== see this change
                        "properties": {
                            "genre": {
                                "type": "text"
                            }
                        }
                    },
                    "title": {
                        "type": "text"
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1563430845642",
                "number_of_shards": "1",
                "number_of_replicas": "0",
                "uuid": "Y1V-LEQoQuW2tMZmnTXDxw",
                "version": {
                    "created": "6080099"
                },
                "provided_name": "broadcasttest"
            }
        }
    }
} 

请查看此链接进行查询

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html


推荐阅读