Sure I'm missing something, but

Basically this is a first pass at incorporating an embedded instance of ES
for testing. We'd then possibly even bundle up ES for deployment using
something similar. However, it's not working. I'm sure there's something
fundamental I'm missing, I just can't see it :S

import static org.elasticsearch.client.Requests.countRequest;
import static org.elasticsearch.client.Requests.getRequest;
import static org.elasticsearch.client.Requests.indexRequest;
import static org.elasticsearch.util.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.xcontent.WildcardQueryBuilder;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.util.settings.ImmutableSettings;
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder;
import org.junit.Assert;
import org.junit.Test;

public class ESTest extends Assert {

private static final String TEST_INDEX = "test_index";
private static final String TEST_TYPE = "test_type";
private static final String TEST_ID1 = "1";
private static final String TEST_USER1 = "paul";
private static final String USER_FIELD = "user";

@Test
public void test() throws IOException {

    Node node = startNode();
    try {
        Client client = node.client();

        createMapping(client);
        index(client);

// count(client);
get(client);

    } finally {
        node.close();
    }
}

private void count(Client client) {
    CountResponse count = client.count(
            countRequest(TEST_INDEX)
            .types(TEST_TYPE).query(new WildcardQueryBuilder(USER_FIELD,

"*"))
).actionGet();

    assertNotNull(count);
    assertEquals(1, count.count());

}

private Node startNode() {

    Node node =

NodeBuilder.nodeBuilder().loadConfigSettings(false).clusterName("test.cluster").local(true).settings(

            ImmutableSettings.settingsBuilder()
            .put("index.number_of_shards", 1)
            .put("index.number_of_replicas", 1).build()

    ).node().start();

    return node;
}

private void get(Client client) {

    GetResponse get = client.get(
            getRequest(TEST_INDEX)
            .type(TEST_TYPE)
            .id(TEST_ID1)
            .fields(USER_FIELD)
    ).actionGet();

    assertNotNull(get);

// assertTrue("Uh-oh, no doc in the index matches...", get.exists());

    assertEquals(TEST_ID1, get.getId());
    assertEquals(TEST_TYPE, get.getType());
    assertEquals(TEST_USER1, get.field(USER_FIELD));

}

private void index(Client client) throws IOException {

    BinaryXContentBuilder json = jsonBuilder()
        .startObject()
            .field(USER_FIELD, TEST_USER1)
        .endObject();

// System.out.println(json.string());

    IndexResponse index = client.index(indexRequest(TEST_INDEX)
            .type(TEST_TYPE)
            .id(TEST_ID1)
            .create(true)
            .source(
                    json
            )
    ).actionGet();

    assertNotNull(index);
    assertEquals(TEST_ID1, index.id());
    assertEquals(TEST_TYPE, index.type());
    assertEquals(TEST_INDEX, index.index());
}

private void createMapping(Client client) throws IOException {

    BinaryXContentBuilder data =

jsonBuilder().startObject().startObject("properties")
.startObject(USER_FIELD)
.field("type", "string")
.field("index", "not_analyzed")
.field("store", "yes")
.endObject()
.endObject().endObject();

// System.out.println(data.string());

    CreateIndexResponse mapping = client.admin().indices().create(
            new CreateIndexRequest(TEST_INDEX).mapping(TEST_TYPE, data)
    ).actionGet();

    assertTrue(mapping.acknowledged());
}

}

Thanks,

--

Paul Loy
paul@keteracel.com
http://www.keteracel.com/

Here is the fixed one. Two things: ElasticSearch is near real time search
engine, so, either you wait for a second till the indexing is visible, or
you can call refresh. I have changed it to call refresh. Second, the get
returns a GetField, and you get a list of values. Here it is:

import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.util.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.index.query.xcontent.WildcardQueryBuilder;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.util.settings.ImmutableSettings;
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder;
import org.junit.Assert;
import org.junit.Test;

public class ESTest extends Assert {

private static final String TEST_INDEX = "test_index";
private static final String TEST_TYPE = "test_type";
private static final String TEST_ID1 = "1";
private static final String TEST_USER1 = "paul";
private static final String USER_FIELD = "user";

@Test
public void test() throws IOException {

    Node node = startNode();
    try {
        Client client = node.client();

        createMapping(client);
        index(client);
        client.admin().indices().refresh(refreshRequest()).actionGet();

// count(client);
get(client);

    } finally {
        node.close();
    }
}

private void count(Client client) {
    CountResponse count = client.count(
            countRequest(TEST_INDEX)
            .types(TEST_TYPE).query(new WildcardQueryBuilder(USER_FIELD,

"*"))
).actionGet();

    assertNotNull(count);
    assertEquals(1, count.count());

}

private Node startNode() {

    Node node =

NodeBuilder.nodeBuilder().loadConfigSettings(false).clusterName("test.cluster").local(true).settings(

            ImmutableSettings.settingsBuilder()
            .put("index.number_of_shards", 1)
            .put("index.number_of_replicas", 1).build()

    ).node().start();

    return node;
}

private void get(Client client) {

    GetResponse get = client.get(
            getRequest(TEST_INDEX)
            .type(TEST_TYPE)
            .id(TEST_ID1)
            .fields(USER_FIELD)
    ).actionGet();

    assertNotNull(get);

// assertTrue("Uh-oh, no doc in the index matches...", get.exists());

    assertEquals(TEST_ID1, get.getId());
    assertEquals(TEST_TYPE, get.getType());
    assertEquals(TEST_USER1, get.field(USER_FIELD).values().get(0));

}

private void index(Client client) throws IOException {

    BinaryXContentBuilder json = jsonBuilder()
        .startObject()
            .field(USER_FIELD, TEST_USER1)
        .endObject();

// System.out.println(json.string());

    IndexResponse index = client.index(indexRequest(TEST_INDEX)
            .type(TEST_TYPE)
            .id(TEST_ID1)
            .create(true)
            .source(
                    json
            )
    ).actionGet();

    assertNotNull(index);
    assertEquals(TEST_ID1, index.id());
    assertEquals(TEST_TYPE, index.type());
    assertEquals(TEST_INDEX, index.index());
}

private void createMapping(Client client) throws IOException {

    BinaryXContentBuilder data =

jsonBuilder().startObject().startObject("properties")
.startObject(USER_FIELD)
.field("type", "string")
.field("index", "not_analyzed")
.field("store", "yes")
.endObject()
.endObject().endObject();

// System.out.println(data.string());

    CreateIndexResponse mapping = client.admin().indices().create(
            createIndexRequest(TEST_INDEX).mapping(TEST_TYPE, data)
    ).actionGet();

    assertTrue(mapping.acknowledged());
}

}

Nice! Wasn't sure if when you did an actionGet on an IndexResponse it waited
until it was actually indexed or not.

Thanks.

On Thu, May 27, 2010 at 6:00 PM, Shay Banon shay.banon@elasticsearch.comwrote:

Here is the fixed one. Two things: Elasticsearch is near real time search
engine, so, either you wait for a second till the indexing is visible, or
you can call refresh. I have changed it to call refresh. Second, the get
returns a GetField, and you get a list of values. Here it is:

import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.util.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.index.query.xcontent.WildcardQueryBuilder;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.util.settings.ImmutableSettings;
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder;
import org.junit.Assert;
import org.junit.Test;

public class ESTest extends Assert {

private static final String TEST_INDEX = "test_index";
private static final String TEST_TYPE = "test_type";
private static final String TEST_ID1 = "1";
private static final String TEST_USER1 = "paul";
private static final String USER_FIELD = "user";

@Test
public void test() throws IOException {

    Node node = startNode();
    try {
        Client client = node.client();

        createMapping(client);
        index(client);
        client.admin().indices().refresh(refreshRequest()).actionGet();

// count(client);
get(client);

    } finally {
        node.close();
    }
}

private void count(Client client) {
    CountResponse count = client.count(
            countRequest(TEST_INDEX)
            .types(TEST_TYPE).query(new

WildcardQueryBuilder(USER_FIELD, "*"))
).actionGet();

    assertNotNull(count);
    assertEquals(1, count.count());

}

private Node startNode() {

    Node node =

NodeBuilder.nodeBuilder().loadConfigSettings(false).clusterName("test.cluster").local(true).settings(

            ImmutableSettings.settingsBuilder()
            .put("index.number_of_shards", 1)
            .put("index.number_of_replicas", 1).build()

    ).node().start();

    return node;
}

private void get(Client client) {

    GetResponse get = client.get(
            getRequest(TEST_INDEX)
            .type(TEST_TYPE)
            .id(TEST_ID1)
            .fields(USER_FIELD)
    ).actionGet();

    assertNotNull(get);

// assertTrue("Uh-oh, no doc in the index matches...",
get.exists());

    assertEquals(TEST_ID1, get.getId());
    assertEquals(TEST_TYPE, get.getType());
    assertEquals(TEST_USER1, get.field(USER_FIELD).values().get(0));

}

private void index(Client client) throws IOException {

    BinaryXContentBuilder json = jsonBuilder()
        .startObject()
            .field(USER_FIELD, TEST_USER1)
        .endObject();

// System.out.println(json.string());

    IndexResponse index = client.index(indexRequest(TEST_INDEX)
            .type(TEST_TYPE)
            .id(TEST_ID1)
            .create(true)
            .source(
                    json
            )
    ).actionGet();

    assertNotNull(index);
    assertEquals(TEST_ID1, index.id());
    assertEquals(TEST_TYPE, index.type());
    assertEquals(TEST_INDEX, index.index());
}

private void createMapping(Client client) throws IOException {

    BinaryXContentBuilder data =

jsonBuilder().startObject().startObject("properties")
.startObject(USER_FIELD)
.field("type", "string")
.field("index", "not_analyzed")
.field("store", "yes")
.endObject()
.endObject().endObject();

// System.out.println(data.string());

    CreateIndexResponse mapping = client.admin().indices().create(
            createIndexRequest(TEST_INDEX).mapping(TEST_TYPE, data)
    ).actionGet();

    assertTrue(mapping.acknowledged());
}

}

--

Paul Loy
paul@keteracel.com
http://www.keteracel.com/paul