26

Is there a way to create an index and specify a custom analyzer using the Java API? It supports adding mappings at index creation, but I can't find a way to do something like this without sending the JSON via HTTP PUT:

curl -XPUT localhost:9200/twitter?pretty=true -d '{
"analysis": {
       "analyzer": {
            "steak" : {
                "type" : "custom", 
                "tokenizer" : "standard",
                "filter" : ["snowball", "standard", "lowercase"]
            }
        }
    }
}'

I can build such a query using JSONBuilder, but I can't find no place in the API where to run it, CreateIndexRequest doesn't have anything I can use and neither does client.admin().indices(), as far as I can see. What's the right way to do this?

2 Answers 2

29

You can set analyzer using client.admin().indices().prepareCreate("twitter").setSettings(...). There are several ways to build settings. You can load them from text, map or even use jsonBuilder if that's what you want:

client.admin().indices().prepareCreate("twitter")
            .setSettings(Settings.settingsBuilder().loadFromSource(jsonBuilder()
                .startObject()
                    .startObject("analysis")
                        .startObject("analyzer")
                            .startObject("steak")
                                .field("type", "custom")
                                .field("tokenizer", "standard")
                                .field("filter", new String[]{"snowball", "standard", "lowercase"})
                            .endObject()
                        .endObject()
                    .endObject()
                .endObject().string()))
            .execute().actionGet();
Sign up to request clarification or add additional context in comments.

2 Comments

when I add my analyzer to a field I got this error MapperParsingException[Mapping definition for [value] has unsupported parameters: [analyzer : delimeter_analyzer]]
for anyone else having the same stupid issue as me (in the new Java API v7.6), the settings json object that you pass must start with "analysis", not "settings"...
1

If you are on a test environnent you can also uses this project which will create your indexes based on Java annotations. https://github.com/tlrx/elasticsearch-test

1 Comment

Nice Work. I really miss in the JAVA ES api POJO mappings for index objects....(( C# has some nice projects , like Nest that implement that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.