在一个多匹配查询中搜索所有文档字段(嵌套和根文档)

时间:2021-03-02 12:33:41

标签: elasticsearch elasticsearch-query elasticsearch-nested

让我们以这些基本文件为例:

{
  "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 个字段。因为搜索词需要搜索许多字段(可能不是全部),所以嵌套和根文档属性的任何类型的串联以使其“可搜索”似乎不切实际。

扁平化索引可能是一个实用的解决方案。我的意思是将所有根文档字段复制到嵌套文档并且只索引嵌套文档。但是在这个问题中,我想知道它是否也适用于嵌套对象而无需修改原始结构。

1 个答案:

答案 0 :(得分:0)

您关于展平的直觉是正确的,但您不需要将根属性复制到嵌套字段上。你可以做相反的事情——通过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"
    }
  ]
}
相关问题