Skip to content

Instantly share code, notes, and snippets.

@TimSin
Created August 5, 2013 10:16
Show Gist options
  • Save TimSin/6154882 to your computer and use it in GitHub Desktop.
Save TimSin/6154882 to your computer and use it in GitHub Desktop.
Elasticsearch Java create mapping
Client client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(
"localhost", 9300));
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("mappings")
.startObject("testobject")
.startObject("properties")
.startObject("key")
.field("type", "string")
.field("search_analyzer", "standard")
.field("index_analyzer", "partial_filename_analyzer")
.endObject()
.startObject("description")
.field("type", "string")
.endObject()
.startObject("owningSiteId")
.field("type", "string")
.endObject()
.endObject()
.endObject()
.endObject()
.endObject();
XContentBuilder index = XContentFactory.jsonBuilder()
.startObject()
.startObject("index")
.startObject("analysis")
.startObject("filter")
.startObject("partial_filename")
.field("max_gram", 2)
.field("min_gram", 250)
.field("type", "ngram")
.endObject()
.endObject()
.endObject()
.startObject("analyzer")
.startObject("partial_filename_analyzer")
.array("filter", new String[]{"standard", "lowercase", "asciifolding", "partial_filename"})
.field("type", "custom")
.field("tokenizer", "standard")
.endObject()
.endObject()
.endObject()
.endObject();
CreateIndexRequestBuilder irb = client.admin().indices().prepareCreate("testindex");
irb.setSettings(index);
irb.addMapping("testobject", mapping);
irb.execute().actionGet();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment