能够使用以下Java代码从elasticsearch中获取与我的输入代码ABC-123
匹配的文档。但是现在,我想在这里再添加一个条件/过滤器,
我想获取所有匹配的代码ABC-123
,而Type
只是Reg
。
SearchRequest searchRequest = new SearchRequest(INDEX);
searchRequest.types(TYPE);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
QueryBuilder qb=QueryBuilders.matchQuery("code", code);
searchSourceBuilder.query(qb);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse1 = null;
try {
searchResponse1 = restHighLevelClient.search(searchRequest);
} catch (IOException e) {
e.getLocalizedMessage();
}
SearchHit[] searchHits1 = searchResponse1.getHits().getHits();
Replacement linkId = null;
Replacement replacement = null;
List<Replacement> linkIDList=new ArrayList<Replacement>();
for (SearchHit hit1 : searchHits1) {
linkId = new Replacement();
Map<String, Object> sourceAsMap1 = hit1.getSourceAsMap();
linkId.setLink_id(sourceAsMap1.get("code"));
linkIDList.add(linkId);
}
请在下面找到我的数据。
id link_id code Type
1001 2001 ABC-123 Reg
1002 2002 ABC-345 Reg
1003 2003 ABC-123 Act
1004 2004 ABC-123 Reg
如果尝试搜索
ABC-123
,则仅期望id link_id code Type
1001 2001 ABC-123 Reg
1004 2004 ABC-123 Reg
它,不应该考虑以下记录。因为它的类型是
Act
。id link_id code Type
1003 2003 ABC-123 Act
任何人都可以有任何想法,我如何在我的Java代码中添加
filter
。 ? 最佳答案
您需要将约束包装在bool/must/filter
查询中,如下所示:
QueryBuilder qb = QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("code", code))
.filter(QueryBuilders.termQuery("Type", "Reg"));
searchSourceBuilder.query(qb);
关于java - 如何在QueryBuilders.matchQuery()中添加过滤器以基于过滤器获取一些匹配的文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50904856/