在Java中,如何根据ID查询elasticsearch中的嵌套字段?

时间:2017-09-13 03:20:04

标签: java elasticsearch

ScoreMode mode=ScoreMode.Total;
        SearchRequestBuilder searchRequestBuilder = 
                    client.prepareSearch() 
                          .setIndices("commons")
                          .setTypes("webpage")
                          //.fields("id")
                          .setSize(10000)
                          .setQuery(QueryBuilders
                                  .nestedQuery("imgSource", 
                                          QueryBuilders.boolQuery()
                                  .must(
                                          QueryBuilders.matchQuery(
                                                  "id","48ca52e0d733c5093c08aa4df0b073f7")
                                          ),mode));

这是我的代码,但结果为空,所以我不知道我是否犯了错误......

1 个答案:

答案 0 :(得分:2)

根据java api for nested queries,您的代码段应如下所示:

  

假设我想在嵌套文档中查询名称字段,则代码段将为:

ScoreMode mode=ScoreMode.Total;
    SearchRequestBuilder searchRequestBuilder = 
                client.prepareSearch() 
                      .setIndices(INDEX_NAME)
                      .setTypes(TYPE_NAME)
                      .setQuery(QueryBuilders
                              .nestedQuery("child", 
                                      QueryBuilders.boolQuery()
                              .must(
                                      QueryBuilders.matchQuery(
                                              "child.name","CHILD_NAME")
                                      ),mode));

因此,匹配查询不正确,应为 imgSource.id