IK分词器api

IK下载及安装

medcl/elasticsearch-analysis-ik

1.compile

checkout ik version respective to your elasticsearch version

git checkout tags/{version}

mvn package

copy and unzip target/releases/elasticsearch-analysis-ik-{version}.zip to your-es-root/plugins/ik

2.restart elasticsearch

XContentBuilder方式

Client client = TransportClient.builder().build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

        new XContentFactory();

        XContentBuilder builder=XContentFactory.jsonBuilder()

        .startObject()//注意不要加index和type

                .startObject("properties")

                .startObject("id1").field("type", "integer").field("store", "yes").endObject()

                .startObject("kw1").field("type", "string").field("store", "yes").field("analyzer", "ik").endObject()

                .startObject("edate33").field("type", "date").field("store", "yes").endObject()

                .endObject()

            .endObject();

创建index并添加

/**
         * 创建并添加方式2
         */
          //client.admin().indices().prepareCreate("phone1").addMapping("jingdong", builder).get();   

修改属性1

/**
         * 修改type中属性 方式2
         */
        /*client.admin().indices().preparePutMapping(new String[]{"phone"})   
        .setType("jingdong").setSource(builder)
        .get();*/

修改属性2

PutMappingRequest mapping = Requests.putMappingRequest(indices).type(mappingType).source(builder);
        client.admin().indices().putMapping(mapping).actionGet();

单独创建index

client.admin().indices().prepareCreate(indices).execute().actionGet();

json方式

 /**
   * 创建并添加方式1
   */
      /*  client.admin().indices().prepareCreate("twitter1") 
        .addMapping("tweet", "{\n" +                
                "    \"tweet\": {\n" +
                "      \"properties\": {\n" +
                "        \"message1\": {\n" +
                "          \"type\": \"string\",\n" +
                       "             \"indexAnalyzer\": \"ik\"\n"+      
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }")
        .get();*/

修改属性

 /**
         * 修改type中属性 方式1
         */
   /*     client.admin().indices().preparePutMapping(new String[]{"phone"})   
        .setType("jingdong")                                
        .setSource("{\n" +                              
                "  \"properties\": {\n" +
                "    \"nameaaa\": {\n" +
                "      \"type\": \"string\"\n" +
                "    }\n" +
                "  }\n" +
                "}")
        .get();*/

方式3,不推荐

/**
         * 方式3不推荐
         */
       /* Map map=new HashMap();
        Map map2=new HashMap();
        map2.put("type", "string");
        Map map3=new HashMap();
     
        map.put("ooooooooo",map2);
        map3.put("properties", map);
        client.admin().indices().preparePutMapping(new String[]{"phone"})   
       
        .setType("jingdong").setSource(map3)
        .get();
        client.close();*/

测试

api查询

for (AnalyzeResponse.AnalyzeToken term : listAnalysis) {
    System.out.print(term.getTerm());
    System.out.print(',');
    queryBuilder.should(QueryBuilders.queryString(term.getTerm()).field("search_keys_ik"));
    //这里可以用must 或者 should 视情况而定
}
System.out.print('\n');

curl查询

curl -XPOST "http://localhost:9200/索引/_analyze?analyzer=ik&pretty=true&text=我是中国人"

参考

elasticsearch java api 使用ik 分词器
elasticsearch 2.3.4 java API 连接,ik分词器,设置集群节点,创建index,mapping的几种方式

你可能感兴趣的:(IK分词器api)