0

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


}
7
  • 1
    what is the elasticsearch version ? Commented Feb 15, 2017 at 16:42
  • It's Elasticsearchversion 1.7.0 Commented Feb 15, 2017 at 16:43
  • 1
    First thing is that you should look into a newer version of elasticsearch. I think elasticsearch 1.x is reaching end of life. You should try to use at least 2.x version. Commented Feb 15, 2017 at 16:46
  • 1
    Are you trying to use an embedded elasticsearch node ? or do you have an standalone elasticsearch server ? Commented Feb 15, 2017 at 16:49
  • I work in a company and there I don't have many possibilities. They just have elasticsearch 1.7.0 . And yes I have embedded elasticsearch node. Sorry for the late version... Commented Feb 15, 2017 at 16:56

1 Answer 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)

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();
Sign up to request clarification or add additional context in comments.

1 Comment

I can't find setSettings() in client.prepareIndex().setSource()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.