使用javaAPI的方式指定elasticSearch的分词插件ik的analysis


package com.ali.es.ik;

import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;

/**
 * @author zdsg
 */

//IK有两种analyzer--- ik_max_word    ik_master
    //需要在创建索引的时候指定
    //如下api创建索引指定ik的analyzer
public class IK_Analysis {

    private TransportClient client;

    @Before

    public void init() throws  Exception{
        Settings settings = Settings.builder().put("cluster.name", "bigdata").put("client.transport.sniff",true).build();
        client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hadoop111"),9300));
    }


    @Test
    public void testCreate() throws IOException {

        XContentBuilder mapping = XContentFactory.jsonBuilder()
                .startObject()
                .startObject("properties")
                .startObject("content").field("type", "text").field("analyzer", "ik_max_word").endObject()
                .startObject("title").field("type", "text").field("analyzer", "ik_max_word").endObject()
                .endObject()    //第一个endObject是停止properties
                .endObject();   //第二个endObject是停止XCotentBuilder

        //china为索引库的名称    news为索引库的其中一个类型
        PutMappingRequest putmap = Requests.putMappingRequest("china").type("news").source(mapping);
        //创建索引
        CreateIndexResponse response = client.admin().indices().prepareCreate("china")
                .setSettings(Settings.builder()
                        .put("index.number_of_shards", 5)    //指定分片数
                        .put("index.number_of_replicas", 0)  //指定副本数
                ).get();


        //为索引添加映射----将type与field添加进去
        client.admin().indices().putMapping(putmap).actionGet();



    }

    @After
    public void close() {
        if(client!=null){
            client.close();
        }

    }




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值