Elasticsearch与嵌套数组或什么是嵌套数组的路径?

时间:2016-05-31 14:52:48

标签: java elasticsearch nested

给出以下索引结果:

curl 'localhost:9200/index123/type123/_search?q=*&pretty'
...
            "_source": {
                "objectId": "objectId123",
                "list": [{
                    "name": "SomeName123",
                    "value": "SomeValue123",
                    "type": "SomeType123"},
                    {"name": "SomeOtherName123", ...}],
                "someOther": {
                    "i": "i",
                    "value": 3
                }
            }
...

现在,我想进行搜索,并获得匹配两个字段的所有条目,例如list.value = SomeValue123&& list.type = SomeType123。问题是,结果可能很大,所以滚动应该是可能的。

到目前为止我所拥有的:

SearchResponse scrollResp = elasticSearchClient
                        .prepareSearch("index123")
                        .setTypes("type123")
                        .setScroll(new TimeValue(60000))
                    .setQuery(
                            QueryBuilders.nestedQuery(
                                    "list",
                                    QueryBuilders
                                            .boolQuery()
                                            .must(QueryBuilders.matchQuery("value", "SomeValue123"))
                                            .must(QueryBuilders.matchQuery("type", "SomeType123")))

                    )
                    .setSize(100)
                    .execute()
                    .actionGet(); 

            SomeQueue<SomeBean> resultQ= new SomeQueue<SomeBean>();
            // Scroll until no hits are returned
            while (true) {

                resultQ.offer(getObjectOutOfHits(scrollResp.getHits().getHits()));

                scrollResp = elasticSearchClient
                        .prepareSearchScroll(scrollResp.getScrollId())
                        .setScroll(new TimeValue(60000))
                        .execute()
                        .actionGet();
                // Break condition: No hits are returned
                if (scrollResp.getHits().getHits().length == 0) {
                    break;
                }
            }

但我得到的只是:

Caused by: org.elasticsearch.index.query.QueryParsingException: [nested] failed to find nested object under path [list]

如何获得与弹性搜索java客户端匹配此字段组合的所有项目?

如果有人知道curl命令,那就没问题,所以我可以使用模板!

1 个答案:

答案 0 :(得分:1)

为什么要将查询嵌套到空查询中? 试试这个:

    .setQuery(
        QueryBuilders.boolQuery()
            .must(QueryBuilders.matchQuery("value", "SomeValue123"))
            .must(QueryBuilders.matchQuery("type", "SomeType123"))
        )