如何使用Elasticsearch创建通配符查询?我尝试了下面的方法,但我认为它不起作用(我的意思是它没有过滤)。
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.wildcardQuery("message", "ANG*"));
我也试过了prefixQuery方法,但它也没有过滤任何结果。
boolQueryBuilder.must(QueryBuilders.prefixQuery("message", "ANG"));
修改
"_index": "log4j_2017",
"_type": "log4j",
"_id": "fd23123122",
"_score": null,
"_source": {
"date": "2017-03-10T19:04:50.049Z",
"contextStack": [],
"level": "INFO",
"marker": null,
"thrown": null,
"message": "ANGServlet 'spring': initialization completed in 2314 ms",
"millis": 1489151090049,
"contextMap": {},
"threadName": "http-apr-8080-exec-77"
}
答案 0 :(得分:1)
一个可能的原因是您对message
字段进行了分析,并且在这种情况下,令牌以小写形式编制索引,因此您需要像这样搜索:
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.wildcardQuery("message", "ang*"));
或
boolQueryBuilder.must(QueryBuilders.prefixQuery("message", "ang"));
答案 1 :(得分:0)
在Java的布尔查询中添加多个通配符
SearchRequest searchRequest = new SearchRequest(Constant.ELASTIC_ROOM_INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.should(QueryBuilders.wildcardQuery("roomNo", "*" + searchText + "*")).boost(2);
boolQueryBuilder.should(QueryBuilders.wildcardQuery("location", "*" + searchText + "*")).boost(1);
searchSourceBuilder.query(QueryBuilders.boolQuery().must(boolQueryBuilder));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] searchHits = searchResponse.getHits().getHits();
for (SearchHit hit : searchHits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
Long roomId = ((Integer) sourceAsMap.get("roomId")).longValue();
String roomNo = (String) sourceAsMap.get("roomNo");
String location = (String) sourceAsMap.get("location");
results.add(new SearchResultDTO(roomNo + ", " + location + ",Room", roomId));
}