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?

2
  • To map the field item as keyword, maybe try this: Put Mapping (But only new indices created will respect this mapping) Commented Nov 30, 2017 at 18:40
  • Please check this answer: Link Commented Dec 19, 2017 at 10:22

3 Answers 3

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"
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

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.