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

时间:2015-03-29 15:31:00

标签: java json elasticsearch mapping

问题:如何使用

从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"
          ]
        }
      }
    }
  }
}

由于以上单独使用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"
        ]
      }
    }
  }
}

这是我加载和添加/设置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();

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

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

2 个答案:

答案 0 :(得分:4)

<强>解决方案: 我设法使用createIndexRequestBuilder.setSource(settings_json);

使用我原来的json文件

答案 1 :(得分:1)

我认为问题在于您的映射文件的结构。

以下是一个示例。

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();

这在我的作品中有效,你可以试试。