Elasticsearch过滤条件不匹配,但应匹配

时间:2019-07-18 11:59:57

标签: 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": []
                    }
                ]
            }
        ]
    }
} 

1 个答案:

答案 0 :(得分:0)

您应该将对象数组的mappings定义为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