java 如何使用java中的jest客户端在elasticsearch中编写搜索代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33909389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to write code for search in elasticsearch using jest client in java
提问by bhagavan
I try this code URL is http://localhost:9200index is restaurant type is menu.
I want pass id through the searchbuilder
. Please anyone can help me. I got this output
我试试这个代码 URL 是http://localhost:9200索引是餐厅类型是菜单。我想通过searchbuilder
. 请任何人都可以帮助我。我得到了这个输出
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.Hymanson.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;
}
}
回答by umamahesh
You can pass id through Get.Builder from io.searchbox.core.Get
您可以从 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());
回答by pradeep kb
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.
您也可以从 git 存储库下载代码。
回答by Pedro Sequeira
With a simple GET call on your broswer you can verify if your index endpoint it's working well:
通过对您的浏览器进行简单的 GET 调用,您可以验证您的索引端点是否运行良好:
http://localhost:9200/yourIndexName
http://localhost:9200/yourIndexName
If all seems well (you should get http status 200) try change
如果一切正常(您应该获得 http 状态 200),请尝试更改
QueryBuilders.queryString("AVE5MpaA7X6GJDWpFptT")
QueryBuilders.queryString("AVE5MpaA7X6GJDWpFptT")
to QueryBuilders.matchQuery("id", "AVE5MpaA7X6GJDWpFptT")
到 QueryBuilders.matchQuery("id", "AVE5MpaA7X6GJDWpFptT")
Note: just a guess, not tested.
注意:只是猜测,未经测试。
Edit: You can also verify if all seems well with your sintax "restarent" xD
编辑:您还可以验证您的语法“restarent”xD 是否一切正常
回答by PraveenKumar K G
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("url", "https://www.elastic.co/blog/*"));
This works.
这有效。
回答by Emma
QueryBuilders.matchQuery("_id", "AVE5MpaA7X6GJDWpFptT")
QueryBuilders.matchQuery("_id", "AVE5MpaA7X6GJDWpFptT")
id is stored as _id in elasticsearch db. This will give you the expected result.
id 在 elasticsearch 数据库中存储为 _id。这会给你预期的结果。