六、elasticsearch-java 的基本操做

 記錄的是es5.6.0的Java Api,springboot1.5.6java

@Resource
    TransportClient transportClient;

檢查索引是否存在

/**
     *  檢查索引是否存在
     * @param indexName 索引名稱
     * @return
     */
    public  boolean checkIndexExist( String indexName){
        IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);

        IndicesExistsResponse inExistsResponse = transportClient.admin().indices()
                .exists(inExistsRequest).actionGet();
        return inExistsResponse.isExists();
    }

    例如在建立索引名爲:book以前須要判斷book這個索引是否存在。spring

 

檢查類型是否存在

/**
     * 檢查類型是否存在
     * 注意: 這種狀況默認時 索引已經存在
     * @param indexName 索引名
     * @param typeName 類型名
     * @return
     */
    public  boolean checkTypeExist(String indexName,String typeName){
        if (!checkIndexExist(indexName)){
            return false;
        }
        TypesExistsRequest inExistsRequest = new TypesExistsRequest(new String[]{indexName},typeName);

        TypesExistsResponse inExistsResponse = transportClient.admin().indices().typesExists(inExistsRequest)
                .actionGet();

        return inExistsResponse.isExists();
    }

 

刪除索引

/**
     * 刪除索引
     * @param indexName
     */
    public  void deleteIndex(String indexName){
        if (!checkIndexExist(indexName)) {
            System.out.println(indexName + " not exists");
        } else {

            DeleteIndexResponse dResponse = transportClient.admin().indices().prepareDelete(indexName)
                    .execute().actionGet();
            if (dResponse.isAcknowledged()) {
                System.out.println("delete index "+indexName+"  successfully!");

            }else{
                System.out.println("Fail to delete index "+indexName);

            }
        }

    }

當一個索引被刪除,那麼索引下的全部類型和下面的文檔將都被刪除。json

建立文檔

示例:建立一個索引名books類型名novels的文檔。springboot

//構建文檔內容
            XContentBuilder content = null;
            try {
                content = XContentFactory.jsonBuilder().startObject()
                        .field("name", "三體-黑暗森林")
                        .field("author","大劉")
                        .field("date","2017-01-25 14:12:52")
                        .field("id", 1);
                        .field("price",102)
                content.endObject();


                //構建索引
                IndexResponse result = transportClient.prepareIndex("books", "novels")
                        .setSource(content)
                        .get();

            } catch (IOException e) {
                e.printStackTrace();
            }

 

高亮顯示

  1. 先須要指定搜索命中的文檔的哪些字段須要高亮顯示
  2. 封裝高亮顯示的樣式
/**
     * 啓用高亮字段
     * @param builder
     * @param fields 須要高亮顯示的文檔字段
     */
    public  void enableHighlight(SearchRequestBuilder builder, String ... fields) {
        //設置自定義高亮顯示
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        Objects.requireNonNull(fields);
        for (String field : fields) {
            highlightBuilder.field(field);
        }
        highlightBuilder.preTags("<font color=\"red\">");
        highlightBuilder.postTags("</font>");
        builder.highlighter(highlightBuilder);
    }

    /**
     * 封裝高亮字段搜索結果
     * @param hit
     * @param fields 須要高亮顯示的文檔字段
     */
    public  void setHighlightResult(SearchHit hit, String ... fields){

        Objects.requireNonNull(fields);
        Map<String, HighlightField> highlightFields = hit.getHighlightFields();
        if (Objects.nonNull(highlightFields)){
            for (String field : fields) {
                HighlightField highlight = highlightFields.get(field);
                if (Objects.nonNull(highlight)){
                    Text[] fragments = highlight.fragments();

                    StringBuilder hitStr= new StringBuilder();
                    for (Text fragment : fragments) {
                        hitStr.append(fragment);
                    }
                    hit.getSource().put(field, hitStr.toString());
                }

            }
        }

    }

使用:app

 

搜索

 

持續更新……post

相關文章
相關標籤/搜索