elasticsearch,elasticsearch-jest,Java,elasticsearch,Elasticsearch Jest" /> elasticsearch,elasticsearch-jest,Java,elasticsearch,Elasticsearch Jest" />

如何在java中使用jest客户端编写elasticsearch中的搜索代码

如何在java中使用jest客户端编写elasticsearch中的搜索代码,java,elasticsearch,elasticsearch-jest,Java,elasticsearch,Elasticsearch Jest,我试试这个代码URL是索引是餐厅类型是菜单。 我想通过searchbuilder传递id。任何人都可以帮助我。我得到了这个输出 722 [main] INFO io.searchbox.client.JestClientFactory - Node Discovery Disabled... hai io.searchbox.core.SearchResult@35f8a538 但是我想要身份证数据 import io.searchbox.client.JestClient; i

我试试这个代码URL是索引是餐厅类型是菜单。 我想通过
searchbuilder
传递id。任何人都可以帮助我。我得到了这个输出

722 [main] INFO io.searchbox.client.JestClientFactory - Node Discovery       Disabled...
hai 
io.searchbox.core.SearchResult@35f8a538
但是我想要身份证数据

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();
搜索代码在这里

   /* 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;
}
}

通过对broswer进行简单的GET调用,您可以验证您的索引端点是否正常工作:

如果一切正常(您应该获得http状态200),请尝试更改

QueryBuilders.queryString(“AVE5MpaA7X6GJDWpFptT”)

QueryBuilders.matchQuery(“id”,“AVE5MpaA7X6GJDWpFptT”)

注:仅为猜测,未经测试

编辑:您还可以验证sintax“Restart”xD是否一切正常

这很有效。

QueryBuilders.matchQuery(“\u id”,“AVE5MpaA7X6GJDWpFptT”)


id作为_id存储在elasticsearch数据库中。这将为您提供预期的结果。

您可以通过io.searchbox.core.Get中的Get.Builder传递id

Get get=new 
    Get.Builder("restaurant","z1iKdWEBNuT7WCY_pthh").type("menu").build();
    JestResult result = client.execute(get);
    System.out.println(result.getJsonString());

这可能对您有所帮助:

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;
}
您也可以从git存储库下载代码

Get get=new 
    Get.Builder("restaurant","z1iKdWEBNuT7WCY_pthh").type("menu").build();
    JestResult result = client.execute(get);
    System.out.println(result.getJsonString());
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;
}