2016-04-20 29 views
5

我試圖在ES中使用JEST創建具有特定分析器和映射的索引。 我米使用以下代碼:如何在使用JEST的ElasticSearch中添加映射

CreateIndex createIndex = new CreateIndex.Builder(indexName) 
    .settings(
      ImmutableSettings.builder() 
        .loadFromClasspath(
          "jestconfiguration.json" 
        ).build().getAsMap() 
    ).build(); 

    JestResult result = client.execute(createIndex); 

而這正是jestconfiguration.java

{ 
    "settings": { 
    "analysis": { 
     "analyzer": { 
     "second": { 
      "type": "custom", 
      "tokenizer": "standard", 
      "filter": [ 
      "lowercase", 
      "synonym" 
      ] 
     } 
     }, 
     "filter": { 
     "synonym" : { 
      "type" : "synonym", 
      "synonyms" : [ 
       "smart phone => smartphone" 
       ]    
        } 
       } 
     } 
    }, 
    "mappings": { 
    "index_type": { 
     "properties": { 
     "Name": { 
      "type": "string", 
      "analyzer": "second" 
     } 
     } 
    } 
    } 
} 

雖然,索引與所指定的「設置」正確裝箱中,「映射」部分不工作,我無法設置字段「名稱」的映射。任何人有想法? 在JESt中是否有一種putmapping()可讓您添加映射?理想情況下,我希望能夠動態設置field_name,而不是在.json文件中。

Thnks

回答

0

,我發現你的問題,而試圖看看我是否能一氣呵成創建索引和映射。我最終創建了第二個創建映射的請求。

String mappingJson = new String(ByteStreams.toByteArray(mappingFile.getInputStream())); 
    boolean indexExists = client.execute(new IndicesExists.Builder(configuration.getIndex()).build()).isSucceeded(); 
    if (indexExists) { 
     logger.info("Updating elasticsearch type mapping."); 
     client.execute(new PutMapping.Builder(configuration.getIndex(), configuration.getBaseType(), mappingJson).build()); 
    } else { 
     logger.info("Creating elasticsearch index and type mapping."); 
     client.execute(new CreateIndex.Builder(configuration.getIndex()).build()); 
     client.execute(new PutMapping.Builder(configuration.getIndex(), configuration.getBaseType(), mappingJson).build()); 
    } 
相關問題