ElasticSearch:包括_all中的嵌套字段?

时间:2017-02-23 08:31:12

标签: elasticsearch nested

我的文件看起来像这样:

{"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,但这会不必要地炸毁文档,而且看起来很难看。参考列表可能非常大。我还可以解析自己的映射并在那里找到所有href,以便我可以在查询中明确列出所有href字段,但随着数据模型的增长,最终不再按比例缩放

帮助?

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

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

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

1 个答案:

答案 0 :(得分:0)

使用提供的映射:

JSONArray jArray = new JSONArray();

        for (int i = 0; i < docList.size(); i++) {
            JSONObject json = new JSONObject(hoi2.get(i));
            jArray.put(json);
        }

当我索引这些文件时:

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

我能够通过以下方式正确搜索href字段:

$ 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"
  }
}'

因此,无需在$ curl -XPOST 'localhost:9200/datasets/data/_search?pretty=true' -d '{ "query": { "query_string": { "query": "thisid", "fields": ["*.href"] } } }' 字段中对其进行索引。

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

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

相关问题