从Java添加映射到类型 - 我该怎么做?

And*_*sen 43 java elasticsearch

我试图或多或少地使用Java API 重新创建此示例.

我认为我只需要为索引添加映射,但Java API文档并不清楚如何执行此操作.

请告诉我如何在Java中创建一个与文档中的示例等效的映射:

curl -X PUT localhost:9200/test/tweet/_mapping -d '{
    "tweet" : {
        "_ttl" : { "enabled" : true, "default" : "1d" }
    }
}'
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

package foo;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;

public class MyTestClass {

    private static Client getClient() {
        ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
        TransportClient transportClient = new TransportClient(settings);
        transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
        return (Client) transportClient;
    }

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

        final Client client = getClient();
        // Create Index and set settings and mappings
        final String indexName = "test";
        final String documentType = "tweet";
        final String documentId = "1";
        final String fieldName = "foo";
        final String value = "bar";

        IndicesExistsResponse res =  client.admin().indices().prepareExists(indexName).execute().actionGet();
        if (res.isExists()) {
            DeleteIndexRequestBuilder delIdx = client.admin().indices().prepareDelete(indexName);
            delIdx.execute().actionGet();
        }

        CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName);

        // MAPPING GOES HERE

//      createIndexRequestBuilder.addMapping(documentType, WHATEVER THE MAPPING IS);

        // MAPPING DONE
        createIndexRequestBuilder.execute().actionGet();

        // Add documents
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, documentType, documentId);
        // build json object
        XContentBuilder contentBuilder = jsonBuilder().startObject().prettyPrint();
        contentBuilder.field(fieldName, value);

        indexRequestBuilder.setSource(contentBuilder);
        indexRequestBuilder.execute().actionGet();

        // Get document
        System.out.println(getValue(client, indexName, documentType, documentId, fieldName));

        Thread.sleep(10000L);

        // Try again
        System.out.println(getValue(client, indexName, documentType, documentId, fieldName));
    }

    protected static String getValue(final Client client, final String indexName, final String documentType,
            final String documentId, final String fieldName) {
        GetRequestBuilder getRequestBuilder = client.prepareGet(indexName, documentType, documentId);
        getRequestBuilder.setFields(new String[] { fieldName });
        GetResponse response2 = getRequestBuilder.execute().actionGet();
        String name = response2.getField(fieldName).getValue().toString();
        return name;
    }

}
Run Code Online (Sandbox Code Playgroud)

And*_*sen 75

最后一天的谷歌搜索得到了回报.坦率地说,弹性搜索的Java API文档可以使用一些端到端的例子,更不用说JavaDoc ......

这是一个运行的例子.您必须在localhost上运行节点才能使其正常工作!

package foo;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;

public class MyTestClass {

    private static final String ID_NOT_FOUND = "<ID NOT FOUND>";

    private static Client getClient() {
        final ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
        TransportClient transportClient = new TransportClient(settings);
        transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
        return transportClient;
    }

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

        final Client client = getClient();
        // Create Index and set settings and mappings
        final String indexName = "test";
        final String documentType = "tweet";
        final String documentId = "1";
        final String fieldName = "foo";
        final String value = "bar";

        final IndicesExistsResponse res = client.admin().indices().prepareExists(indexName).execute().actionGet();
        if (res.isExists()) {
            final DeleteIndexRequestBuilder delIdx = client.admin().indices().prepareDelete(indexName);
            delIdx.execute().actionGet();
        }

        final CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName);

        // MAPPING GOES HERE

        final XContentBuilder mappingBuilder = jsonBuilder().startObject().startObject(documentType)
                .startObject("_ttl").field("enabled", "true").field("default", "1s").endObject().endObject()
                .endObject();
        System.out.println(mappingBuilder.string());
        createIndexRequestBuilder.addMapping(documentType, mappingBuilder);

        // MAPPING DONE
        createIndexRequestBuilder.execute().actionGet();

        // Add documents
        final IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, documentType, documentId);
        // build json object
        final XContentBuilder contentBuilder = jsonBuilder().startObject().prettyPrint();
        contentBuilder.field(fieldName, value);

        indexRequestBuilder.setSource(contentBuilder);
        indexRequestBuilder.execute().actionGet();

        // Get document
        System.out.println(getValue(client, indexName, documentType, documentId, fieldName));

        int idx = 0;
        while (true) {
            Thread.sleep(10000L);
            idx++;
            System.out.println(idx * 10 + " seconds passed");
            final String name = getValue(client, indexName, documentType, documentId, fieldName);
            if (ID_NOT_FOUND.equals(name)) {
                break;
            } else {
                // Try again
                System.out.println(name);
            }
        }
        System.out.println("Document was garbage collected");
    }

    protected static String getValue(final Client client, final String indexName, final String documentType,
            final String documentId, final String fieldName) {
        final GetRequestBuilder getRequestBuilder = client.prepareGet(indexName, documentType, documentId);
        getRequestBuilder.setFields(new String[] { fieldName });
        final GetResponse response2 = getRequestBuilder.execute().actionGet();
        if (response2.isExists()) {
            final String name = response2.getField(fieldName).getValue().toString();
            return name;
        } else {
            return ID_NOT_FOUND;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 我想我发现了怎么做.您可以使用preparePutMapping而不是prepareCreate:PutMappingResponse response = client.admin().indices().preparePutMapping(index).setType(type).setSource(mappingBuilder).execute().actionGet(); (6认同)
  • 是的,文档有点轻,不是吗?谢谢你搞清楚这一点. (4认同)
  • 这非常接近我需要的...除了我还需要能够添加映射到已经存在的索引.任何人? (3认同)

sha*_*i15 8

我实际上要在这里添加另一个答案,因为坦率地说,上面的答案为我的实现提供了一个开始,但没有回答100%的实际问题(不仅更新根级属性,而且更新ACTUAL字段/属性).花了将近2天时间来解决这个问题(文档对于ES Java API来说有点轻松).我的"Mapping"类还不是100%,但可以在以后添加更多字段("格式"等).

我希望这可以帮助其他尝试使用更新映射的人!

获取/检索映射

ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings = response.getMappings();
                    ImmutableOpenMap<String, MappingMetaData> typeMappings = indexMappings.get(indexName);
                    MappingMetaData mapping = typeMappings.get(type);
                    Map<String, Mapping> mappingAsMap = new HashMap<>();
                    try {
                        Object properties = mapping.sourceAsMap().get("properties");
                        mappingAsMap = (Map<String, Mapping>) gson.fromJson(gson.toJson(properties), _elasticsearch_type_mapping_map_type);
                        return mappingAsMap;
                    }
Run Code Online (Sandbox Code Playgroud)

更新映射

PutMappingRequest mappingRequest = new PutMappingRequest(indexName);
            Map<String, Object> properties = new HashMap<>();
            Map<String, Object> mappingsMap = (Map<String, Object>) gson.fromJson(gson.toJson(mapping), Json._obj_map_type);
            properties.put("properties", mappingsMap);
            mappingRequest = mappingRequest.ignoreConflicts(true).type(type).source(properties).actionGet();
Run Code Online (Sandbox Code Playgroud)

我的GSON映射类型

public static final Type _obj_map_type = new TypeToken<LinkedHashMap<String, Object>>(){}.getType();
public static final Type _elasticsearch_type_mapping_map_type = new TypeToken<LinkedHashMap<String, Mapping>>(){}.getType();
Run Code Online (Sandbox Code Playgroud)

我的映射类

public class Mapping {

    private String type;
    private String index;
    private String analyzer;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public String getAnalyzer() {
        return analyzer;
    }

    public void setAnalyzer(String analyzer) {
        this.analyzer = analyzer;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

另一种解决方案是使用称为动态模板的功能.这篇文章在本文中有很好的描述http://joelabrahamsson.com/dynamic-mappings-and-dates-in-elasticsearch/

所以这种情况使用一个正则表达式,声明以tikaprop_开头的任何字段都是String类型.

curl -XPUT "http://localhost:9200/myindex" -d'
    {
       "mappings": {
          "_default_": {
             "date_detection": true,
             "dynamic_templates": [
                {
                   "tikaprops": {
                      "match": "tikaprop_.*",
                      "match_pattern": "regex",
                      "mapping": {
                         "type": "string"
                      }
                   }
                }
             ]
          }
       }
    }'
Run Code Online (Sandbox Code Playgroud)

或者您更愿意通过Elasticsearch Java API来完成它

CreateIndexRequestBuilder cirb = this.client.admin().indices().prepareCreate(INDEX_NAME).addMapping("_default_", getIndexFieldMapping());
CreateIndexResponse createIndexResponse = cirb.execute().actionGet();

private String getIndexFieldMapping() {
return IOUtils.toString(getClass().getClassLoader().getResourceAsStream("elasticsearch_dynamic_templates_config.json"));
}
Run Code Online (Sandbox Code Playgroud)

与elasticsearch_dynamic_templates_config.json beein:

{
     "date_detection": true,
     "dynamic_templates": [
        {
           "tikaprops": {
              "match": "tikaprop_.*",
              "match_pattern": "regex",
              "mapping": {
                 "type": "string"
              }
           }
        }
     ]
   }
Run Code Online (Sandbox Code Playgroud)