我试试这个代码 URL 是http://localhost:9200索引是餐厅类型是菜单。我想通过searchbuilder
. 请任何人都可以帮助我。我得到了这个输出
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;
}
}
我的模型课是
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;
}
}