我有在Elasticsearch中索引的对象:
{“id”:“一个”,“名称”:“John”}
{“id”:“两个”,“名称”:“Steve”}
我用索引“people”,类型“human”和文档“/ id(one,two)/”将其置为 flex
任务是使用带有高级客户端的Elasticsearch 6.2.4在Java中按“名称”搜索记录。
这是我的代码示例:
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("name", name));
SearchRequest searchRequest = new SearchRequest("people");
searchRequest.types("human");
searchRequest.indices("name").source(sourceBuilder);
SearchResponse searchResponse;
RestHighLevelClient client = getClient();
searchResponse = client.search(searchRequest);
这是行不通的。
在执行搜索时需要帮助。
最佳答案
请尝试以下代码。
SearchRequest searchRequest = new SearchRequest("people");
searchRequest.types("human");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("name", name"));
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
此处
name
必须为小写。