简体   繁体   中英

Elasticsearch Filter term doesn't match, but it should

I have some problems with Elasticsearch 6.8:

My filter query doesn't match the string "Empty" in category

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

All the category.category have this same value for debugging this problem.

But it should work as its exactly the same term, stored in Elastic as a Text field, and it match perfectly when I try to filter the channel field "13thstreethd" but no filter match for the category field, I have no idea why !

Here is the mapping:

{
    "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"
            }
        }
    }
}

One document:

            {
                "_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"
                            }
                        }

            ]

        }
    }
}

Returning the result:

{
    "_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": []
                    }
                ]
            }
        ]
    }
} 

you should define the mappings as nested for the array of objects, and then write a nested match query

{
    "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"
            }
        }
    }
} 

and please look at this link for querying

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM