怎么又是你

Java REST Client 怎么把Lucene的原生query传给ES

Elasticsearch | 作者 liuzhen6835 | 发布于2019年02月14日 | 阅读数:329

Lucene的Query对象,或者query的字符串;怎么转成 Java REST Client 用 的Query对象
已邀请:

ouyangchucai - 阿里云Elasticsearch技术专家,Elasticsearch中文社区钉钉群号 : 21737292

赞同来自: liuzhen6835

GET请求URL中可以指定q参数,用lucene语法查询
Response response = restClient.performRequest("GET", "/index/type/_search?_source=id&sort=time&size=10&q=content:123", Collections.singletonMap("pretty", "true"));
 

ouyangchucai - 阿里云Elasticsearch技术专家,Elasticsearch中文社区钉钉群号 : 21737292

赞同来自: liuzhen6835

SearchRequest searchRequest = new SearchRequest(indexName);
searchRequest.types(typeName);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from(0);
sourceBuilder.size(10);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

//q为Lucene检索表达式, 直接输入关键词匹配_all或者*字段, 字段匹配user:kimchy, 多字段匹配user:kimchy AND message:Elasticsearch
QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery(q); sourceBuilder.query(queryStringQueryBuilder);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest);
SearchHits searchHits = searchResponse.getHits();

 

要回复问题请先登录注册