1. 程式人生 > >Elasticsearch 5.2.1 建立索引-自定義分詞器

Elasticsearch 5.2.1 建立索引-自定義分詞器

開發語言:JAVA

解決問題:ES 5.2.1預設使用的 standard 分詞器,該分詞器是單個漢字進行分詞的,而需求使用二元分詞,故使用ngram 實現二元分詞

通過mapping 將分詞器 與要分詞的欄位進行對映

示例程式碼:

// DoubleAnalyzer 為自定義的fe

/** * 建立一個索引 * @param indexName 索引名 */
    public static void createIndex2(TransportClient client, String indexName) {
        try {
            /*
                OpenIndexResponse indexOpen = client.admin().indices().prepareOpen(indexName).get();
                if (indexOpen.isAcknowledged())
                {
                    System.out.println("open " + indexOpen.isAcknowledged()); // true表示建立成功
                    client.admin().indices().prepareClose(indexName);
                }
                */
                {
                    try {
                        XContentBuilder settingsBuilder = XContentFactory.jsonBuilder()
                                .startObject()
                                .startObject("analysis")
                                .startObject("analyzer")
                                .startObject("DoubleAnalyzer")
                                .field("tokenizer", "my_ngram_tokenizer")
                                .endObject()
                                .endObject()

                                .startObject("tokenizer")
                                .startObject("my_ngram_tokenizer")
                                .field("type", "ngram")
                                .field("min_gram", 2)
                                .field("max_gram", 2)
                                .field("token_chars", "letter, digit")
                                .endObject()
                                .endObject()
                                .endObject()
                                .endObject();

                        CreateIndexResponse indexResponse = client
                                .admin()
                                .indices()
                                .prepareCreate(indexName)
                                .setSettings(settingsBuilder)
                                .get();

                        System.out.println(indexResponse.isAcknowledged()); // true表示建立成功
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        } catch (ElasticsearchException e) {
            e.printStackTrace();
        }
    }

 /** * 給索引增加mapping。 * @param index 索引名 * @param type mapping所對應的type */
    public static void addMapping(TransportClient client, String index, String type) {
        try {
            // 使用XContentBuilder建立Mapping
            XContentBuilder builder = XContentFactory.jsonBuilder()
                            .startObject()
                            .field("properties")
                            .startObject()
                            .field("title")
                            .startObject()
                            .field("analyzer", "DoubleAnalyzer")
                            .field("type", "string")
                            .endObject()
                            .field("content")
                            .startObject()
                            .field("analyzer", "DoubleAnalyzer")
                            .field("type", "string")
                            .endObject()
                            .endObject()
                            .endObject();
            System.out.println(builder.string());
            PutMappingRequest mappingRequest = Requests.putMappingRequest(index).source(builder).type(type);
            client.admin().indices().putMapping(mappingRequest).actionGet();
        } catch (ElasticsearchException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

/** * 建立一個文件 * @param index index * @param type type */
    public static void createDoc(TransportClient client, String index, String type , List<String> jsonData) {

        try {
            for (String js : jsonData) {
                JSONObject jsonObj = JSON.parseObject(js);

                // 使用XContentBuilder建立一個doc source
                XContentBuilder builder =
                        XContentFactory.jsonBuilder()
                                .startObject()
                                .field("id", jsonObj.get("id").toString())
                                .field("title", jsonObj.get("title").toString())
                                .field("posttime", jsonObj.get("posttime").toString())
                                .field("content", jsonObj.get("content").toString())
                                .endObject();

                IndexResponse indexResponse = client
                        .prepareIndex()
                        .setIndex(index)
                        .setType(type)
                                // .setId(id) // 如果沒有設定id,則ES會自動生成一個id
                        .setSource(builder.string())
                        .get();
                System.out.println(indexResponse.status().getStatus());
            }

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

//根據關鍵字 進行檢索
    public static void searchIndex(TransportClient client)
    {

        //QueryBuilder qb1 = QueryBuilders.termQuery("title", "基本");
        QueryBuilder qb2= QueryBuilders.multiMatchQuery("CRUD", "title", "content");

        SearchResponse response = client.prepareSearch("blog2").setQuery(qb2).execute()
                .actionGet();

        //client.prepareSearch("blog")
        SearchHits hits = response.getHits();
        if (hits.totalHits() > 0) {
            for (SearchHit hit : hits) {
                System.out.println("score:"+hit.getScore()+":\t"+hit.getSource());// .get("title")
            }
        } else {
            System.out.println("搜到0條結果");
        }
    }

public static void main(String[] args) {
        try {

            //設定叢集名稱
            //
            // Settings settings = Settings.builder().put("cluster.name", "elasticsearch").put("xpack.security.user", "elastic:changeme").build();
            //Settings settings = Settings.builder().put("cluster.name", "elasticsearch").put("shield.user", "elastic:changeme").build();
            //ShieldPlugin shiledP = new ShieldPlugin(Settings.builder().put("cluster.name", "elasticsearch").put("shield.user", "elastic:changeme").build());
            //Settings settings = shiledP.additionalSettings(); //建立client
            //TransportClient client = new PreBuiltXPackTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.190.20.167"), 9300));
            //搜尋資料
            //GetResponse response = client.prepareGet("blog", "article", "1").execute().actionGet();


            //輸出結果
            //System.out.println(response.getSourceAsString());
            //關閉client
            //client.close();

            Settings settings = Settings.builder().put("cluster.name", "elasticsearch").put("xpack.security.user", "elastic:changeme").build();
            TransportClient client = new PreBuiltXPackTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.190.20.167"), 9300));

            //createIndex(client);

            searchIndex(client);

            //analyzeIndex(client);
            /*
            createIndex2(client, "blog2");
            addMapping(client, "blog2", "article2");*/

            //List<String> jsonData = DataFactory.getInitJsonData();

            //createDoc(client, "blog2", "article2", jsonData);

            //GetResponse response = client.prepareGet("blog", "article", "1").execute().actionGet();
            //System.out.println(response.getSourceAsString());
            /*
            String token = basicAuthHeaderValue("elastic", new SecuredString("changeme".toCharArray()));
            ActionResponse responseA  = client.filterWithHeader(Collections.singletonMap("Authorization", token)).prepareSearch().get();
            System.out.println(response.toString());
            */
            client.close();

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