Elasticsearch用java api 创建mapping
Client client = TransportClient.builder().build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); //建立链接 client.admin().indi
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); //建立链接
client.admin().indices().prepareCreate("producthuzhuindex").execute().actionGet(); //创建一个空索引,如没有索引,创建mapping时会报错
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject() .startObject("producthuzhuindex").startObject("properties")
.startObject("plan_intro") //嵌套对象字段
.startObject("properties")
.startObject("item").field("type", "string").field("store", "yes").field("analyzer", "ik").field("search_analyzer", "ik").endObject()
.startObject("content").field("type", "string").field("store", "yes").field("analyzer", "ik").field("search_analyzer", "ik").endObject()
.endObject()
.endObject()
.startObject("today_member").field("type", "string").field("store", "yes").endObject() //普通字段
//多字段 : 这个意思,我理解,就是一个字段有多个类型,如下这个,既有一个analyzer = id,又有一个no_analyzed 可以用于全文检索,还可以做精确查找。
.startObject("name").field("type", "string").field("store", "yes").field("analyzer","ik")
.startObject("fields").startObject("unname").field("type", "string").field("index","not_analyzed").endObject().endObject()
.endObject()
.endObject().endObject().endObject();
PutMappingRequest mappingRequest = Requests.putMappingRequest("producthuzhuindex").type("producthuzhuindex").source(mapping);
client.admin().indices().putMapping(mappingRequest).actionGet();
说明:
today_member:字段名
field("type", "string") 字段类型,必填
field("store", "yes") 是否存储
field("analyzer", "ik") 所使用的分词器
multi field:同一个json串指定不同的处理方式。
以上为我所遇到的mapping的所有情况。 今后还会不断补充。
建完mapping后,就可以往索引里灌数据了。
我新装了5.2以后,就没再单独创建mapping,5.2版,没有string类型了,全部用text,而且text的,自动加了keyword属性,如果精确查找,后面直接加keyword就ok,
例如: name.keyword='中国'
更多推荐



所有评论(0)