6

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"
         }
4
  • Can you share your mapping please? Commented Mar 10, 2017 at 13:07
  • I am using log4j and I think it doesn't have any mapping.(dynamic mapping I think) Commented Mar 10, 2017 at 13:12
  • what do you get when running curl -XGET localhost:9200/your_index ? Commented Mar 10, 2017 at 13:15
  • @Val I don't have any access to es server. I can only send HTTP request. Commented Mar 10, 2017 at 13:25

2 Answers 2

7

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"));
Sign up to request clarification or add additional context in comments.

3 Comments

Yes It worked when I use lowercase.I think I need to create a new mapping. Thanks for your help.
How can we build wildcardQuery to match multiple terms in java?
@RameshPapaganti Feel free to create a new question with your exact needs
0

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));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.