elasticsearch java动态设置mapping并指定分词器

//创建索引
client.admin().indices().prepareCreate("twitter").execute().actionGet();
//配置mapping
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject()
        .startObject("tweet")
            .startObject("properties")
            .startObject("user").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject()
            .startObject("message").field("type", "string").field("store", "yes").field("index", "analyzed").endObject()
            .startObject("postDate").field("type", "date").endObject()
            // .startObject("content").field("type","string").field("store","yes").field("analyzer","ik").field("search_analyzer","ik_smart").endObject()
            .startObject("content").field("type", "string").field("store", "yes").field("analyzer", "ik").endObject()
            .endObject()
        .endObject()
    .endObject();
PutMappingRequest mappingRequest = Requests.putMappingRequest("twitter").type("tweet").source(mapping);
client.admin().indices().putMapping(mappingRequest).actionGet();
 
// 批量插入数据
BulkRequestBuilder bulkRequest = client.prepareBulk();
IndexRequest request = client.prepareIndex("twitter", "tweet", "1").setSource(json).request();
IndexRequest request2 = client.prepareIndex("twitter", "tweet", "2").setSource(json2).request();
bulkRequest.add(request);
bulkRequest.add(request2);
bulkRequest.execute().actionGet();
client.close();

index的值只有三种(no,not_analyzed,analyzed)
https://www.elastic.co/guide/en/elasticsearch/reference/2.4/mapping-index.html
no:不添加到索引
not_analyzed:添加到索引不分词
analyzed : 添加到索引并分词

posted @ 2019-07-17 10:59  努力挣扎的小兵  阅读(7423)  评论(2编辑  收藏  举报