【问题标题】:ElasticSearch: Keyword Query Does not WorkElasticSearch:关键字查询不起作用
【发布时间】:2017-09-25 11:52:58
【问题描述】:

我的关键字查询有问题。

我想按键和值过滤 categoryProperties。

key 为“color”,value 包含“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"
                }
            }]
        }
    }
}

这是我的 回复

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

还有我的文档的映射: 映射

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

【问题讨论】:

    标签: elasticsearch keyword-search


    【解决方案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 的所有分组键放入keyListobject。 现在我正在搜索keyListfirst。它给了我正确的回应。这解决了我的问题。

    这是正确的请求

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

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 2017-07-08
      • 2018-04-10
      • 2015-01-05
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多