我尝试此代码网址为http://localhost:9200索引是餐厅类型是菜单。
我希望通过searchbuilder
传递ID。请任何人都可以帮助我。我得到了这个输出
722 [main] INFO io.searchbox.client.JestClientFactory - Node Discovery Disabled...
hai
io.searchbox.core.SearchResult@35f8a538
但我想要id数据
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;
}
}
我的模型类是
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;
}
}
答案 0 :(得分:1)
您可以从io.searchbox.core.Get
通过Get.Builder传递idmysql -u root
答案 1 :(得分:1)
这可能对您有帮助:
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存储库下载代码。
答案 2 :(得分:0)
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("url", "https://www.elastic.co/blog/*"));
这很有效。
答案 3 :(得分:0)
QueryBuilders.matchQuery(“_ id”,“AVE5MpaA7X6GJDWpFptT”)
id在elasticsearch db中存储为_id。这将为您提供预期的结果。