3

I am using ElasticSearch 1.5.2 and I wish to have the following settings :

"settings": {
"analysis": {
    "filter": {
        "filter_shingle": {
            "type": "shingle",
            "max_shingle_size": 2,
            "min_shingle_size": 2,
            "output_unigrams": false
        },
        "filter_stemmer": {
            "type": "porter_stem",
            "language": "English"
        }
    },
    "tokenizer": {
        "my_ngram_tokenizer": {
            "type": "nGram",
            "min_gram": 1,
            "max_gram": 1
        }
    },
    "analyzer": {
        "ShingleAnalyzer": {
            "tokenizer": "my_ngram_tokenizer",
            "filter": [
                "standard",
                "lowercase",
                "filter_stemmer",
                "filter_shingle"
            ]
        }
    }
  }
}    

Where should I add them? I mean before index creation or after?

By searching online I found some method like

client.admin().indices().prepareCreate("temp_index").setSettings(ImmutableSettings.settingsBuilder().loadFromSource((jsonBuilder()
    .startObject()
       .startObject("analysis")
          .startObject("analyzer")......and so on)

But I am having 2 issues,

  1. I get a compile error : The method loadFromSource in the type ImmutableSettings.builder is not applicable for the arguments XContentBuilder

  2. Also, I don't know how to convert my settings to this format. And where is the documentation to get to know about all these methods? I tried reading the official ElasticSearch JAVA API https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/search.html but I was unable to find anything about setting up analyzers. All the related content I find is only in the form of REST APIs and not in Java API.

3
  • So, do you want to add setting with java api? Or Direct using REST ? Commented May 25, 2015 at 10:46
  • I want to add using java api. Commented May 25, 2015 at 10:48
  • If you could provide a link for java api documentation for analyzers, it would be very useful. Commented May 25, 2015 at 10:55

1 Answer 1

4

XContentBuilder have specific syntax , can be used to create a json object.

You can follow https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index_.html#helpers to convert your string to XContentBuilder format.

If you have same string loaded in application. You simply can do.

    client.admin().indices()
.prepareCreate("index_name").setSettings("{setting _ json _ string }").get()

Will simply work.

If you want to know how the json will look like in XContentBuilder, then here it is,

XContentBuilder settingsBuilder = XContentFactory.jsonBuilder()
        .startObject()
            .startObject("analysis")
                .startObject("filter")
                    .startObject("filter_shingle")
                        .field("type","shingle")
                        .field("max_shingle_size",2)
                        .field("min_shingle_size",2)
                        .field("output_unigrams",false)
                    .endObject()
                    .startObject("filter_stemmer")
                        .field("type","porter_stem")
                        .field("language","English")
                    .endObject()
                .endObject()
                .startObject("tokenizer")
                    .startObject("my_ngram_tokenizer")
                        .field("type","nGram")
                        .field("min_gram",1)
                        .field("max_gram",1)
                    .endObject()
                .endObject()
                .startObject("analyzer")
                    .startObject("ShingleAnalyzer")
                        .field("tokenizer","my_ngram_tokenizer")
                        .array("filter","standard","lowercase","filter_stemmer","filter_shingle")
                    .endObject()
                .endObject()
            .endObject()
        .endObject()

client.admin().indices()
.prepareCreate("index_name").setSettings(settingsBuilder).get()
Sign up to request clarification or add additional context in comments.

4 Comments

Can you please provide the source from where you got this client.admin().indices() .prepareCreate("index_name").setSettings("{setting _ json _ string }").get() ?
source ? If you are asking me about documentation, then I am looking at elasticsearch code.
as in the original elasticsearch source code? No documentation available for it??
I am sorry, I don't think there is proper documentation of java api, If you look at elasticsearch client , org.elasticsearch.client.Client class, then you can find out what methods are available.

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.