javajsonelasticsearchindexingsearch-engine

Elasticsearch Java API Set index settings


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

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:

@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;
}


}

Solution

  • 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)

    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();