Search java api not working v6.2.1


(Ali Imran) #1

I am using elasticsearch (6.2.1) in my project. The indexing queries are running fine. Whereas the searching query is not working. It works with curl and give search results. But when using java search api, it always give 0 hits including for "match_all" query.

The Java code is given below:

RestClientBuilder builder = RestClient.builder(
new HttpHost("localhost", 9200, "http"));
        builder.setMaxRetryTimeoutMillis(60*1000);
        RestHighLevelClient client = new RestHighLevelClient(builder);

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder .query(QueryBuilders.queryStringQuery(queryString));
sourceBuilder .timeout(new TimeValue(60, TimeUnit.SECONDS));**

SearchRequest searchRequest = new SearchRequest(); 
searchRequest.source(sourceBuilder );
        
SearchResponse searchResponse = client.search(searchRequest);
        
SearchHits hits = searchResponse.getHits();

The query that generated is:

{"timeout":"60s","query":{"query_string":{"query":"(name:John)","fields":[],"type":"best_fields","default_operator":"or","max_determinized_states":10000,"enable_position_increments":true,"fuzziness":"AUTO","fuzzy_prefix_length":0,"fuzzy_max_expansions":50,"phrase_slop":0,"escape":false,"auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1.0}}}

Doc that I expect to match:

"_index": "registration_forms_index",
"_type": "doc",
"_id": "9J_k72MBX-yYREG15tOQ",
"_score": 1,
"_source": {
"{"reg_no":31,"name":"John","datafilePath":"C:\repo_master\registration_forms\registration_forms_folder\json.txt"}": "JSON"
}
}


(David Pilato) #2

What gives the following in Kibana dev console?

GET _search
{"query":{"query_string":{"query":"(name:John)"}}}

(Ali Imran) #3

The output of system.print of "sourceBuilder". I tested the query on "elasticsearch_head" extension, it is working. "queryString" is string "(name:John)". The format to generate "multi-search" query is :
GET /_search
{
"query": {
"query_string": {
"query": "(content:this OR name:this) AND (content:that OR name:that)"
}
}
}

For multi-search, the link below is used:
https://www.elastic.co/guide/en/elasticsearch/reference/6.3/query-dsl-query-string-query.html

It is not working in java


(David Pilato) #4

Can you share the output of the command I asked to run? Please don't run it on head or use POST verb instead of GET. Better, run it in Kibana.


(Ali Imran) #5

The output of Kibana console is:

What is wrong with query?

The result of "match_all" in Kibana console is:

Any help will be appreciated


(David Pilato) #6

Please don't post images of text as they are hardly readable and not searchable.

Instead paste the text and format it with </> icon. Check the preview window.

Try now with name:John:

GET /_search
{
  "query": {
    "query_string": {
      "query": "name:John"
    }
  }
}

(Ali Imran) #7

There is still no hit. The output in dev console is:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

Is the query for "query_string" is right?


(David Pilato) #8

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.


(Ali Imran) #9

The format of my data is :

{ "reg_no":31, "name":"John", "datafilePath":"C:\repo_master\registration_forms\registration_forms_folder\json.txt" }

Indexing of the data is done by code below:

RestClientBuilder builder = RestClient.builder(
				                    new HttpHost("localhost", 9200, "http"));
builder.setMaxRetryTimeoutMillis(60*1000);
RestHighLevelClient client = new RestHighLevelClient(builder);

IndexRequest request = new IndexRequest(index, "doc");
request.source(metaDataJson, XContentType.JSON);

IndexResponse response = client.index(request);

client.close();

metaDataJson is json file in the above.

The file indexed successfully. The output of "match_all" query is:

{
  "took": 29,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "registration_forms_index",
        "_type": "doc",
        "_id": "9J_k72MBX-yYREG15tOQ",
        "_score": 1,
        "_source": {
          """{"reg_no":31,"name":"John","datafilePath":"C:\\repo_master\\registration_forms\\registration_forms_folder\\json.txt"}""": "JSON"
        }
      },
      {
        "_index": "registration_forms_index",
        "_type": "doc",
        "_id": "9Z_o72MBX-yYREG1_tOb",
        "_score": 1,
        "_source": {
          """{"reg_no":31,"name":"John","datafilePath":"C:\\repo_master\\registration_forms\\registration_forms_folder\\json.txt"}""": "JSON"
        }
      },
      {
        "_index": "registration_forms_index",
        "_type": "doc",
        "_id": "HQ0PLWQBBx7EgLHX5sV8",
        "_score": 1,
        "_source": {
          """{"reg_no":31,"name":"John","datafilePath":"C:\\repo_master\\registration_forms\\registration_forms_folder\\json.txt"}""": "JSON"
        }
      }
    ]
  }
}

The code for searching in indexed files is given in the first post.


(David Pilato) #10

Please provide something I can copy and paste in Kibana as per the example I gave.


(system) #11

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.