elasticsearch笔记(4) java操作es的查询_04----- prefix查询 fuzzy查询 wildcard查询 range查询 regexp查询
作者:互联网
以下所有匹配的查询因为涉及到匹配, 所以效率低.(regexp查询 wildcard查询 fuzzy查询 prefix查询)
一. prefix前缀查询, 比如某个field是"途虎科技", 搜索词是"途虎";则可以查询出来;
和match的区别, 如果"途虎科技"是"keyword"类型, 是查询不到的.
1.1 kibana中用prefix查询
1.2 java中用prefix查询
1 @Test 2 public void getPrefixTest() throws IOException { 3 // 1. 创建查询对象 4 String index = "sms-logs-index"; 5 String type = "sms-logs-type"; 6 SearchRequest searchRequest = new SearchRequest(index);//指定索引 7 searchRequest.types(type);//指定类型 8 // 2. 封装查询条件 9 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 10 searchSourceBuilder.query(QueryBuilders.prefixQuery("corpName","途虎")); 11 searchRequest.source(searchSourceBuilder); 12 13 // 3.执行查询 14 // client执行 15 HttpHost httpHost = new HttpHost("192.168.43.30", 9200); 16 RestClientBuilder restClientBuilder = RestClient.builder(httpHost); 17 RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); 18 SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); 19 20 // 4.获取数据(source中的数据) 21 SearchHit[] hits = search.getHits().getHits(); 22 for(SearchHit searchHit : hits){ 23 System.out.println(searchHit); 24 } 25 }
二. fuzzy模糊匹配
当匹配的内容中有途虎两个字不错误, 后面的有错别字, 可以匹配.
2.1 kibana中用fuzzy查询
2.2java中用fuzzy查询
1 @Test 2 public void getFuzzyTest() throws IOException { 3 // 1. 创建查询对象 4 String index = "sms-logs-index"; 5 String type = "sms-logs-type"; 6 SearchRequest searchRequest = new SearchRequest(index);//指定索引 7 searchRequest.types(type);//指定类型 8 // 2. 封装查询条件 9 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 10 searchSourceBuilder.query(QueryBuilders.fuzzyQuery("corpName","途虎养伡").prefixLength(2)); 11 searchRequest.source(searchSourceBuilder); 12 13 // 3.执行查询 14 // client执行 15 HttpHost httpHost = new HttpHost("192.168.43.30", 9200); 16 RestClientBuilder restClientBuilder = RestClient.builder(httpHost); 17 RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); 18 SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); 19 20 // 4.获取数据(source中的数据) 21 SearchHit[] hits = search.getHits().getHits(); 22 for(SearchHit searchHit : hits){ 23 System.out.println(searchHit); 24 } 25 }
三. wildcard查询(未知查询)通配查询
3.2 在java中使用wildcard查询
@Test public void getWildcardTest() throws IOException { // 1. 创建查询对象 String index = "sms-logs-index"; String type = "sms-logs-type"; SearchRequest searchRequest = new SearchRequest(index);//指定索引 searchRequest.types(type);//指定类型 // 2. 封装查询条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.wildcardQuery("corpName","途虎*")); searchRequest.source(searchSourceBuilder); // 3.执行查询 // client执行 HttpHost httpHost = new HttpHost("192.168.43.30", 9200); RestClientBuilder restClientBuilder = RestClient.builder(httpHost); RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); // 4.获取数据(source中的数据) SearchHit[] hits = search.getHits().getHits(); for(SearchHit searchHit : hits){ System.out.println(searchHit); } }
四. range查询
在某个范围内的查询:
4.1 查询fee字段的范围是0-10的
4.2 java中用range查询
@Test public void getrangesTest() throws IOException { // 1. 创建查询对象 String index = "sms-logs-index"; String type = "sms-logs-type"; SearchRequest searchRequest = new SearchRequest(index);//指定索引 searchRequest.types(type);//指定类型 // 2. 封装查询条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.rangeQuery("fee").gte(0).lte(10)); searchRequest.source(searchSourceBuilder); // 3.执行查询 // client执行 HttpHost httpHost = new HttpHost("192.168.43.30", 9200); RestClientBuilder restClientBuilder = RestClient.builder(httpHost); RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); // 4.获取数据(source中的数据) SearchHit[] hits = search.getHits().getHits(); for(SearchHit searchHit : hits){ System.out.println(searchHit); } }
五 regexp查询: 正则查询,
5.1 regexp在在kibana
5.2 regexp在java中
1 @Test 2 public void regexTest() throws IOException { 3 // 1. 创建查询对象 4 String index = "sms-logs-index"; 5 String type = "sms-logs-type"; 6 SearchRequest searchRequest = new SearchRequest(index);//指定索引 7 searchRequest.types(type);//指定类型 8 // 2. 封装查询条件 9 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 10 searchSourceBuilder.query(QueryBuilders.regexpQuery("mobile","159[0-9]{7}")); 11 searchRequest.source(searchSourceBuilder); 12 13 // 3.执行查询 14 // client执行 15 HttpHost httpHost = new HttpHost("192.168.43.30", 9200); 16 RestClientBuilder restClientBuilder = RestClient.builder(httpHost); 17 RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); 18 SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); 19 20 // 4.获取数据(source中的数据) 21 SearchHit[] hits = search.getHits().getHits(); 22 for(SearchHit searchHit : hits){ 23 System.out.println(searchHit); 24 } 25 }
------------恢复内容结束------------
标签:index,search,java,04,searchRequest,查询,new,type 来源: https://www.cnblogs.com/dangdanghepingping/p/14406349.html