Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add mapping in ElasticSearch using JEST

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.

like image 488
panipsilos Avatar asked Apr 20 '16 14:04

panipsilos


1 Answers

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());
    }
like image 144
Alex Pruss Avatar answered Nov 15 '22 23:11

Alex Pruss