弹性搜索,通过Rest Java Client使用源和设置创建索引

时间:2019-04-27 15:44:05

标签: java elasticsearch

我正在尝试按照本指南创建索引:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-create-index.html#_providing_the_whole_source
问题是,该索引创建不正确。看起来整个settings部分以及completion类型都被忽略了。

我的json文件:

{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 1,
        "analysis": {
            "filter": {},
            "analyzer": {
                "keyword_analyzer": {
                    "filter": [
                        "lowercase",
                        "asciifolding",
                        "trim"
                    ],
                    "char_filter": [],
                    "type": "custom",
                    "tokenizer": "keyword"
                }
            }
        }
    },
    "mappings": {
        "my_type": {
            "properties": {
                "first": {
                    "type": "text",
                    "fields": {
                        "keywordstring": {
                            "type": "text",
                            "analyzer": "keyword_analyzer"
                        },
                        "completion": {
                            "type": "completion"
                        }
                    },
                    "analyzer": "standard"
                },
                "second": {
                    "type": "text",
                    "fields": {
                        "keywordstring": {
                            "type": "text",
                            "analyzer": "keyword_analyzer"
                        },
                        "completion": {
                            "type": "completion"
                        }
                    },
                    "analyzer": "standard"
                },
                "third": {
                    "type": "text",
                    "fields": {
                        "keywordstring": {
                            "type": "text",
                            "analyzer": "keyword_analyzer"
                        },
                        "completion": {
                            "type": "completion"
                        }
                    },
                    "analyzer": "standard"
                },
                "fourth": {
                    "type": "text",
                    "fields": {
                        "keywordstring": {
                            "type": "text",
                            "analyzer": "keyword_analyzer"
                        },
                        "completion": {
                            "type": "completion"
                        }
                    },
                    "analyzer": "standard"
                }
            }
        }
    }
}  

Java代码:

CreateIndexRequest indexRequest = new CreateIndexRequest(ESClientConfiguration.INDEX_NAME);
        URL url = Resources.getResource(TERYT_INDEX_CONFIGURATION_FILE_NAME);

        return Try.of(() -> Resources.toString(url, Charsets.UTF_8))
                  .map(jsonIndexConfiguration -> indexRequest.source(jsonIndexConfiguration, XContentType.JSON))
                  .get();

createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2));

        Try.of(() -> client.indices().create(createIndexRequest, RequestOptions.DEFAULT))...

索引已创建,但是当我查看索引元数据时,它看起来完全错误:

{
    "state": "open",
    "settings": {
        "index": {
            "creation_date": "1556379012380",
            "number_of_shards": "1",
            "number_of_replicas": "1",
            "uuid": "L5fmkrjeQ6eKmuDyZ3MP3g",
            "version": {
                "created": "7000099"
            },
            "provided_name": "my_index"
        }
    },
    "mappings": {
        "my_type": {
            "properties": {
                "first": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                },
                "second": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                },
                "third": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                },
                "fourth": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    },
    "aliases": [],
    "primary_terms": {
        "0": 1
    },
    "in_sync_allocations": {
        "0": [
            "Cx6tBeohR8mzbTO74dwsCw",
            "FcTUhpb_SL2LiaEyy_uwkg"
        ]
    }
}

只有1个没有副本的分片,我也没有看到有关completion类型的任何信息。有人可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:1)

我认为这一行:

    Try.of(() -> client.indices().create(createIndexRequest, RequestOptions.DEFAULT))...

隐藏了一个重要的例外。 在这里,您使用的是Elasticsearch 7.0.0,它不再允许在映射中提供“类型”名称。

代替

"mappings": {
    "my_type": {
        "properties": {

您应该写:

"mappings": {
    "properties": {

由于该异常以及可能在创建索引之后就对某些文档建立索引这一事实,因此将应用默认的索引设置和映射。 可以解释您所看到的内容。

您需要先修复索引设置。 我建议在Kibana开发人员控制台中进行此操作。