0

I try this code URL is http://localhost:9200 index is restaurant type is menu. I want pass id through the searchbuilder. Please anyone can help me. I got this output

722 [main] INFO io.searchbox.client.JestClientFactory - Node Discovery       Disabled...
hai 
io.searchbox.core.SearchResult@35f8a538

but I want id data

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.SearchResult.Hit;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import jesttask.*;

import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import com.google.gson.JsonObject;

public class Elasticsearch 
{
public static void main( String[] args ) throws Exception 
{
    JestClient client = openClient();

    JsonObject json=new JsonObject();

for search code here

   /* Search search = (Search) new Search.Builder("http://localhost:9200/")
    // multiple index or types can be added.
    .addIndex("articles")
    .addType("article")
    .build();

JestResult result = client.execute(search);*/

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.queryString("AVE5MpaA7X6GJDWpFptT"));
    Search search = (Search) new Search.Builder(searchSourceBuilder.toString())
                                    // multiple index or types can be added.
                                    .addIndex("restarent")
                                    .addType("menu")
                                    .build();
    System.out.println("hai");
    JestResult result = client.execute(search);
    System.out.println(result);


    List<SearchModel> reviewList = result.getSourceAsObjectList(SearchModel.class);
    for(SearchModel review: reviewList){
        System.out.println("h");
      System.out.println("search result is " + review);
    }

    /*
    SearchHit[] results = result.getHits().getHits();
    for (SearchHit hit : results) {
      System.out.println(hit.getId());    //prints out the id of the document
      Map<String,Object> result2 = hit.getSource();   //the retrieved document
    }*/

    /*List<SearchModel> sources = result.getSourceAsObjectList(SearchModel.class);

    for (SearchModel source : sources) {
        System.out.println("h");

        System.out.println(source.getTitle() + " -> " + source.getItem());
    }*/

    client.shutdownClient();
}

private static JestClient openClient()
{ 
 HttpClientConfig clientConfig = new HttpClientConfig.Builder("http://localhost:9200/")
      .multiThreaded(true).build();
 JestClientFactory factory = new JestClientFactory();

 factory.setHttpClientConfig(clientConfig);
 JestClient jestClient = factory.getObject();

 return jestClient;
}
}

my model class is

import io.searchbox.annotations.JestId;

import com.fasterxml.jackson.annotation.JsonProperty;

public class SearchModel {
@JestId
private String id;

@JsonProperty("price")
private double price;
@JsonProperty("quantity")
private String quantity;

@JsonProperty("item")
private String item;




public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public double getPrice() {
    return price;
}
public void setPrice(Double price) {
    this.price = price;
}

public String getQuantity() {
    return quantity;
}
public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public String getItem() {
    return item;
}
public void setItem(String item) {
    this.item = item;
}


}

5 Answers 5

1

You can pass id through Get.Builder from io.searchbox.core.Get

Get get=new 
    Get.Builder("restaurant","z1iKdWEBNuT7WCY_pthh").type("menu").build();
    JestResult result = client.execute(get);
    System.out.println(result.getJsonString());
Sign up to request clarification or add additional context in comments.

Comments

1

This may be helpful for you:

public Employee employeeDetail(String id) throws Exception {

  JestClientFactory factory = new JestClientFactory();
  factory.setHttpClientConfig(new HttpClientConfig.Builder("http://" + hostName +":" + portNumber).multiThreaded(true).build());
  jestClient = factory.getObject();
  Get get = new Get.Builder("employees", id).type("empid").build();
  JestResult results = jestClient.execute(get);
  Employee employeeConsumer = new Employee();

  if(results.isSucceeded()) {
    Employee employee = new ObjectMapper()
       .readValue(results.getJsonObject().get("_source").toString(), Employee.class);
    employeeConsumer.setEmpName(employee.getEmpName());
    employeeConsumer.setEmpId(employee.getEmpId());
    employeeConsumer.setEmpDept(employee.getEmpDept());
  }

  return employeeConsumer;
}

You can download code from git repository as well.

https://github.com/pradeekb/ElasticSearch

Comments

0

With a simple GET call on your broswer you can verify if your index endpoint it's working well:

http://localhost:9200/yourIndexName

If all seems well (you should get http status 200) try change

QueryBuilders.queryString("AVE5MpaA7X6GJDWpFptT")

to QueryBuilders.matchQuery("id", "AVE5MpaA7X6GJDWpFptT")

Note: just a guess, not tested.

Edit: You can also verify if all seems well with your sintax "restarent" xD

Comments

0
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchQuery("url", "https://www.elastic.co/blog/*"));

This works.

Comments

0

QueryBuilders.matchQuery("_id", "AVE5MpaA7X6GJDWpFptT")

id is stored as _id in elasticsearch db. This will give you the expected result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.