通过匹配两个字段从弹性搜索中检索文档

时间:2014-12-29 14:34:12

标签: elasticsearch

我的数据存储在弹性搜索中,如下所示

{  
   "identifier":{  
      "source":"source 1",
      "id":"22081070"
   },
   "title":"Book 1",
   "published":2011,
   "types":[  
      "type1",
      "type2, 
      "type3"
   ]
}

有没有办法检索具有特定" identifier.id"的文档?和" identifier.source"参数?例如,我正在使用其id作为输入检索上述文档,其中包含以下内容:

QueryBuilder queryBuilder = QueryBuilders.matchQuery("identifier.id", "22081070");
SearchResponse searchResponse = client.prepareSearch("test-index")
                .setTypes("type").setQuery(queryBuilder).execute().actionGet();

但我知道如何添加" identifier.source"作为匹配参数。

1 个答案:

答案 0 :(得分:2)

试试这个:

BoolQueryBuilder boolQuery = new BoolQueryBuilder();
QueryBuilder queryBuilder1 = QueryBuilders.matchQuery("identifier.id", "22081070");
QueryBuilder queryBuilder2 = QueryBuilders.matchQuery("identifier.source", "source 1");

boolQuery.must(queryBuilder1).must(queryBuilder2);

SearchResponse searchResponse = client.prepareSearch("test-index")
                .setTypes("type").setQuery(boolQuery).execute().actionGet();