弹出搜索索引定义被覆盖

时间:2015-09-10 20:45:27

标签: rest elasticsearch elasticsearch-java-api

我正在尝试使用以下Java代码

创建弹性搜索索引
    public class MappingCreator {

    static Logger log = Logger.getLogger(MappingCreator.class.getName());

    final static String indexName =  "indxyz";

    final static String typeName = "indtype";

    final static String mappingFileName = "customMapping.json";

    final static String clusterName = "elasticsearch";

    final static String hostName = "localhost";

    public static void main(String args[]) throws IOException
    {

        MappingCreator mapCreator = new MappingCreator();

        Client myESclient = getClient();

        IndicesExistsResponse res = myESclient.admin().indices().prepareExists(indexName).execute().actionGet();

        if (res.isExists()) {

            log.warn("Index "+indexName +" already exists. Will be deleted");

            final DeleteIndexRequestBuilder deleteIndexBuilder = myESclient.admin().indices().prepareDelete(indexName);

            deleteIndexBuilder.execute().actionGet();
        }

        final CreateIndexRequestBuilder createIndexBuilder = myESclient.admin().indices().prepareCreate(indexName)
                .addMapping(typeName, mapCreator.getIndexFieldMapping());

        CreateIndexResponse createIndexResponse = createIndexBuilder.execute().actionGet();

        log.debug("Created mapping "+createIndexResponse.toString());

        myESclient.close();

    }

    private String getIndexFieldMapping() throws IOException {

        return IOUtils.toString(getClass().getClassLoader().getResourceAsStream(mappingFileName));
    }

    private static Client getClient() {

        TransportClient transportClient = null;

        try
        {
            Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).build();

            transportClient = new TransportClient(settings);

            transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress(hostName, 9300));

        }
        catch (Exception e)
        {
            log.error("Error in MappingCreator creating Elastic Search Client\n"
                    + "Message "+e.getMessage()+"\n"
                            + "StackTrace "+e.getStackTrace()
                    );
        }

        return (Client) transportClient;

    }

}

customMapping.json看起来像这样

 {

                "properties": {

                   "popkDate": {

                      "type":"date",
                      "format": "yyyy-MM-dd 'T' HH:mm:ss.SSZ"
                   },

                   "popk": {
                      "type": "string"
                   }
                }
    }

现在,在创建映射后,如果我在elasticsearch marvel插件中运行以下内容

get indxyz/

我得到了

 {
   "indxyz": {
      "aliases": {},
      "mappings": {
         "indtype": {
            "properties": {
               "popkDate": {
                  "type": "date",
                  "format": "yyyy-MM-dd 'T' HH:mm:ss.SSZ"
               },
               "popk": {
                  "type": "string"
               }
            }
         }
       }

这是准确的。

现在当我尝试摄取这些数据时

  {
        "popkDate":"2001-01-01T235959.00-0400",
        "popk":"cuinbcwenvubn"
    }

由于某种原因,popkDate字段被索引为String,而不是Date。现在,如果我再看看

get indxyz/

这次我得到了,

{
       "indxyz": {
          "aliases": {},
          "mappings": {
             "indtype": {
                "properties": {
                   "popkDate": {
                      "type": "date",
                      "format": "yyyy-MM-dd 'T' HH:mm:ss.SSZ"
                   },
                   "popk": {
                      "type": "string"
                   }
                }
             },

           "indtype": {
                "properties": {
                   "popkDate": {
                      "type": "string"
                    },
                   "popk": {
                      "type": "string"
                   }
                }
             }
           }

为什么要创建这个重复的映射?对于同一类型?请注意,如果我创建没有JAVA api的映射,即通过curl命令,则不会观察到此行为,并且popkDate字段将按预期编制索引。为什么会这样?

请告知。

0 个答案:

没有答案