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

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. 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

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

and getting the response on Kibana

{
  "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

	@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

{
    "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.

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:

Java API Client:
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_getting_started.html

1 Like

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.

1 Like

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

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.

Thank you, I will definitely look into these.

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