• 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 : 添加到索引并分词

  • 相关阅读:
    去除安卓apk中的广告
    公司固定资产管理细则
    固定资产基础知识
    C#的WebBrowser操作frame
    C#程序集版本控制文件属性祥解
    c#使用RSA进行注册码验证
    Web前端开发十日谈
    Web.xml配置详解
    IBM WebSphere MQ的C#工具类以及源码(net)
    Castle IOC FOR MVC 使用方法
  • 原文地址:https://www.cnblogs.com/gavinYang/p/11199658.html
Copyright © 2020-2023  润新知