您的位置:首页 > 移动开发

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

2016-07-16 13:04 726 查看

1、默认集群连接

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


2、自定义集群连接

Settings settings = Settings.settingsBuilder().put("cluster.name", "cluster-lhy").build();
Client client = TransportClient.builder().settings(settings).build().addTransportAddress(new   InetSocketTransportAddress(InetAddress.getByName("localhost"),   9300));


3、index创建

client.admin().indices().prepareCreate("phone12").get();


4、mapping创建

有四种方式,只提倡前两种(json字符串格式和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();

/**
* 创建并添加方式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();*/
/**
* 创建并添加方式2
*/
//client.admin().indices().prepareCreate("phone1").addMapping("jingdong", builder).get();
/**
* 修改type中属性 方式1
*/
/* client.admin().indices().preparePutMapping(new String[]{"phone"})
.setType("jingdong")
.setSource("{\n" +
" \"properties\": {\n" +
" \"nameaaa\": {\n" +
" \"type\": \"string\"\n" +
" }\n" +
" }\n" +
"}")
.get();*/

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


5、2.3.4版本elasticsearch的ik分词器配置注意

 不需要再在elasticsearch配置文件elasticsearch.yml中配置

1.从https://github.com/medcl/elasticsearch-analysis-ik下载elasticsearch-analysis-ik-master.zip

2.解压elasticsearch-analysis-ik-master.zip

   unzip elasticsearch-analysis-ik-master.zip

3.进入elasticsearch-analysis-ik-master,编译源码

  cd  elasticsearch-analysis-ik-master
  mvn clean package
  编译后会放在 elasticsearch-analysis-ik-master/target/releases目录下



4.在$ES_HOME/plugins文件夹下创建ik

    mkdir ik
5、找到将编译后生成的elasticsearch-analysis-ik-1.9.4.zip解压到$ES_HOME/plugins/ik下



测试分词器curl -XPOST  "http://localhost:9200/索引/_analyze?analyzer=ik&pretty=true&text=我是中国人" 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: