简体   繁体   English

ElasticSearch:在搜索结果中包含内部对象

[英]ElasticSearch: Include inner object in search result

I've a elastic search index containing elements taht have nested subelements as properties, eg. 我有一个包含元素的弹性搜索索引,该元素具有嵌套的子元素作为属性,例如。

{
    _index: tests,
    _type: test,
    _id: 11021,
    _version: 1,
    _score: 1,
    _source: {
        id: 11021,
        name: "demotest",
        responsiblePerson: {
            userId: "221",
            userName: "Walter",
            userSurName: "White",
            userEmail: "Walter.White@lospollos.com"
        },      
       listOfSubItems: [{"name": "location"},{"name":"sample"},{"name":"experiment"}]
    }
}

Now, I'd like to include some fields of that explicitly in the result of a query (eg to exclude the listOfSubitems from the result to reduce it's size; that's also the reason why i don't want to fall back onto the _source attribute). 现在,我想在查询结果中明确包含该字段的某些字段(例如,从结果中排除listOfSubitems以减小其大小;这也是为什么我不想回到_source属性的原因)。

The code for the java API would be: Java API的代码为:

 SearchResponse responseTests = client.prepareSearch("tests")
                    .addField("id")
                    .addField("name")
                    .addField("responsiblePerson") 
                    .setQuery(QueryBuilders.matchQuery("id", testId))
                    .execute()
                    .actionGet();

SearchHits testHits = responseTests.getHits(); SearchHits testHits = responseTests.getHits();

Unfortunately, this query doesn't work for some reason as testHits.getTotalHits() shows a number but testHits.getHits().length is 0 :-( 不幸的是,该查询由于某种原因而无法正常工作,因为testHits.getTotalHits()显示了一个数字,但testHits.getHits()。length为0 :-(

Does someone have a hint for me how i can get this to work? 有人对我有提示我如何使它工作吗?

"The reason why you can't get the stored field values for a nested object is because it is stored in a separate Lucene document. “无法获取嵌套对象的存储字段值的原因是因为它存储在单独的Lucene文档中。

If nested is enabled in the mapping a single ES document is stored as separate Lucene documents. 如果在映射中启用了嵌套,则单个ES文档将存储为单独的Lucene文档。 Each nested object will be a single Lucene document. 每个嵌套对象将是单个Lucene文档。 Also the main / root document will be a separate Lucene document. 主/根文档也将是单独的Lucene文档。 ES will always translates matches back to the root Lucene document. ES始终会将匹配项转换回Lucene根文档。 The _source is always associated with the root Lucene document. _source始终与根Lucene文档相关联。 When fields are being fetched the translation to Lucene root document already has taken place, so accessing the nested stored fields isn't possible, but accessing the values from the _source will work. 提取字段时,已经完成了对Lucene根文档的转换,因此无法访问嵌套的存储字段,但是可以从_source访问值。

I don't think this is a bug, but rather an limitation of how ES currently works with nested docs. 我不认为这是一个错误,而是对ES当前与嵌套文档的工作方式的限制。 Once #3022 has been implemented, accessing the stored fields of nested Lucene docs is possible." 一旦实现了#3022,就可以访问嵌套的Lucene文档的存储字段。”

you can find it here https://github.com/elasticsearch/elasticsearch/issues/5245 您可以在这里找到它https://github.com/elasticsearch/elasticsearch/issues/5245

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM