【问题标题】:Elasticsearch with nested objects query带有嵌套对象查询的 Elasticsearch
【发布时间】:2018-12-23 15:04:34
【问题描述】:

我有一个带有嵌套映射的索引。 我想执行一个查询,该查询将返回以下内容:给我搜索词中的每个单词出现在一个或多个嵌套文档中的所有文档。

这是索引:

properties: {
      column_values_index_as_objects: {
        type: "nested",
        properties: {
          value: {
            ignore_above: 256,
            type: 'keyword',
            fields: {
              word_middle: {
                analyzer: "searchkick_word_middle_index",
                type: "text"
              },
              analyzed: {
                term_vector: "with_positions_offsets",
                type: "text"
              }
            }
          }
        }
      }
    }

这是我尝试的最新查询:

nested: {
       path: "column_values_index_as_objects",
       query: {
          bool: {
             must: [
                {
                   match: {
                       "column_values_index_as_objects.value.analyzed": {
                       query: search_term,
                       boost: 10 * boost_factor,
                       operator: "or",
                       analyzer: "searchkick_search"
                      }
                   }
                }

例如,如果我搜索单词“食物和水”,我希望每个单词至少出现在嵌套文档中。 即使只存在一个单词,当前搜索也会返回文档

感谢您的帮助!

更新: 正如 Cristoph 建议的那样,该解决方案有效。现在我有以下问题。

这是我的索引:

  properties: {
      name: {
        type: "text"
      },
      column_values_index_as_objects: {
        type: "nested",
        properties: {
          value: {
            ignore_above: 256,
            type: 'keyword',
            fields: {
              word_middle: {
                analyzer: "searchkick_word_middle_index",
                type: "text"
              },
              analyzed: {
                term_vector: "with_positions_offsets",
                type: "text"
              }
            }
          }
        }
      }
    }

我想要执行的查询是如果我搜索“我的名字是家伙”,并且会给出所有找到所有单词的文档 - 可能在嵌套文档中,也可能在名称字段中。 例如,我可以有一个文档,其中 name 字段的值为“guy”,嵌套文档中的其他单词

【问题讨论】:

  • 如果你想要 AND 的话,那么操作符的值应该是 and 而不是 or
  • 谢谢!如果我使用“AND”,这意味着只有当整个句子都在值中时它才会给我结果。这不是我想要的。我希望每个单词都会出现在至少一个子文档中

标签: elasticsearch nested


【解决方案1】:

为了做到这一点,我通常会拆分条款并生成这样的请求(foo:bar 是其他字段的其他条件):

{
  "bool": {
    "must": [
      {
        "nested": {
          "path": "column_values_index_as_objects",
          "query": {
            "match": {
              "column_values_index_as_objects.value.analyzed": {
                "query": "food",
                "boost": "10 * boost_factor",
                "analyzer": "searchkick_search"
              }
            }
          }
        }
      },
      {
        "nested": {
          "path": "column_values_index_as_objects",
          "query": {
            "match": {
              "column_values_index_as_objects.value.analyzed": {
                "query": "and",
                "boost": "10 * boost_factor",
                "analyzer": "searchkick_search"
              }
            }
          }
        }
      },
      {
        "nested": {
          "path": "column_values_index_as_objects",
          "query": {
            "match": {
              "column_values_index_as_objects.value.analyzed": {
                "query": "water",
                "boost": "10 * boost_factor",
                "analyzer": "searchkick_search"
              }
            }
          }
        }
      },
      {
        "query": {
          "term": {
            "foo": "bar"
          }
        }
      }
    ]
  }
}

【讨论】:

  • 现在我还有一个问题。如果我想这样做并添加另一个常规字段?没有嵌套,那将如何工作?意思是如果我寻找“一二三” - 如果我在嵌套字段中有“一”和“二”,但在另一个字段中有“三” - 我想获取此文档
  • 谁有线索...?
  • 只需将您的查询添加到“必须”数组中。我已经更新了我的答案。
  • 嗨@Christophe Quintard 非常感谢!我不确定它是否完全回答了我的问题,我已经更新了我的问题,你能看看吗?感谢您的帮助!
  • 这里有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-31
  • 2015-01-13
  • 2021-02-03
  • 1970-01-01
相关资源
最近更新 更多