【发布时间】:2021-06-01 00:01:35
【问题描述】:
我们以这些基本文档为例:
{
"name": "pants",
"description": "with stripes",
"items": [
{
"color": "red",
"size": "44"
},
{
"color": "blue",
"size": "38"
}
]
}
{
"name": "shirt",
"description": "with stripes",
"items": [
{
"color": "green",
"size": "40"
}
]
}
{
"name": "pants",
"description": "with dots",
"items": [
{
"color": "green",
"size": "38"
},
{
"color": "blue",
"size": "38"
}
]
}
我需要找到带有pants stripes blue 38 之类的搜索词的第一个文档。所有术语都应该用 AND 连接,因为我对带有圆点或其他尺寸和颜色组合的裤子不感兴趣。
我的映射如下所示:
{
"settings": {
"index.queries.cache.enabled": true,
"index.number_of_shards": 1,
"index.number_of_replicas": 2,
"analysis": {
"filter": {
"german_stop": {
"type": "stop",
"stopwords": "_german_"
},
"german_stemmer": {
"type": "stemmer",
"language": "light_german"
},
"synonym": {
"type": "synonym_graph",
"synonyms_path": "dictionaries/de/synonyms.txt",
"updateable" : true
}
},
"analyzer": {
"index_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"german_stop",
"german_normalization",
"german_stemmer"
]
},
"search_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"synonym",
"german_stop",
"german_normalization",
"german_stemmer"
]
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "index_analyzer",
"search_analyzer": "search_analyzer"
},
"description": {
"type": "text",
"analyzer": "index_analyzer",
"search_analyzer": "search_analyzer"
},
"items": {
"type": "nested",
"properties": {
"color": {
"type": "text",
"analyzer": "index_analyzer",
"search_analyzer": "search_analyzer"
},
"size": {
"type": "text",
"analyzer": "index_analyzer",
"search_analyzer": "search_analyzer"
}
}
}
}
}
}
请忽略我使用德语停用词等的事实。我将上面的示例文件保留为英文,以便每个人都能理解,但没有调整映射,因为原始示例是德文的。
理想情况下,我希望我的查询如下所示:
{
"query": {
"nested": {
"path": "items",
"query": {
"multi_match": {
"query": "pants stripes blue 38",
"fields": [
"name",
"description",
"items.color",
"items.size"
],
"type": "cross_fields",
"operator": "and",
"auto_generate_synonyms_phrase_query": "false",
"fuzzy_transpositions": "false"
}
}
}
}
}
来自 Kibana 的 Search Profiler 显示查询将像这样执行:
ToParentBlockJoinQuery (
+(
+(items.color:pant | items.size:pant | name:pant | description:pant)
+(items.color:strip | items.size:strip | name:strip | description:strip)
+(items.color:blu | items.size:blu | name:blu | description:blu)
+(items.color:38 | items.size:38 | name:38 | description:38)
) #_type:__items)
这看起来正是我需要的 AND 和 OR 逻辑。用每个术语搜索每个属性,并用 AND 连接这些结果。因此,每个搜索词都需要在其中一个字段中,但在哪个字段中找到它并不重要。
但是这个查询似乎只在嵌套文档中搜索。事实上,似乎每个查询只能搜索嵌套对象或根文档。不是同时两个。如果我删除嵌套部分,搜索探查器会显示差异:
{
"query": {
"multi_match": {
"query": "pants stripes blue 38",
"fields": [
"name",
"description",
"items.color",
"items.size"
],
"type": "cross_fields",
"operator": "and",
"auto_generate_synonyms_phrase_query": "false",
"fuzzy_transpositions": "false"
}
}
}
结果:
+(
+(items.color:pant | items.size:pant | name:pant | description:pant)
+(items.color:strip | items.size:strip | name:strip | description:strip)
+(items.color:blu | items.size:blu | name:blu | description:blu)
+(items.color:38 | items.size:38 | name:38 | description:38)
) #DocValuesFieldExistsQuery [field=_primary_term]
两个查询都返回零结果。
所以我的问题是,是否有一种方法可以使上述查询正常工作,并能够在一个多匹配查询中逐词地真正搜索所有定义的字段(嵌套和根文档)。
我想避免对搜索词进行任何预处理,以便根据它们位于嵌套文档或根文档中进行拆分,因为这有其自身的一系列挑战。但我知道这是解决我问题的方法。
编辑 原始文件有更多的属性。根文档可能有多达 250 个字段,每个嵌套文档可能会添加另外 20-30 个字段。因为搜索词需要搜索很多字段(可能不是全部),所以任何形式的嵌套和根文档属性的串联以使它们“可搜索”似乎不切实际。
扁平化索引可能是一种实用的解决方案。我的意思是将所有根文档字段复制到嵌套文档并仅索引嵌套文档。但在这个问题中,我想知道它是否也适用于嵌套对象而不修改原始结构。
【问题讨论】:
标签: elasticsearch elasticsearch-query elasticsearch-nested