Reputation: 5236
How can I create a wildcard query with Elasticsearch? I tried below method but I think its not working(I mean it doesn't filter).
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.wildcardQuery("message", "ANG*"));
I also tried prefixQuery method but Its also didnt filter any result.
boolQueryBuilder.must(QueryBuilders.prefixQuery("message", "ANG"));
EDIT:
"_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"
}
Upvotes: 6
Views: 12280
Reputation: 449
adding multiple wildcard in boolean query in 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));
}
Upvotes: 0
Reputation: 217304
One possible reason is that you message
field is analyzed, and it that case the tokens are indexed in lowercase, so you need to search like this:
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.wildcardQuery("message", "ang*"));
or
boolQueryBuilder.must(QueryBuilders.prefixQuery("message", "ang"));
Upvotes: 7