# Search java api not working v6.2.1

**URL:** <https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147>\
**Category:** Elasticsearch\
**Created:** [June 24, 2018, 2:30pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147 "2018-06-24T14:30:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali\_imran](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@ali\_imran](https://discuss.elastic.co/u/ali_imran)\
**Post date:** [June 24, 2018, 2:30pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/1 "2018-06-24T14:30:41Z")

</div>

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"  
}  
}**

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 24, 2018, 3:32pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/2 "2018-06-24T15:32:43Z")

</div>

What gives the following in Kibana dev console?

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

---

<div class="post-metadata">

**Author:** ![ali\_imran](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@ali\_imran](https://discuss.elastic.co/u/ali_imran)\
**Post date:** [June 25, 2018, 11:35am UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/3 "2018-06-25T11:35:17Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/query-dsl-query-string-query.html)

**It is not working in java**

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 25, 2018, 12:42pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/4 "2018-06-25T12:42:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ali\_imran](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@ali\_imran](https://discuss.elastic.co/u/ali_imran)\
**Post date:** [June 25, 2018, 5:45pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/5 "2018-06-25T17:45:22Z")

</div>

The output of Kibana console is:

 ![Capture](https://us1.discourse-cdn.com/elastic/original/3X/c/a/cacb06a6259ecf5eda20d3a969f05e0aaeea090d.PNG)

**What is wrong with query?**

The result of "match\_all" in Kibana console is:

 ![Capture2](https://us1.discourse-cdn.com/elastic/original/3X/9/9/99ab3babc89c206739073e68a729138d98e9962e.PNG)

Any help will be appreciated

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 25, 2018, 5:58pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/6 "2018-06-25T17:58:46Z")

</div>

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`:

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

```

---

<div class="post-metadata">

**Author:** ![ali\_imran](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@ali\_imran](https://discuss.elastic.co/u/ali_imran)\
**Post date:** [June 26, 2018, 4:25pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/7 "2018-06-26T16:25:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 26, 2018, 5:44pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/8 "2018-06-26T17:44:30Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). 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.

---

<div class="post-metadata">

**Author:** ![ali\_imran](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@ali\_imran](https://discuss.elastic.co/u/ali_imran)\
**Post date:** [June 26, 2018, 7:06pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/9 "2018-06-26T19:06:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 26, 2018, 7:18pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/10 "2018-06-26T19:18:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 24, 2018, 7:18pm UTC](https://discuss.elastic.co/t/search-java-api-not-working-v6-2-1/137147/11 "2018-07-24T19:18:22Z")

</div>

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