Issue while creating custom analyzers for index

I'm trying to add a tokenizer and three analyzers to my index at creation time.
Here's the code:

client.admin().indices().prepareCreate(indexName).setSettings(Settings.builder().loadFromSource(jsonBuilder()
			        .startObject()
				        .startObject("analysis")
					        .startObject("tokenizer")
						        .startObject("username_tokenizer")
				                    .field("type", "nGram")
				                    .field("min_gram", 1)
				                    .field("max_gram", 80)
				                .endObject()
					        .endObject()
				            .startObject("analyzer")
				                .startObject("string_lowercase")
				                    .field("type", "custom")
				                    .field("tokenizer", "keyword")
				                    .field("filter", new String[]{"lowercase"})
				                .endObject()
				            .endObject()
				            .startObject("analyzer")
				                .startObject("email")
				                    .field("type", "custom")
				                    .field("tokenizer", "uax_url_email")
				                    .field("filter", new String[]{"standard","lowercase","stop"})
				                .endObject()
				            .endObject()
				            .startObject("analyzer")
				                .startObject("username")
				                    .field("type", "custom")
				                    .field("tokenizer", "username_tokenizer")
				                    .field("filter", new String[]{"standard","lowercase"})
				                .endObject()
				            .endObject()
				        .endObject()
				    .endObject().string(), XContentType.JSON))
			.execute().actionGet();

I'm getting the following error:

Caused by: com.fasterxml.jackson.core.JsonParseException: Duplicate field 'analyzer'
 at [Source: {"analysis":{"tokenizer":{"username_tokenizer":{"type":"nGram","min_gram":1,"max_gram":80}},"analyzer":{"string_lowercase":{"type":"custom","tokenizer":"keyword","filter":["lowercase"]}},"analyzer":{"email":{"type":"custom","tokenizer":"uax_url_email","filter":["standard","lowercase","stop"]}},"analyzer":{"username":{"type":"custom","tokenizer":"username_tokenizer","filter":["standard","lowercase"]}}}}; line: 1, column: 198]
	at com.fasterxml.jackson.core.json.JsonReadContext._checkDup(JsonReadContext.java:197) ~[jackson-core-2.6.2.jar:2.6.2]
	at com.fasterxml.jackson.core.json.JsonReadContext.setCurrentName(JsonReadContext.java:192) ~[jackson-core-2.6.2.jar:2.6.2]
	at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:625) ~[jackson-core-2.6.2.jar:2.6.2]
	at org.elasticsearch.common.xcontent.json.JsonXContentParser.nextToken(JsonXContentParser.java:52) ~[elasticsearch-6.1.0.jar:6.1.0]
	at org.elasticsearch.common.settings.Settings.fromXContent(Settings.java:697) ~[elasticsearch-6.1.0.jar:6.1.0]
	at org.elasticsearch.common.settings.Settings.fromXContent(Settings.java:703) ~[elasticsearch-6.1.0.jar:6.1.0]
	at org.elasticsearch.common.settings.Settings.fromXContent(Settings.java:672) ~[elasticsearch-6.1.0.jar:6.1.0]
	at org.elasticsearch.common.settings.Settings.access$500(Settings.java:81) ~[elasticsearch-6.1.0.jar:6.1.0]
	at org.elasticsearch.common.settings.Settings$Builder.loadFromSource(Settings.java:1148) ~[elasticsearch-6.1.0.jar:6.1.0]
	... 10 common frames omitted

I suspect it may have something to do with creating multiple analyzers in the same JSON object. Is there a way to do this, or should I send each analyzer individually after creating the index? If so, what is the syntax to do this?

It's wrong. You should write something like:

				            .startObject("analyzer")
				                .startObject("analyzer1")
				                    .field("type", "custom")
				                    .field("tokenizer", "keyword")
				                    .field("filter", new String[]{"lowercase"})
				                .endObject()
				                .startObject("analyzer2")
				                    .field("type", "custom")
				                    .field("tokenizer", "keyword")
				                    .field("filter", new String[]{"lowercase"})
				                .endObject()
				                .startObject("analyzer3")
				                    .field("type", "custom")
				                    .field("tokenizer", "keyword")
				                    .field("filter", new String[]{"lowercase"})
				                .endObject()
				            .endObject()

HTH

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.