Elastic Search Newbie


(vinodbohra) #1

I am trying build a POC on how we can use ElasticSearch for our quick search.
The case is we have millions and millions of customer records and a CSR need to search across set of fields to fetch results.
These results will then pass through entitlements and visibility layer where right records will be returned back to GUI.

The query that I have is as below:

  var wildcard = "*"+(request.term+"").replace(/\s/g, "* *")+"*" ;
  var quey_St = "{'fields': ['last','first','fullname'],'query':'*"+request.term+"*','default_operator' : 'AND'}";
  var postData = {
    "query" :{ "custom_score" : {
            "query": {


                    "query_string" : { 'fields': ['last','first', 'middle', 'fullname', 'email'], "default_operator" : "AND", "query" : wildcard}
            },
            "script" : "_score * doc['userHit'].value"
    }
    },"size" : 100,


    "fields": ["partyId", "last", "first", "middle", "fullname", "cas", "eci", "ssn", "itin", "phone", "email", "category1", "category2","_id", "userHit"]
  };

This query returns me matching results which i display on UI.
I need to understand how can I build equivalent query to be used in java code to fetch the results which I can use it to make an sp call for further verification of data.
I created the query builder as below in my java class as below…

            String queryString = "{ \'fields\': [\'last\',\'first\'], \"query\" : \"SMITH\"}";
            SearchRequestBuilder builder = client.prepareSearch("pbws1");
            QueryStringQueryBuilder qb =  QueryBuilders.queryString(queryString).defaultOperator(Operator.AND)
                    .field("first").field("last")
                    .allowLeadingWildcard(false).useDisMax(true);
                    //builder.addSort("createdAt", SortOrder.DESC);
                    builder.setFrom(0).setSize(10);
                    builder.setQuery(qb);

It returns below error:

****** Creting SearchResponse Object**********
Caused by: org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to execute phase [query], total failure; shardFailures {[lCFrAfM4RSuLVnnirR4vMw][pbws1][0]: SearchParseException[[pbws1][0]: from[0],size[60]: Parse Failure [Failed to parse source [na]]]; nested: QueryParsingException[[pbws1] Failed to parse query [{ 'fields': ['last','first'], "query" : "SMITH"}]]; nested: ParseException[Cannot parse '{ 'fields': ['last','first'], "query" : "SMITH"}': Encountered " <RANGEEX_QUOTED> ""query" "" at line 1, column 30.
Was expecting:
"}" ...
]; nested: ParseException[Encountered " <RANGEEX_QUOTED> ""query" "" at line 1, column 30.
Was expecting:
"}" ...

]; }{[lCFrAfM4RSuLVnnirR4vMw][pbws1][3]: SearchParseException[[pbws1][3]: from[0],size[60]: Parse Failure [Failed to parse source [_na_]]]; nested: QueryParsingException[[pbws1] Failed to parse query [{ 'fields': ['last','first'], "query" : "SMITH"}]]; nested: ParseException[Cannot parse '{ 'fields': ['last','first'], "query" : "SMITH"}': Encountered " <RANGEEX_QUOTED> "\"query\" "" at line 1, column 30.

Was expecting:
"}" ...
]; nested: ParseException[Encountered " <RANGEEX_QUOTED> ""query" "" at line 1, column 30.
Was expecting:
"}" ...

]; }{[lCFrAfM4RSuLVnnirR4vMw][pbws1][1]: SearchParseException[[pbws1][1]: from[0],size[60]: Parse Failure [Failed to parse source [_na_]]]; nested: QueryParsingException[[pbws1] Failed to parse query [{ 'fields': ['last','first'], "query" : "SMITH"}]]; nested: ParseException[Cannot parse '{ 'fields': ['last','first'], "query" : "SMITH"}': Encountered " <RANGEEX_QUOTED> "\"query\" "" at line 1, column 30.

Was expecting:
"}" ...
]; nested: ParseException[Encountered " <RANGEEX_QUOTED> ""query" "" at line 1, column 30.
Was expecting:
"}" ...
]; }

    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:257)
    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.access$400(TransportSearchTypeAction.java:81)


    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$3.onFailure(TransportSearchTypeAction.java:207)
    at org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteQuery(SearchServiceTransportAction.java:137)


    at org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchQueryThenFetchAction.java:76)


    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:201)
    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.access$000(TransportSearchTypeAction.java:81)
    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:177)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)


    at java.lang.Thread.run(Thread.java:619)

Can you please assist.


(system) #2