Elasticsearch Java API addMapping()和setSettings()用法

Pet*_*ter 6 java mapping json elasticsearch

问题:如何使用json文件创建索引

json文件包含索引的定义de_brochures.它还定义了一个分析器de_analyzer,其中包含相应索引使用的自定义过滤器.由于json使用curl和Sense,我假设我必须调整它的语法来使用java API.

我不想使用XContentFactory.jsonBuilder(),因为json来自一个文件!

我有以下json文件来创建我的映射和设置设置:

使用Sense和PUT/indexname,它确实从中创建了一个索引.

{
  "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"
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

由于以上不能单独使用addMapping(),我试图将其拆分为两个单独的文件(我意识到我必须删除它"mappings":"settings":部分):

------ 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"
        ]
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我加载和添加/设置json的java代码.

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();
Run Code Online (Sandbox Code Playgroud)

没有关于映射文件结构的抱怨,但它现在失败并出现错误:

Caused by: org.elasticsearch.index.mapper.MapperParsingException: Analyzer [de_analyzer] not found for field [text]
Run Code Online (Sandbox Code Playgroud)

Pet*_*ter 4

解决方案: 我设法使用原始 json 文件来完成此操作createIndexRequestBuilder.setSource(settings_json);