ElasticSearch(8)--使用Java客戶端進行復雜查詢

進行復雜的查詢:

包括:字段匹配查詢、相似度查詢、範圍查詢、組合查詢、查詢加排序

package com.es.querydemo;
 
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import com.es.bean.Product;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
 
/**
 * 查詢
 * 
 * @author Beck
 * @date 2018年2月6日
 */
public class TestESQuery2 {
    private static final String HOST = "127.0.0.1";
    private static final int PORT = 9300;
    
    private static final String INDEX = "eshop";
    private static final String TYPE = "product";
    
    private static final ObjectMapper MAPPER = new ObjectMapper();
    
    private TransportClient client = null;
    
    // 查詢加排序
    @Test
    public void SortOrder(){
        SearchResponse searchResponse = this.client.prepareSearch(INDEX).setTypes(TYPE)
                .setQuery(QueryBuilders.matchAllQuery())
                .addSort("id", SortOrder.DESC)
                .get();
        
        // 查詢的總數(命中數)
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();
        System.out.println("總記錄數: " + totalHits);
        // 遍歷查詢的結果
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()){
            SearchHit next = iterator.next();
            // System.out.println(next.getSourceAsString());
            String id = next.getId();
            Map<String, Object> source = next.getSource();
            Integer productId = (Integer) source.get("id");
            String productTitle = (String) source.get("title");
            
            System.out.println("Document ID: " + id);
            System.out.println("商品的id: " + productId);
            System.out.println("商品的title: " + productTitle);
        }
    }
    // 組合查詢
    @Test
    public void boolQuery(){
        SearchResponse searchResponse = this.client.prepareSearch(INDEX).setTypes(TYPE)
                .setQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("id", 3))
                        .must(QueryBuilders.matchQuery("title", "超薄")))
                .get();
        
        // 查詢的總數(命中數)
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();
        System.out.println("總記錄數: " + totalHits);
        // 遍歷查詢的結果
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()){
            SearchHit next = iterator.next();
            // System.out.println(next.getSourceAsString());
            String id = next.getId();
            Map<String, Object> source = next.getSource();
            Integer productId = (Integer) source.get("id");
            String productTitle = (String) source.get("title");
            
            System.out.println("Document ID: " + id);
            System.out.println("商品的id: " + productId);
            System.out.println("商品的title: " + productTitle);
        }
    }
    // 範圍查詢:可以查詢數值也可以查詢字符串
    @Test
    public void rangeQuery(){
        SearchResponse searchResponse = this.client.prepareSearch(INDEX).setTypes(TYPE)
                .setQuery(QueryBuilders.rangeQuery("id").from(1).to(3).includeLower(false))
                .get();
        
        // 查詢的總數(命中數)
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();
        System.out.println("總記錄數: " + totalHits);
        // 遍歷查詢的結果
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()){
            SearchHit next = iterator.next();
            // System.out.println(next.getSourceAsString());
            String id = next.getId();
            Map<String, Object> source = next.getSource();
            Integer productId = (Integer) source.get("id");
            String productTitle = (String) source.get("title");
            
            System.out.println("Document ID: " + id);
            System.out.println("商品的id: " + productId);
            System.out.println("商品的title: " + productTitle);
        }
    }
    // 相似度查詢
    @Test
    public void fuzzyQuery(){
        SearchResponse searchResponse = this.client.prepareSearch(INDEX).setTypes(TYPE)
                .setQuery(QueryBuilders.fuzzyQuery("title", "appla"))
                .get();
        
        // 查詢的總數(命中數)
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();
        System.out.println("總記錄數: " + totalHits);
        // 遍歷查詢的結果
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()){
            SearchHit next = iterator.next();
            // System.out.println(next.getSourceAsString());
            String id = next.getId();
            Map<String, Object> source = next.getSource();
            Integer productId = (Integer) source.get("id");
            String productTitle = (String) source.get("title");
            
            System.out.println("Document ID: " + id);
            System.out.println("商品的id: " + productId);
            System.out.println("商品的title: " + productTitle);
        }
    }
    // 字段匹配查詢,多個字段(會進行分詞然後查詢)
    @Test
    public void multiMatchQuery(){
        SearchResponse searchResponse = this.client.prepareSearch(INDEX).setTypes(TYPE)
                .setQuery(QueryBuilders.multiMatchQuery("手機", "title", "id"))
                .get();
        
        // 查詢的總數(命中數)
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();
        System.out.println("總記錄數: " + totalHits);
        // 遍歷查詢的結果
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()){
            SearchHit next = iterator.next();
            // System.out.println(next.getSourceAsString());
            String id = next.getId();
            Map<String, Object> source = next.getSource();
            Integer productId = (Integer) source.get("id");
            String productTitle = (String) source.get("title");
            
            System.out.println("Document ID: " + id);
            System.out.println("商品的id: " + productId);
            System.out.println("商品的title: " + productTitle);
        }
    }
    // 字段匹配查詢,單個字段(會進行分詞然後查詢)
    @Test
    public void matchQuery(){
        SearchResponse searchResponse = this.client.prepareSearch(INDEX).setTypes(TYPE)
                .setQuery(QueryBuilders.matchQuery("title", "電視機"))
                .get();
        
        // 查詢的總數(命中數)
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();
        System.out.println("總記錄數: " + totalHits);
        // 遍歷查詢的結果
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()){
            SearchHit next = iterator.next();
            // System.out.println(next.getSourceAsString());
            String id = next.getId();
            Map<String, Object> source = next.getSource();
            Integer productId = (Integer) source.get("id");
            String productTitle = (String) source.get("title");
            
            System.out.println("Document ID: " + id);
            System.out.println("商品的id: " + productId);
            System.out.println("商品的title: " + productTitle);
        }
    }
    
    // 獲取客戶端
    @Before
    public void getClient() throws Exception{
        client = TransportClient.builder()
        .build()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT));
    }
    
    // 關閉客戶端
    @After
    public void closeClient(){
        if (this.client != null){
            this.client.close();
        }
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章