使用JSON模式和ElasticSearch Java API添加类型映射 [英] Add type mapping with JSON schema and ElasticSearch Java API

查看:709
本文介绍了使用JSON模式和ElasticSearch Java API添加类型映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用Java API将一个类型映射添加到ElasticSearch 索引 JSON 架构?

Is it possible to add a type mapping to an ElasticSearch Index with the Java API using a JSON schema?

我知道ElasticSearch使用第一个文档来创建一个映射,因此我可以使用 json 架构来增强我的第一个文档。

I know that ElasticSearch uses the first document to create a mapping and therefor i could enhance my first document with json schema. But i want to create the types before indexing an document.

推荐答案

您可以执行以下操作:

You could do something like:

String mapping = XContentFactory.jsonBuilder().startObject().startObject(typeName).startObject("properties")
                    .startObject("location").field("type", "geo_point").endObject()
                    .startObject("language").field("type", "string").field("index", "not_analyzed").endObject()
                    .startObject("user").startObject("properties").startObject("screen_name").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject()
                    .startObject("mention").startObject("properties").startObject("screen_name").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject()
                    .startObject("in_reply").startObject("properties").startObject("user_screen_name").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject()
                    .startObject("retweet").startObject("properties").startObject("user_screen_name").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject()
                    .endObject().endObject().endObject().string();
client.admin().indices().preparePutMapping(indexName).setType(typeName).setSource(mapping).execute().actionGet();

或者如果您的映射为String

Or if you have your mapping as a String

String json = "{}";
PutMappingResponse response = client.admin().indices()
                    .preparePutMapping(index)
                    .setType(type)
                    .setSource(json)
                    .execute().actionGet();     

这篇关于使用JSON模式和ElasticSearch Java API添加类型映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆