gpt4 book ai didi

java - 如何在 QueryBuilders.matchQuery() 中添加过滤器以根据过滤器获取一些匹配文档

转载 作者:行者123 更新时间:2023-12-02 11:06:43 25 4
gpt4 key购买 nike

我能够使用下面的java代码从elasticsearch中获取与我的输入代码ABC-123匹配的文档。但现在,我想在这里再添加一个条件/过滤器,我想获取所有 Type 仅为 Reg 的匹配代码 ABC-123

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com