【问题标题】:Elasticsearch Filter term doesn't match, but it shouldElasticsearch 过滤器术语不匹配,但应该
【发布时间】:2019-07-18 11:59:57
【问题描述】:

我在使用 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


    【解决方案1】:

    您应该为对象数组定义mappingsnested,然后编写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

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 2013-07-28
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      相关资源
      最近更新 更多