Search code examples
javaelasticsearchsearchkibanaelasticsearch-6

Querying elastic search based on _source fields searching


Am searching records from elastic search by using _id and am able to fetch records from elastic search. But now i want to search based on _source (any fields from _source) using wildcards. Am not sure how to build my query for this. Is there any documentation on this ?

Please find my code below that, am able to query elastic search based on _id.

Am using elastic search 6.2.3 version.

public Product getProductById(String id){

    String[] includes = new String[]{id};
    String[] excludes = Strings.EMPTY_ARRAY;
    GetRequest getRequest = new GetRequest(INDEX, TYPE, SOURCE);
    getRequest.routing(id);

    GetResponse getResponse = null;
    try {
        getResponse = restHighLevelClient.get(getRequest);
    } catch (java.io.IOException e){
        e.getLocalizedMessage();
    }

    //GetResponse getResponse = null;

    // create the search request
    SearchRequest searchRequest = new SearchRequest(INDEX); 
    searchRequest.types(TYPE);

    // create the match query on the author field
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("_id", id); 
    searchSourceBuilder.query(matchQueryBuilder); 
    searchRequest.source(searchSourceBuilder);

    // send the request
    SearchResponse searchResponse = null;
    try {
         searchResponse = restHighLevelClient.search(searchRequest);
    } catch (IOException e) {
        e.getLocalizedMessage();
    }
    // read the response
    String productName = null;
    Product product = null;
    SearchHit[] searchHits = searchResponse.getHits().getHits();
    for (SearchHit hit : searchHits) {
        // get each hit as a Map
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        product=new Product();
        product.setName(sourceAsMap.get("name").toString());
    }

    Gson gson=new Gson();
    JSONObject productJSON = null;
    String prodStr=gson.toJson(product);
    try {
        productJSON=new JSONObject(prodStr);
    } catch (JSONException e) { 
        e.printStackTrace();
    }
    return product;
}

Please find the record available in elastic search. Here i want to search this record based on _source field. for example : search based on code

{  
   "took":50,
   "timed_out":false,
   "_shards":{  
      "total":5,
      "successful":5,
      "skipped":0,
      "failed":0
},
"hits":{  
  "total":1,
  "max_score":1.0,
  "hits":[  
     {  
        "_index":"my_index",
        "_type":"doc",
        "_id":"MUC8GmMBRU-f7c0A8LUY",
        "_score":1.0,
        "_source":{  
           "@version":"1",
           "vendor_id":1,
           "name":"prod7",
           "code":"abc1234",
           "catalog_id":343,
           "is_visible":1,
           "@timestamp":"2018-05-01T08:06:16.642Z"
        }
     }
  ]
}

Solution

  • Simply replace this line

    MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("_id", id); 
    

    with this one

    MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("code", "abc1234");