Elasticsearch-js: Setting settings?

Created on 8 Sep 2014  路  15Comments  路  Source: elastic/elasticsearch-js

How can we use elasticsearch-js to set settings?

I assumed that this would work

var settings = {
    "index":{
        "analysis":{
            "analyzer":{
                "inurl_analyzer": {
                    "tokenizer":"ngram",
                    "filter":"lowercase"
                }
            },
            "tokenizer":{
                "ngram": {
                    "type": "ngram",
                    "min_gram": 3,
                    "max_gram": 50
                }
            }
        }
    }
};
client.indices.putSettings(settings, ...)

But then we get an error:

TypeError: Invalid index: expected be a comma seperated list, array, number or string.

When, this works flawlessly:

curl -X PUT "http://localhost:9200/superfeedr" -d '{
    "settings":{
        "index":{
            "analysis":{
                "analyzer":{
                    "inurl_analyzer": {
                        "tokenizer":"ngram",
                        "filter":"lowercase"
                    }
                },
                "tokenizer":{
                    "ngram": {
                        "type": "ngram",
                        "min_gram": 3,
                        "max_gram": 50
                    }
                }
            }
        }
    }
}'

Most helpful comment

Ok, for posterity what worked was indeed creating the index, with a body:

client.indices.create({index: 'superfeedr', body: settings}

All 15 comments

Take a look at this doc page that describes the common parameters that every method accepts.

The value of your settings object is the intended http body for the request, and so it should be specified as the body: param.

var settings = {...};
client.indices.putSettings({
  body: settings
});

Except that this yields another error:

message: 'IndexMissingException[[_all] missing]' } { error: 'IndexMissingException[[_all] missing]'

I admit that I didn't check the body of your request for errors. The syntax for everything under the body: key is dictated by Elasticsearch and defined here.

That particular error makes it sound like you don't have any indices... is that the case?

Well yes, but the curl request _works_ even without any index previously created. Is there a way to achieve the same thing with JS or should I add another request to create the index first, then, update it?

Also, creating the index before makes it impossible to update the setings:

ElasticsearchIllegalArgumentException[Can\u0027t update non dynamic settings[[index.analysis.analyzer.inurl_analyzer.filter, index.analysis.tokenizer.ngram.type, index.analysis.tokenizer.ngram.max_gram, index.analysis.analyzer.inurl_analyzer.tokenizer, index.analysis.tokenizer.ngram.min_gram]] for open indices[[superfeedr]]]

The curl request is different because it specifies an index name. I expect you will get the same result if you tell the js client the index to work with:

client.indices.putSettings({
  index: 'superfeedr',
  body: settings
});

You can specify the settings for the index at creation time. See the second example here.

Nope.. that did not work either :

client.indices.putSettings({
  index: 'superfeedr',
  body: settings
});

{ message: 'IndexMissingException[[superfeedr] missing]' } { error: 'IndexMissingException[[superfeedr] missing]',

As a matter of facts, it fails because elasticsearch-js generates the following url: -> PUT http://localhost:9200/superfeedr/_settings

Ok, for posterity what worked was indeed creating the index, with a body:

client.indices.create({index: 'superfeedr', body: settings}

Oh my, I totally missed what was actually happening in the curl call!

The curl call is creating an index, not trying to update the settings on an existing index. Check the example I previously linked and try something like:

client.indices.create({
  index: 'superfeedr',
  body: {
    settings: ....
  }
});

:thumbsup:

Github should allow re-arraging of threads, but yes, that worked (eventually)!
As a side note, I think the elasticsearch-js docs and examples are pretty awful. I understand the value of using manifests to generate wrappers and docs and example, but that usually yields very low quality code, docs and examples :(

Have any suggestions for making it better? Happy to do what I can to make the experience better.

If you prefer, I'd be happy to have this conversation in a new ticket or via email: spencer.[email protected]

I'll send you an email with some thoughts in the coming days!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sahas- picture sahas-  路  22Comments

coryarmbrecht picture coryarmbrecht  路  10Comments

tvarghese picture tvarghese  路  19Comments

kelaban picture kelaban  路  14Comments

simon-tannai picture simon-tannai  路  11Comments