java创建mapping_JAVA 创建 ElaticSearch mapping

用JAVA创建mapping

package pylx.createMapping;

import java.net.InetAddress;

import java.net.UnknownHostException;

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;

import org.elasticsearch.client.Client;

import org.elasticsearch.client.Requests;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.transport.InetSocketTransportAddress;

import org.elasticsearch.common.xcontent.XContentBuilder;

import org.elasticsearch.common.xcontent.XContentFactory;

public class CreateMapping {

/**

* 创建索引名称

* @param indices 索引名称

*/

public static void createCluterName(String indices){

Client client =null;

try {

client = TransportClient.builder().build()

.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.10.10"), 9300));

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

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

client.close();

}

/**

* 创建mapping(feid("indexAnalyzer","ik")该字段分词IK索引 ;feid("searchAnalyzer","ik")该字段分词ik查询;具体分词插件请看IK分词插件说明)

* @param indices 索引名称;

* @param mappingType 索引类型

* @throws Exception

*/

public static void createMapping(String indices,String mappingType)throws Exception{

Client client =null;

try {

client = TransportClient.builder().build()

.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("168.192.122.100"), 9300));

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

new XContentFactory();

XContentBuilder builder=XContentFactory.jsonBuilder()

.startObject()

.startObject(indices)

.startObject("properties")

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

.startObject("name").field("type", "string").field("store", "yes").field("term_vector","with_positions_offsets").field("analyzer","ik_max_word").field("search_analyzer","ik_max_word").field("include_in_all","true").field("boost",8).endObject()

.startObject("address").field("type", "string").field("store", "yes").field("term_vector","with_positions_offsets").field("analyzer","ik_max_word").field("search_analyzer","ik_max_word").field("include_in_all","true").field("boost",8).endObject()

.startObject("password").field("type", "string").field("store", "yes").field("term_vector","with_positions_offsets").field("analyzer","ik_max_word").field("search_analyzer","ik_max_word").field("include_in_all","true").field("boost",8).endObject()

.startObject("userinfo").field("type", "string").field("store", "yes").field("term_vector","with_positions_offsets").field("analyzer","ik_max_word").field("search_analyzer","ik_max_word").field("include_in_all","true").field("boost",8).endObject()

.endObject()

.endObject()

.endObject();

PutMappingRequest mapping = Requests.putMappingRequest(indices).type(mappingType).source(builder);

client.admin().indices().putMapping(mapping).actionGet();

client.close();

}

public static void main(String[] args)throws Exception {

createCluterName("lianan");

createMapping("lianan", "lianan");

}

}

//插入数据

curl -XPOST http://localhost:9200/lianan/lianan/2 -d'

{"id":1,"name":"汪冉冉12222","address":"地址海淀区","password":"密码是什么","userinfo":"用户信息,北京市中国江苏"}

'

结果

[root@c6-100-100 elasticsearch]# curl -XPOST http://localhost:9200/lianan/lianan/2 -d'

> {"id":1,"name":"汪冉冉12222","address":"地址海淀区","password":"密码是什么","userinfo":"用户信息,北京市中国江苏"}> '

{"_index":"lianan","_type":"lianan","_id":"2","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}[root@c6-00-100 elasticsearch]#

curl -XPOST http://localhost:9200/lianan/lianan/4 -d'

{"id":1,"name":"汪冉冉12222","address":"地址海淀区","password":"密码是什么","userinfo":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}

'

查询数据

curl -XPOST http://localhost:9200/lianan/lianan/_search -d'

{

"query" : { "match" : { "password" : "是什么" }},

"highlight" : {

"pre_tags" : ["", ""],

"post_tags" : ["", ""],

"fields" : {

"password" : {}

}

}

}

'

结果

[root@c6-100-100 elasticsearch]# curl -XPOST http://localhost:9200/lianan/lianan/_search -d'

> {

> "query" : { "match" : { "password" : "是什么" }},

> "highlight" : {

> "pre_tags" : ["", ""],

> "post_tags" : ["", ""],

> "fields" : {

> "password" : {}

> }

> }

> }

> '

{"took":14,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":3.089294,"hits":[{"_index":"lianan","_type":"lianan","_id":"4","_score":3.089294,"_source":

{"id":1,"name":"汪冉冉12222","address":"地址海淀区","password":"密码是什么","userinfo":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}

,"highlight":{"password":["密码是什么"]}},{"_index":"lianan","_type":"lianan","_id":"2","_score":3.089294,"_source":

{"id":1,"name":"汪冉冉12222","address":"地址海淀区","password":"密码是什么","userinfo":"用户信息,北京市中国江苏"}

,"highlight":{"password":["密码是什么"]}}]}}[root@c6-100-100 elasticsearch]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值