【问题标题】:ElasticSearch: including nested fields in _all?ElasticSearch:包括_all中的嵌套字段?
【发布时间】:2017-07-13 15:03:33
【问题描述】:

我的文档如下所示:

{"foo" : "blah blah blah",
 "bar" : "bla bla bla",
 "baz" : [{"href" : "someid"}, {"href" : "otherid"}, ...],
 ... }

我想进行搜索,以查找 id 出现在 href 子文档之一中的所有文档。如果我在映射中将baz.href设置为不分析,我可以用词条查询搜索baz.href

但是,我真正想要的是能够搜索此 ID,无论它出现在哪里。它可能位于baz.hrefquux.hrefwhatever.href。在_all 中搜索是完全可以接受的。

但是,我就是无法完成这项工作。我从来没有得到任何结果,除非我完全搜索 baz.href

我尝试在映射中将include_in_all 设置为baz,但无济于事。我尝试在baz.href 上设置它,但也没有用。

我知道我可以在顶层的单独 all_hrefs 字段中复制 ID,但这会不必要地炸毁文档,而且看起来很难看。参考列表可能非常大。我还可以解析我自己的映射并在那里找到所有hrefs,这样我就可以在查询中明确列出所有href 字段,但随着数据模型的增长,最终将不再扩展。

帮助?

更新:datasets 字段的映射(以及包含hrefs 的所有其他字段如下所示):

"datasets" : {
   "properties" : {
      "href" : {
         "include_in_all" : true,
         "index" : "not_analyzed",
         "type" : "string"
      }
   },
   "type" : "nested"
},

我试过删除nested,删除include_in_all,但没有区别。当我有nested 时,我可以使用嵌套查询,但path 必须设置为datasets,因为* 失败,因为并非所有字段都包含嵌套对象。

【问题讨论】:

    标签: elasticsearch nested


    【解决方案1】:

    使用提供的映射:

    $ curl -XPOST 'localhost:9200/datasets/data?pretty=true' -d '
    {
      "datasets" : {
        "properties" : {
          "href" : {
            "include_in_all" : false,
            "index" : "not_analyzed",
            "type" : "string"
          }
        },
        "type" : "nested"
      }
    }'
    

    当我索引这些文档时:

    $ curl -XPOST 'localhost:9200/datasets/data' -d '
    {
      "foo": "blah blah blah",
      "bar": "bla bla bla",
      "baz": [
        {
          "href": "someid"
        },
        {
          "href": "otherid"
        }
      ],
      "quux": {
        "href": "thisid"
      },
      "whatever": {
        "href": "thatid"
      }
    }'
    $ curl -XPOST 'localhost:9200/datasets/data' -d '
    {
      "foo": "argh argh argh",
      "bar": "arg arg arg",
      "baz": [
        {
          "href": "funkyid"
        },
        {
          "href": "thisid"
        }
      ],
      "quux": {
        "href": "hipsterid"
      },
      "whatever": {
        "href": "coolid"
      }
    }'
    

    我能够正确搜索 href 字段:

    $ curl -XPOST 'localhost:9200/datasets/data/_search?pretty=true' -d '{
      "query": {
        "query_string": {
          "query": "thisid",
          "fields": ["*.href"]
        }
      }
    }'
    

    因此,无需在_all 字段中对它们进行索引。

    我的测试是在 Elasticsearch 5.2.1 上完成的。

    仅供参考,这是我找到问题解决方案的地方: Searching term in subdocuments with elasticsearch

    【讨论】:

    • 不幸的是,这对我不起作用。不同之处在于,在我的情况下,href 子字段具有index: not_analyzed,这是避免来自类似 ID 的误报所必需的。我在术语查询中尝试了*.href,但这不起作用。我还尝试在query_string 查询中设置analyzer: keyword,但这也不起作用。不过,非常感谢您的尝试!
    • 您能否转储您用于该索引的映射样本?这将有助于重现问题并进一步帮助解决问题。
    • 我用 5.1 再次尝试了你的建议,我确实得到了搜索结果,但我得到的太多了。我想按 ID 搜索,但搜索会找到相似但不相同的值。
    猜你喜欢
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 2020-04-16
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2023-04-03
    相关资源
    最近更新 更多