6

I m trying to create an index in ES with specific analyzer and mapping, using JEST.

I m using the following code:

 CreateIndex createIndex =  new CreateIndex.Builder(indexName)
    .settings(
            ImmutableSettings.builder()
                    .loadFromClasspath(
                            "jestconfiguration.json"
                    ).build().getAsMap()
    ).build();
    
    JestResult result = client.execute(createIndex);

And this is the jestconfiguration.java

{
  "settings": {
    "analysis": {
      "analyzer": {
        "second": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "synonym"
          ]
        }
      },
      "filter": {
        "synonym" : {
            "type" : "synonym",
            "synonyms" : [
                "smart phone => smartphone"
                ]             
                    }
                }
        }
  },
    "mappings": {
    "index_type": {
      "properties": {
        "Name": {
          "type": "string",
          "analyzer": "second"
        }
      }
    }
  }
}

While, the index is crated correctly with the specified "settings", the "mappings "section does not work and I m not able to set the mapping for the field "Name". Anybody has an idea?

Is there are a sort of putmapping() in jest that lets you add the mappings? Ideally I d like to be able to set the field_name dynamically and not in the .json file.

1 Answer 1

1

I found your question while trying to see if I could create an index and mappings in one go. I ended up just creating a second request for creating mappings.

    String mappingJson = new String(ByteStreams.toByteArray(mappingFile.getInputStream()));
    boolean indexExists = client.execute(new IndicesExists.Builder(configuration.getIndex()).build()).isSucceeded();
    if (indexExists) {
        logger.info("Updating elasticsearch type mapping.");
        client.execute(new PutMapping.Builder(configuration.getIndex(), configuration.getBaseType(), mappingJson).build());
    } else {
        logger.info("Creating elasticsearch index and type mapping.");
        client.execute(new CreateIndex.Builder(configuration.getIndex()).build());
        client.execute(new PutMapping.Builder(configuration.getIndex(), configuration.getBaseType(), mappingJson).build());
    }
Sign up to request clarification or add additional context in comments.

Comments

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.