6

Problem: How to create an index from a json file using

The json file contains a definition for the index de_brochures. It also defines an analyzer de_analyzerwith custom filters that are used by the respective index. As the json works with curl and Sense I assume I have to adapt the syntax of it to work with the java API.

I don't want to use XContentFactory.jsonBuilder() as the json comes from a file!

I have the following json file to create my mapping from and to set settings:

Using Sense with PUT /indexname it does create an index from this.

{
  "mappings": {
    "de_brochures": {
      "properties": {
        "text": {
          "type": "string",
          "store": true,
          "index_analyzer": "de_analyzer"
        },
        "classification": {
          "type": "string",
          "index": "not_analyzed"
        },
        "language": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  "settings": {
    "analysis": {
      "filter": {
        "de_stopwords": {
          "type": "stop",
          "stopwords": "_german_"
        },
        "de_stemmer": {
          "type": "stemmer",
          "name": "light_german"
        }
      },
      "analyzer": {
        "de_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "de_stopwords",
            "de_stemmer"
          ]
        }
      }
    }
  }
}

As the above did not work with addMapping() alone I tried to split it into two seperate files (I realized that I had to remove the "mappings": and "settings": part):

------ Mapping json ------
{
  "de_brochures": {
    "properties": {
      "text": {
        "type": "string",
        "store": true,
        "index_analyzer": "de_analyzer"
      },
      "classification": {
        "type": "string",
        "index": "not_analyzed"
      },
      "language": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  }
}
------- Settings json --------
{
  "analysis": {
    "filter": {
      "de_stopwords": {
        "type": "stop",
        "stopwords": "_german_"
      },
      "de_stemmer": {
        "type": "stemmer",
        "name": "light_german"
      }
    },
    "analyzer": {
      "de_analyzer": {
        "type": "custom",
        "tokenizer": "standard",
        "filter": [
          "lowercase",
          "de_stopwords",
          "de_stemmer"
        ]
      }
    }
  }
}

This is my java code to load and add/set the json.

CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(index);
// CREATE SETTINGS
String settings_json = new String(Files.readAllBytes(brochures_mapping_path));
createIndexRequestBuilder.setSettings(settings_json);
// CREATE MAPPING
String mapping_json = new String(Files.readAllBytes(brochures_mapping_path));
createIndexRequestBuilder.addMapping("de_brochures", mapping_json);
CreateIndexResponse indexResponse = createIndexRequestBuilder.execute().actionGet();

There is no more complaint about the mapping file's structure but it now fails with the error:

Caused by: org.elasticsearch.index.mapper.MapperParsingException: Analyzer [de_analyzer] not found for field [text]

2 Answers 2

4

Solution: I managed to do it with my original json file using createIndexRequestBuilder.setSource(settings_json);

Sign up to request clarification or add additional context in comments.

1 Comment

Please don't add the solution to the question. Solutions aren't questions. Solutions are answers. Stackoverflow encourages self-answering your questions when you find solutions. But please do that in an answer. Editing the question to include the solution just confuses readers. Thanks!
1

I think the problem is with structure of your mapping file.

Here is a sample example.

mapping.json
{
"en_brochures": {
    "properties": {
        "text": {
            "type": "string",
            "store": true,
            "index_analyzer": "en_analyzer",
            "term_vector": "yes"
        },
        "classification": {
            "type": "string",
            "index": "not_analyzed"
        },
        "language": {
            "type": "string",
            "index": "not_analyzed"
        }
    }
    }
}



String mapping = new String(Files.readAllBytes(Paths.get("mapping.json")));
    createIndexRequestBuilder.addMapping('en_brochures', mapping);
    CreateIndexResponse indexResponse =createIndexRequestBuilder.execute().actionGet();

This works in mine, you can try.

3 Comments

Do you have any idea how I can fit in the "analyzer" part into your json structure for it to work?
Look up! I have changed, my answer. And, for this to work you have to add settings (as you did before)
I assume you already added the en_analyzer? How did you do that? Otherwise it complains about not finding "en_analyzer" for "text".

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.