ElasticSearch:关键字查询不起作用

时间:2017-09-25 11:52:59

标签: elasticsearch keyword-search

我的关键字查询存在问题。

我想按键和值过滤categoryProperties。

键是“颜色”,值包含“mavi”

但它给我的文件包含的关键是“颜色”,值包含“Beyaz”

你知道为什么吗?

请求 我在下面查询searchQuery.categoryProperties.key和searchQuery.categoryProperties.values.value。

{
    "query": {
        "bool": {
            "must": [{
                "nested": {
                    "query": {
                        "bool": {
                            "must": [{
                                "nested": {
                                    "query": {
                                        "bool": {
                                            "should": [{
                                                "bool": {
                                                    "must_not": [{
                                                        "term": {
                                                            "searchQuery.categoryProperties.key": {
                                                                "value": "color"
                                                            }
                                                        }
                                                    }]
                                                }
                                            },
                                            {
                                                "bool": {
                                                    "must": [{
                                                        "term": {
                                                            "searchQuery.categoryProperties.key": {
                                                                "value": "color"
                                                            }
                                                        }
                                                    },
                                                    {
                                                        "nested": {
                                                            "query": {
                                                                "term": {
                                                                    "searchQuery.categoryProperties.values.value": {
                                                                        "value": "Mavi"
                                                                    }
                                                                }
                                                            },
                                                            "path": "searchQuery.categoryProperties.values"
                                                        }
                                                    }]
                                                }
                                            }]
                                        }
                                    },
                                    "path": "searchQuery.categoryProperties"
                                }
                            }]
                        }
                    },
                    "path": "searchQuery"
                }
            }]
        }
    }
}

这是我的 的 RESPONSE

{
    "hits": {
        "total": 1,
        "max_score": null,
        "hits": [{
            "_index": "favoritesearchsearchmodelindex_2",
            "_type": "favoritesearchsearchmodel",
            "_id": "76175",
            "_score": null,
            "_source": {
                "searchQuery": {
                    "categoryProperties": [
                    {
                        "key": "color",
                        "values": [{
                            "value": "Beyaz"
                        }]
                    }]
                }
            }
        }]
    }
}

我的文档的映射: 的 MAPPING

{
    "favoritesearchsearchmodelindex_2": {
        "mappings": {
            "favoritesearchsearchmodel": {
                "properties": {
                    "searchQuery": {
                        "type": "nested",
                        "properties": {
                            "categoryProperties": {
                                "type": "nested",
                                "properties": {
                                    "intValue": {
                                        "type": "integer"
                                    },
                                    "key": {
                                        "type": "keyword"
                                    },
                                    "values": {
                                        "type": "nested",
                                        "properties": {
                                            "value": {
                                                "type": "keyword"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我解决了我的问题。你可以看一下真正的答案:Elastic Discuss Forum

根据Mark回应,我改变了我的映射。 新映射

{
    "favoritesearchsearchmodelindex_2": {
        "mappings": {
            "favoritesearchsearchmodel": {
                "properties": {
                    "searchQuery": {
                        "type": "nested",
                        "properties": {
                            "categoryProperties": {
                                "properties": {
                                    "key": {
                                        "type": "keyword"
                                    },
                                    "numberValue": {
                                        "type": "double"
                                    },
                                    "values": {
                                        "properties": {
                                            "value": {
                                                "type": "keyword"
                                            }
                                        }
                                    }
                                }
                            },
                            "keyList": {
                                "properties": {
                                    "value": {
                                        "type": "keyword"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

改变映射后我才意识到; 我搜索searchQuery.categoryProperties.key不是color。我有一个数组,如果其中一个键不是color它可以搜索,但不适合我。 我创建了一个keyList数组,并将searchQuery.categoryProperties.key的所有分组键放到keyList对象中。 现在我先搜索keyList。它给了我正确的答案。这解决了我的问题。

这是正确的REQUEST

{
    "query": {
        "bool": {
            "must": [{
                "nested": {
                    "query": {
                        "bool": {
                            "filter": [{
                                "bool": {
                                    "should": [{
                                        "bool": {
                                            "must_not": [{
                                                "term": {
                                                    "searchQuery.keyList.value": {
                                                        "value": "color"
                                                    }
                                                }
                                            }]
                                        }
                                    },
                                    {
                                        "bool": {
                                            "must": [{
                                                "term": {
                                                    "searchQuery.categoryProperties.key": {
                                                        "value": "color"
                                                    }
                                                }
                                            },
                                            {
                                                "term": {
                                                    "searchQuery.categoryProperties.values.value": {
                                                        "value": "Mavi"
                                                    }
                                                }
                                            }]
                                        }
                                    }]
                                }
                            }]
                        }
                    },
                    "path": "searchQuery"
                }
            }]
        }
    }
}