Elasticsearch Java Api设置索引设置

时间:2021-05-30 04:14:49

Hello Elasticsearchfriends.

你好Elasticsearchfriends。

I'm struggling about the search-by-typing analyser. I have a NodeFactory and there I create a node. Than in my IndexService class, I index a whole json file wich has some data. I'm not an Elasticsearch pro and that's why I wanna know, how I can add settings to my Index or if I have to add the settings to the node.

我正在努力寻找按字母搜索的分析器。我有一个NodeFactory,我在那里创建一个节点。比我的IndexService类,我索引一个包含一些数据的整个json文件。我不是Elasticsearch pro,这就是为什么我想知道,如何向我的索引添加设置或者是否必须将设置添加到节点。

NodeFactory.java

NodeFactory.java

@Component("es")
public class ElasticSearchNodeFactory implements FactoryBean<Node> {
private Node node;

public ElasticSearchNodeFactory() {
    System.out.println("hallo");
}

@Override
public Node getObject() throws Exception {
    return getNode();
}

@Override
public Class getObjectType() {
    return Node.class;
}

@Override
public boolean isSingleton() {
    return true;
}

private Node getNode() throws Exception {

    ImmutableSettings.Builder meineSettings = ImmutableSettings.settingsBuilder();
    meineSettings.put("node.name", "orange11-node");
    meineSettings.put("path.data", "/Users/lucaarchidiacono/IdeaProjects/moap2/MP3_MoapSampleBuild/data/index");
    meineSettings.put("index.store.type", "memory");
    meineSettings.put("http.enabled", false);
    Settings setting = meineSettings.build();
    node = NodeBuilder.nodeBuilder().local(true).data(true).clusterName("orange11-cluster").settings(setting).node();
    return node;
}
}

IndexService class:

IndexService类:

@Service
public class IndexService {
private Node node;
private Client client;

@Autowired
public IndexService(Node node) throws Exception {
    this.node = node;

    client = this.node.client();


    List<Map<String, Object>> data = jsonToMap();
    for (int i = 0; i < data.size(); ++i) {
        Map<String, Object> object = data.get(i);

        IndexRequest indexRequest = Requests.indexRequest("orange11").type("profile").id(Integer.toString(i)).source(object);
        IndexResponse indexResponse = client.index(indexRequest).actionGet();

        if (indexResponse != null && indexResponse.isCreated()) {
            System.out.println("Index has been created !");
            // read report from response
            System.out.println("------------------------------");
            System.out.println("Index name: " + indexResponse.getIndex());
            System.out.println("Type name: " + indexResponse.getType());
            System.out.println("ID: " + indexResponse.getId());
            System.out.println("Version: " + indexResponse.getVersion());
            System.out.println("------------------------------");
        } else {
            System.err.println("Index creation failed.");
        }
    }

}

public List<Map<String, Object>> jsonToMap() throws IOException {

    ObjectMapper mapper = new ObjectMapper();

    List<Map<String, Object>> listOfMaps = new ArrayList<Map<String, Object>>();

    Map<String, Object>[] jsonDocument = mapper.readValue(new File("/Users/lucaarchidiacono/IdeaProjects/moap2/MP3_MoapSampleBuild/data/index/accounts.json"), new TypeReference<HashMap<String, Object>[]>() {});

    for (int i = 0; i < jsonDocument.length; i++) {

        listOfMaps.add(i, jsonDocument[i]);
    }

    return listOfMaps;
}


}

1 个解决方案

#1


1  

Here is how you can add setting to embedded note.

以下是如何为嵌入式笔记添加设置。

Node node =
    nodeBuilder()
        .settings(ImmutableSettings.settingsBuilder().put("http.enabled", false))
        .client(true)
    .node();

You can get the client for that embedded node as follows. (If you have the reference to Node object)

您可以按如下方式获取该嵌入式节点的客户端。 (如果您有Node对象的引用)

Client client = node.client();

If not you can simply create new transport client to make queries.

如果不是,您只需创建新的传输客户端即可进行查询。

Client client = new TransportClient()
        .addTransportAddress(new InetSocketTransportAddress("host1", 9300))
        .addTransportAddress(new InetSocketTransportAddress("host2", 9300));

Then you can give setting to your index at creation as follows.

然后,您可以在创建时为索引设置,如下所示。

IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
        .setSource(jsonBuilder()
                    .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                    .endObject()
                  )
        .setSettings(ImmutableSettings.settingsBuilder().put("**whatever_setting_key**", **whatever_setting_value**))
        .execute()
        .actionGet();.

EDIT:

编辑:

You can use the following.

您可以使用以下内容。

String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";

// add your setting json here
String setting_json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";

IndexResponse response = client.admin().indices().prepareCreate("twitter")
        .setSource(json)
        .setSetting(setting_json)
        .execute()
        .actionGet();

#1


1  

Here is how you can add setting to embedded note.

以下是如何为嵌入式笔记添加设置。

Node node =
    nodeBuilder()
        .settings(ImmutableSettings.settingsBuilder().put("http.enabled", false))
        .client(true)
    .node();

You can get the client for that embedded node as follows. (If you have the reference to Node object)

您可以按如下方式获取该嵌入式节点的客户端。 (如果您有Node对象的引用)

Client client = node.client();

If not you can simply create new transport client to make queries.

如果不是,您只需创建新的传输客户端即可进行查询。

Client client = new TransportClient()
        .addTransportAddress(new InetSocketTransportAddress("host1", 9300))
        .addTransportAddress(new InetSocketTransportAddress("host2", 9300));

Then you can give setting to your index at creation as follows.

然后,您可以在创建时为索引设置,如下所示。

IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
        .setSource(jsonBuilder()
                    .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                    .endObject()
                  )
        .setSettings(ImmutableSettings.settingsBuilder().put("**whatever_setting_key**", **whatever_setting_value**))
        .execute()
        .actionGet();.

EDIT:

编辑:

You can use the following.

您可以使用以下内容。

String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";

// add your setting json here
String setting_json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";

IndexResponse response = client.admin().indices().prepareCreate("twitter")
        .setSource(json)
        .setSetting(setting_json)
        .execute()
        .actionGet();