0

How to search data based on condition from elastic search index using RestHighLevelClient in spring boot . For below example .

case 1 : If i search with "company_name" (ex:"DEFG") . my output should be get all matched data "company_name/address3" (ex: DEFG/smp1 , DEFG/chtp2 , DEFG/gmd , DEFG/tste )

case 2 : If i search with "address3"(Ex: "smp1") .my output should be only "company_name/address3 (Ex :"DEFG/smp1).

"_index" : "es_52_companydetails_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "company_id" : "3",
          "company_name" : "DEFG",
          "companyaddress" : [
            {
              "address3" : "smp1",
              "main_phone1" : "1"
            },
            {
              "address3" : "chtp2",
              "main_phone1" : "2"
            },
            {
              "address3" : "gmd",
              "main_phone1" : "3"
            },
            {
              "address3" : "tste",
              "main_phone1" : "4"
            }
          ]
        }
Dorayya
  • 11
  • 1
  • 6

1 Answers1

0

Case 1:

{
  "query": {
    "match": {
      "company_name": "DEFG"
    }
  }
}

Case 1 Java:

RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http")));

        SearchRequest searchRequest = new SearchRequest("company");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchQuery("company_name", "DEFG"));
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse.toString());

Case 2: You can achived this using script_field. You need to pass same query to the if condition as well. so it will generate new field with only address which is matched with your query.

{
  "_source": {
    "excludes": "name"
  }, 
  "query": {
    "match": {
      "companyaddress.address3": "smp1"
    }
  },
  "script_fields": {
    "address": {
      "script": {
        "lang": "painless",
        "source": """
      List li = new ArrayList();
      if(params['_source']['companyaddress'] != null)
      {
        for(p in params['_source']['companyaddress'])
        {
          if( p.address3 == 'smp1')
            li.add(p);
        }
      }
      return li;
      """
      }
    }
  }
}

Case 2 Response: Please see the fields tag which have address which is matching to the query.

 {
        "_index" : "company",
        "_type" : "_doc",
        "_id" : "101",
        "_score" : 0.6548753,
        "_source" : {
          "companyaddress" : [
            {
              "address3" : "smp1",
              "main_phone1" : "1"
            },
            {
              "address3" : "chtp2",
              "main_phone1" : "2"
            },
            {
              "address3" : "gmd",
              "main_phone1" : "3"
            },
            {
              "address3" : "tste",
              "main_phone1" : "4"
            }
          ],
          "company_id" : "3",
          "company_name" : "DEFG"
        },
        "fields" : {
          "address" : [
            {
              "address3" : "smp1",
              "main_phone1" : "1"
            }
          ]
        }
      }

Case 2 Java:

RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http")));

        SearchRequest searchRequest = new SearchRequest("company");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchQuery("companyaddress.address3", "smp1"));

        Map<String, Object> param = new HashMap<String, Object>();

        String source = "List li = new ArrayList();if(params['_source']['companyaddress'] != null){for(p in params['_source']['companyaddress']){if( p.address3 == 'smp1')li.add(p);}}return li;";
        Script script = new Script(ScriptType.INLINE, "painless", source, param);

        sourceBuilder.scriptField("address", script);
        sourceBuilder.fetchSource(true);
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse.toString());
        client.close();
Sagar Patel
  • 4,993
  • 1
  • 8
  • 19
  • I want solution to my problem is Using RestHighLevelClient . So please provide piece of code for 2 cases . i am new to this elasticsearch concept .thanks advance – Dorayya Aug 26 '21 at 10:41
  • @Dorayya Please check now. I have added code for java. if this is help you then marked answer as solution and do upvote. – Sagar Patel Aug 26 '21 at 12:05
  • QueryBuilders.matchQuery("companyaddress.address3", "smp1") – Dorayya Aug 26 '21 at 12:17
  • @Dorayya is this not what you are looking for ? – Sagar Patel Aug 26 '21 at 12:20
  • QueryBuilders.matchQuery("companyaddress.address3", "smp1") Here 1st parameter in matchQuery is dynamic . means my search word can belongs to "company_name or companyaddress.address3 " . Actually at my end i have String[] ,its contain all fields name with nested , Ex : String [] columnsToBeSearched = {"company_id","company_name","companyaddress.address1"} – Dorayya Aug 26 '21 at 12:25
  • then you need to used multi match query. ```String[] columnsToBeSearched = { "company_id", "company_name", "companyaddress.address1" }; sourceBuilder.query(QueryBuilders.multiMatchQuery("smp1", columnsToBeSearched));``` – Sagar Patel Aug 26 '21 at 12:29
  • Initially i am using multiMatchQuery(). like below .but reach result getting empty. searchSourceBuilder.query(QueryBuilders.multiMatchQuery(queryString, multimatchFields).lenient(true)); – Dorayya Aug 26 '21 at 13:39
  • Would you like to automatically move this discussion to chat? Okay – Dorayya Aug 26 '21 at 13:42
  • Please help on this – Dorayya Aug 27 '21 at 09:33