# How to perform a search query on an indexed document using Java

**URL:** <https://discuss.elastic.co/t/how-to-perform-a-search-query-on-an-indexed-document-using-java/304259>\
**Category:** Elasticsearch\
**Created:** [May 9, 2022, 1:53pm UTC](https://discuss.elastic.co/t/how-to-perform-a-search-query-on-an-indexed-document-using-java/304259 "2022-05-09T13:53:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 9, 2022, 1:53pm UTC](https://discuss.elastic.co/t/how-to-perform-a-search-query-on-an-indexed-document-using-java/304259/1 "2022-05-09T13:53:51Z")

</div>

Hello,  
I'm using Elasticsearch 17.12.0 and Java 1.8.  
I am able to perform a search operation on an indexed document via Kibana, I want to know how I can perform this task using my Java Springboot app which is already configured with Elasticsearch and connected to my node. I am able to index a document using my Java Springboot app, but this current document on which I want to perform search operation was imported from Kibana from [https://www.kaggle.com/datasets/rmisra/news-category-dataset](https://www.kaggle.com/datasets/rmisra/news-category-dataset). this website.

I tried looking on our official documentation website and got the idea but was unable to understand how to actually create and run a search query.  
I am writing this query in Kibana devtools

```auto
GET news_headlines/_search
{
  "query": {
    "match": {
      "authors": "Andy McDonald"    
    }
  }
}

```

and getting the response on Kibana

```auto
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "news_headlines",
        "_type" : "_doc",
        "_id" : "QIM5qIAB1QJoV5WnD2FW",
        "_score" : 1.0,
        "_source" : {
          "date" : "2018-05-26",
          "short_description" : "She left her husband. He killed their children. Just another day in America.",
          "@timestamp" : "2018-05-26T00:00:00.000+05:30",
          "link" : "https://www.huffingtonpost.com/entry/texas-amanda-painter-mass-shooting_us_5b081ab4e4b0802d69caad89",
          "category" : "CRIME",
          "headline" : "There Were 2 Mass Shootings In Texas Last Week, But Only 1 On TV",
          "authors" : "Melissa Jeltsen"
        }
      },
      {
        "_index" : "news_headlines",
        "_type" : "_doc",
        "_id" : "QYM5qIAB1QJoV5WnD2FW",
        "_score" : 1.0,
        "_source" : {
          "date" : "2018-05-26",
          "short_description" : "Of course it has a song.",
          "@timestamp" : "2018-05-26T00:00:00.000+05:30",
          "link" : "https://www.huffingtonpost.com/entry/will-smith-joins-diplo-and-nicky-jam-for-the-official-2018-world-cup-song_us_5b09726fe4b0fdb2aa541201",
          "category" : "ENTERTAINMENT",
          "headline" : "Will Smith Joins Diplo And Nicky Jam For The 2018 World Cup's Official Song",
          "authors" : "Andy McDonald"
        }
      }, .. . . . . .. . .. . . .. ......

```

I want to do the same through my Java application, when I tried to read the documentation and do this, I wrote a Getmapping request in my controller which looks like

```auto
	@GetMapping("/search")
	public SearchRequest trail3() throws IOException {
		SearchRequest searchRequest = new SearchRequest();//1
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //2
		sourceBuilder.query(QueryBuilders.termQuery("headline", "will"));
		sourceBuilder.from(0); 
		sourceBuilder.size(5); 
		
		searchRequest.indices("news_headlines");

		return searchRequest.source(sourceBuilder);
	
	}

```

and when I run it from postman I get the output as follows

```auto
{
    "batchedReduceSize": 512,
    "maxConcurrentShardRequests": 5,
    "preFilterShardSize": null,
    "ccsMinimizeRoundtrips": true,
    "suggestOnly": false,
    "shouldStoreResult": false,
    "parentTask": {
        "nodeId": "",
        "id": -1,
        "set": false
    },
    "description": ""
}

```

Please let me know how to do this, my end goal is to create a search bar for our document storing application.

Also, please let me know if you need any additional information.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 9, 2022, 2:44pm UTC](https://discuss.elastic.co/t/how-to-perform-a-search-query-on-an-indexed-document-using-java/304259/2 "2022-05-09T14:44:55Z")

</div>

Hi!

Are you using the Java API Client or the Java REST Client?

If you are using the High Level Rest Client, I noticed in your code that the RestHighLevelClient is missing.

Add this line to the end:

> client.search(searchRequest, RequestOptions.DEFAULT)

I recommend reading:

> **[Initialization | Java REST Client \[7.17\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html)**

Java API Client:  
[https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/\_getting\_started.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_getting_started.html)

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 9, 2022, 3:59pm UTC](https://discuss.elastic.co/t/how-to-perform-a-search-query-on-an-indexed-document-using-java/304259/3 "2022-05-09T15:59:55Z")

</div>

I am sorry I forgot to mention i'm using Java High level rest client, I created object of Java Rest Client `RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http"), new HttpHost("localhost")));`

Thank you so much Issue is resolved now, seems I was returning **searchRequest** and I needed to return **searchResponse**.

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 9, 2022, 4:06pm UTC](https://discuss.elastic.co/t/how-to-perform-a-search-query-on-an-indexed-document-using-java/304259/4 "2022-05-09T16:06:49Z")

</div>

Also, can you please suggest me a document or a website on how to create a search functionality for a document storing application?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 9, 2022, 4:28pm UTC](https://discuss.elastic.co/t/how-to-perform-a-search-query-on-an-indexed-document-using-java/304259/5 "2022-05-09T16:28:34Z")

</div>

I started with the documentation of elastic itself to understand the components and apply them in the best way.  
I'm not sure what your business is but some of these links below can guide you.

> **[Query and filter context | Elasticsearch Guide \[8.2\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html)**

> **[Implementing a modern E-Commerce Search](https://spinscale.de/posts/2020-06-22-implementing-a-modern-ecommerce-search.html)**
>
> TLDR; Before joining Elastic my previous job was running a product search engine at a small b2b market place. I learned quite a few things, and especially, what I would do differently nowadays, given that search engines have evolved a lot in the last...

> **[How to implement Elastic Search Using Spring Boot?](https://medium.com/javarevisited/how-to-implement-using-elastic-search-using-spring-boot-31a4f800b41f)**
>
> Hello 🖐 It's Rohan Ravindra Kadam 😊

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [May 9, 2022, 4:30pm UTC](https://discuss.elastic.co/t/how-to-perform-a-search-query-on-an-indexed-document-using-java/304259/6 "2022-05-09T16:30:40Z")

</div>

Thank you, I will definitely look into these.

---

<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:** [June 6, 2022, 4:31pm UTC](https://discuss.elastic.co/t/how-to-perform-a-search-query-on-an-indexed-document-using-java/304259/7 "2022-06-06T16:31:04Z")

</div>

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