Elasticsearch search querry

GET products/_search
"query": {
"multi_match" : {
"query": "novel",
"fields": [ "description", "name","id" ,"price"]
this is the querry which get my results done i want to convert it into java api in this function can anyone help me out this is my querry class searching method where want to write this querry in mu controller class
package com.pixelTrice.elastic.search;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.search.Hit;

import org.apache.lucene.queryparser.flexible.core.builders.QueryBuilder;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ElasticSearchQuery {

private ElasticsearchClient elasticsearchClient;

private final String indexName = "products";

public String createOrUpdateDocument(Product product) throws IOException {

    IndexResponse response = elasticsearchClient.index(i -> i
    if (response.result().name().equals("Created")) {
        return new StringBuilder("Document has been successfully created.").toString();
    } else if (response.result().name().equals("Updated")) {
        return new StringBuilder("Document has been successfully updated.").toString();
    return new StringBuilder("Error while performing the operation.").toString();

public Product getDocumentById(String productId) throws IOException {
    Product product = null;
    GetResponse<Product> response = elasticsearchClient.get(g -> g

    if (response.found()) {
        product = response.source();
        System.out.println("Product name " + product.getName());
    } else {
        System.out.println("Product not found");

    return product;

public String deleteDocumentById(String productId) throws IOException {

    DeleteRequest request = DeleteRequest.of(d -> d.index(indexName).id(productId));

    DeleteResponse deleteResponse = elasticsearchClient.delete(request);
    if (Objects.nonNull(deleteResponse.result()) && !deleteResponse.result().name().equals("NotFound")) {
        return new StringBuilder("Product with id " + deleteResponse.id() + " has been deleted.").toString();
    System.out.println("Product not found");
    return new StringBuilder("Product with id " + deleteResponse.id() + " does not exist.").toString();

public List<Product> searchAllDocuments() throws IOException {

    SearchRequest searchRequest = SearchRequest.of(s -> s.index(indexName));
    SearchResponse searchResponse = elasticsearchClient.search(searchRequest, Product.class);
    List<Hit> hits = searchResponse.hits().hits();
    List<Product> products = new ArrayList<>();
    for (Hit object : hits) {

        System.out.print(((Product) object.source()));
        products.add((Product) object.source());

    return products;

public List<Product> searching() throws IOException{
	SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    QueryBuilder cluase0 = QueryBuilders.multiMatchQuery(queryString, 
    MultiMatchQueryBuilder multiMatchQueryBuilder1 = new MultiMatchQueryBuilder(queryString, "firstName", "lastName",
    	      "password", "emailId", "userId", "mobileNumber");


Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:


This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

1 Like

You already asked this question in How to convert this ES querry into java? which I'm going to close to keep the discussion in one place.

GET products/_search
"query": {
"multi_match" : {
"query": "novel",
"fields": [ "description", "name","id" ,"price"]

Please be patient in waiting for responses to your question and refrain from pinging multiple times asking for a response or opening multiple topics for the same question. This is a community forum, it may take time for someone to reply to your question. For more information please refer to the Community Code of Conduct specifically the section "Be patient". Also, please refrain from pinging folks directly, this is a forum and anyone that participates might be able to assist you.

If you are in need of a service with an SLA that covers response times for questions then you may want to consider talking to us about a subscription.

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

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