term查询-脚本
term查询:不会对查询条件进行分词
语法:
GET 索引名称/_search
{
“query”:{
“term”:{
“字段名称”:{
“value”:“查询条件”
}}}}
/* termQuery:词条查询 */
@Test
public void testTermQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("goods");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 这里是结果词条查询
QueryBuilder query = QueryBuilders.termQuery("title","华为");
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
// 获取记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:"+value);
List<Goods> goodsList = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
// 转为Java
Goods goods = JSON.parseObject(sourceAsString, Goods.class);
goodsList.add(goods);
}
for (Goods goods : goodsList) {
System.out.println(goods);
}
}
}