1、前綴查詢(prefix)
//prefix前綴查詢 @Test public void test15() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名稱 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) //獲取es主機中節點的ip地址及端口號(以下是單個節點案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //prefixQuery 第一個參數為字段名,后面是以zhao開頭的條件進行查詢 QueryBuilder builder = QueryBuilders.prefixQuery("name", "zhao"); SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get(); SearchHits hits = response.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //將獲取的值轉換成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key對應的值為:" +map.get(key)); } } }
2、wildcard模糊查詢
//wildcard模糊查詢 @Test public void test16() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名稱 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) //獲取es主機中節點的ip地址及端口號(以下是單個節點案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //wildcardQuery模糊查詢 QueryBuilder builder = QueryBuilders.wildcardQuery("name", "zhao*"); SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get(); SearchHits hits = response.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //將獲取的值轉換成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key對應的值為:" +map.get(key)); } } }
3、fuzzy模糊查詢
說明:fuzzy才是實現真正的模糊查詢,我們輸入的字符可以是個大概,他可以根據我們輸入的文字大概進行匹配查詢,具體可看文章中的解釋和代碼,注意與wildcard模糊查詢的區別
//fuzzy模糊查詢(輸入的值輸入個大概也可以查詢出來) @Test public void test17() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名稱 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) //獲取es主機中節點的ip地址及端口號(以下是單個節點案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //fuzzyQuery模糊查詢 QueryBuilder builder = QueryBuilders.fuzzyQuery("name", "chagge"); SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get(); SearchHits hits = response.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //將獲取的值轉換成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key對應的值為:" +map.get(key)); } } } //typeQuery類型查詢 @Test public void test18() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名稱 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) //獲取es主機中節點的ip地址及端口號(以下是單個節點案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //typeQuery類型查詢 QueryBuilder builder = QueryBuilders.typeQuery("blog"); SearchResponse response = client.prepareSearch("index1").setQuery(builder).get(); SearchHits hits = response.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //將獲取的值轉換成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key對應的值為:" +map.get(key)); } } }
4、ids查詢
說明:根據多個id值進行查詢
//idsQuery id查詢(可以同時根據多個id進行查詢) @Test public void test19() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名稱 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.創建訪問ES服務器的客戶端 TransportClient client = new PreBuiltTransportClient(settings) //獲取es主機中節點的ip地址及端口號(以下是單個節點案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //idsQuery id查詢 QueryBuilder builder = QueryBuilders.idsQuery("1","2"); SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get(); SearchHits hits = response.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //將獲取的值轉換成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key對應的值為:" +map.get(key)); } } }
下一篇博客本人將書寫java操作elasticsearch實現聚合查詢。對后期博客感興趣的朋友可以關注交流,轉發請說明出處,本人的博客地址為:https://www.cnblogs.com/chenyuanbo/
技術在於交流!
