1

I have created an ElasticSearch index using the ElasticSearch Java API. Now I would like to perform some aggregations on data stored in this index, but I get the following error:

      "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [item] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."

As suggested at this link, to solve this issue I should enable fielddata on the "item" text field, but how can I do that using the ElasticSearch Java API?

An alternative might be mapping the "item" field as a keyword, but same question: how can I do that with the ElasticSearch Java API?

user3352382
  • 343
  • 2
  • 5
  • 12
  • To map the field `item` as keyword, maybe try this: [Put Mapping](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-admin-indices.html#java-admin-indices-put-mapping) (But only new indices created will respect this mapping) – kgf3JfUtW Nov 30 '17 at 18:40
  • Please check this answer: [Link](https://stackoverflow.com/a/47884701/1012497) – nikli Dec 19 '17 at 10:22

3 Answers3

1

For a new index you can set the mappings at creation time by doing something like:

CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
String source = // probably read the mapping from a file
createIndexRequest.source(source, XContentType.JSON);
restHighLevelClient.indices().create(createIndexRequest);

The mapping should have the same format as the request you can do against the rest endpoint similar to this:

{
  "mappings": {
    "your_type": {
      "properties": {
        "your_property": {
          "type": "keyword"
        }
      }
    }
  }
}
1

Use XContentBuilder, easy to create json string to create or update mapping. It's seem like

.startObject("field's name")
    .field("type", "text")
    .field("fielddata", true)
.endObject()

After just use IndexRequest to create new indices or use PutMappingRequest to update old mapping

0

I have recently encountered this issue but in my case, I faced this issue when I was performing the Sorting. I have posted a working solution for this problem in another similar question -> set field data = true on java elasticsearch

Prasanth Rajendran
  • 4,570
  • 2
  • 42
  • 59