在使用Transport Client创建索引时,如何指定对not_analyzed的字段分析

时间:2018-01-27 05:40:01

标签: elasticsearch

我正在使用elasticsearch-6.1.1。由于JavaHighLeve client-6.1.1没有提供CreateIndexRequest api,我尝试通过解决TransportClient来创建索引,如下所示:(https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/_changing_the_application_8217_s_code.html) 代码是:

public class IndexOperations {

    RestHighLevelClient client ;
    public IndexOperations(RestHighLevelClient client){
        this.client = client ; 
    }

    public void indexOperations()throws UnknownHostException,IOException  {

        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));


        String mapping = XContentFactory.jsonBuilder()
                        .startObject()
                            .startObject("delllogfiles")
                                .startObject("properties")

                                    .startObject("message")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("logid")
                                        .field("type","long")
                                    .endObject()

                                    .startObject("version")
                                        .field("type","long")
                                    .endObject()

                                    .startObject("qualifiers")
                                        .field("type","text")
                                    .endObject()

                                    .startObject("level")
                                        .field("type","long")
                                    .endObject()

                                    .startObject("task")
                                        .field("type","long")
                                    .endObject()

                                    .startObject("opcode")
                                        .field("type","long")
                                    .endObject()

                                    .startObject("keywords")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("recordid")
                                        .field("type","long")
                                    .endObject()

                                    .startObject("providername")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("providerid")
                                        .field("type","long")
                                    .endObject()

                                    .startObject("logname")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("processid")
                                        .field("type","long")
                                    .endObject()

                                    .startObject("threadid")
                                        .field("type","long")
                                    .endObject()

                                    .startObject("machinename")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("userid")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("timecreated")
                                        .field("type","date")
                                    .endObject()

                                    .startObject("activityid")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("relatedactivityid")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("containerlog")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("matchedqueryids")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("bookmark")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("levldispalyname")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("opcodedisplayname")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("taskdisplayname")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("keywordsdisplayname")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                    .startObject("properties")
                                        .field("type","text")
                                        .field("index","not_analyzed")
                                    .endObject()

                                .endObject()
                            .endObject()
                        .endObject()
                        .string(); 


        CreateIndexResponse createIndexResponse = transportClient.admin().indices()
                                                    .prepareCreate("logfiles")
                                                    .addMapping("delllogfiles",mapping,XContentType.JSON)
                                                    .get();

        System.out.println("finally index created "+createIndexResponse.isAcknowledged());
        transportClient.close();

运行时代码我得到一个例外:

Exception in thread "main" MapperParsingException[Failed to parse mapping [delllogfiless]: Could not convert [message.index] to boolean]; nested: IllegalArgumentException[Could not convert [message.index] to boolean]; nested: IllegalArgumentException[Failed to parse value [not_analyzed] as only [true] or [false] are allowed.];

如何在使用TransportClient时将字段的anaylzer设置为not_analyzed?

1 个答案:

答案 0 :(得分:10)

从弹性搜索5索引属性只有两个选项

对于not_analyzed字符串字段,请使用字段类型关键字而不是文字,默认情况下为not_analyzed

这是它的链接。 https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html