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

如何解决在一个多匹配查询中搜索所有文档字段嵌套和根文档

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

{
  "name": "pants","description": "with stripes","items": [
    {
      "color": "red","size": "44"
    },{
      "color": "blue","size": "38"
    }
  ]
}
{
  "name": "shirt","items": [
    {
      "color": "green","size": "40"
    }
  ]
}
{
  "name": "pants","description": "with dots","size": "38"
    },"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","synonym","german_stemmer"
          ]
        }
      }
    }
  },"mappings": {
    "properties": {
      "name": {
        "type": "text","analyzer": "index_analyzer","search_analyzer": "search_analyzer"
      },"description": {
        "type": "text","items": {
        "type": "nested","properties": {
          "color": {
            "type": "text","search_analyzer": "search_analyzer"
          },"size": {
            "type": "text","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","items.size"
      ],"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 个字段。因为搜索词需要搜索许多字段(可能不是全部),所以嵌套和根文档属性的任何类型的串联以使其“可搜索”似乎不切实际。

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

解决方法

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

POST inventory/_doc
{"name":"shirt","description":"with dots","size":"38"},"size":"38"}]}

// this one *should* match
POST inventory/_doc
{"name":"pants","size":"39"}]}

POST inventory/_doc
{"name":"pants","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","items":[
    {
      "color":"red","size":"44"
    },{
      "color":"blue","size":"38"
    }
  ]
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?