【问题标题】:Search through all document fields (nested and root document) in one multi match query在一个多重匹配查询中搜索所有文档字段(嵌套和根文档)
【发布时间】: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


    【解决方案1】:

    您对展平的直觉是正确的,但您不需要将根属性复制到嵌套字段中。你可以做相反的事情——通过include_in_root mapping parameter

    当您像这样更新映射时:

    PUT inventory
    {
      "settings": {
          ... 
        }
      },
      "mappings": {
        "properties": {
          ...
          "items": {
            "type": "nested",
            "include_in_root": true,     <---
            "properties": {
              ...
            }
          }
        }
      }
    }
    

    然后索引一些示例文档(其中至少一个包括pants,因为您的原始问题不包含任何内容):

    POST inventory/_doc
    {"name":"shirt","description":"with stripes","items":[{"color":"red","size":"44"},{"color":"blue","size":"38"}]}
    
    POST inventory/_doc
    {"name":"shirt","description":"with stripes","items":[{"color":"green","size":"40"}]}
    
    POST inventory/_doc
    {"name":"shirt","description":"with dots","items":[{"color":"green","size":"38"},{"color":"blue","size":"38"}]}
    
    // this one *should* match
    POST inventory/_doc
    {"name":"pants","description":"with stripes","items":[{"color":"red","size":"44"},{"color":"blue","size":"39"}]}
    
    POST inventory/_doc
    {"name":"pants","description":"with stripes","items":[{"color":"red","size":"44"},{"color":"blue","size":"38"}]}
    

    然后您可以使用您的第二个查询并保持嵌套字段路径不变,因为它们现在在根目录中可用,尽管在相同的点路径下有些混乱:

    POST inventory/_search
    {
      "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"
        }
      }
    }
    

    并且只会返回一个完全匹配的文档:

    {
      "name":"pants",
      "description":"with stripes",
      "items":[
        {
          "color":"red",
          "size":"44"
        },
        {
          "color":"blue",
          "size":"38"
        }
      ]
    }
    

    【讨论】:

    • 非常感谢您的回答!据我对include_in_root 参数的理解,它将嵌套对象添加为平面字段。所以我认为它可能会将多个项目合并在一起,我们会松散哪种颜色属于哪种尺寸。但你说不是这样吗?我将在星期一尝试您的解决方案。来源:elastic.co/guide/en/elasticsearch/reference/current/…
    • 是的,它将被展平,这意味着如果您有color: red; size: 38color: blue; size: 42,系统仍会返回此文档 b/c 连接赌注。尺寸和颜色将丢失。所以它不会是完美的。坦率地说,如果没有适当的解析器,我认为 ES 无法处理这种自然的语言理解查询。 multi_match 查询是一种蛮力,它会向一堆字段抛出一堆术语。但是它无法知道哪个字段是哪个字段。换句话说,即使是 color: 38; size: blue 的无意义形式的文档也会因为 mult_match 而匹配。
    • 不幸的是,颜色和尺寸之间的联系对我们来说非常重要。所以include_in_root 不适合我们。但感谢您抽出宝贵时间!
    • 没有问题。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多