0

我正在尝试使用以下 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 字段被索引为字符串,而不是日期。现在,如果我再看看

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 字段按预期编制索引。为什么会这样?

请指教。

4

0 回答 0