Hi all,
New to Elastic Search and just having some issues getting my head
around mapping via the Java API. I've got the following unit test
below in which I'm trying to map a simple media object with 2 fields
(title and description).
Index
- mediaindex1
type
- object
id
- 1
I'm accessing one instance of ES running locally but outside the JVM.
I see that I'm able to index the doc and if I query ES via the rest
API:
curl -XGET 'http://localhost:9200/mediaindex1/object/1'
I get the following JSON response:
{"_index":"mediaindex1","_type":"object","_id":"1", "_source" :
{"title":"harry potters songs of wackyness"}}
My test below though tries to get the result and check the title
field. This is null and the source is also empty.
I'm not sure how to store the values in ES so they can also be
retrieved. My mapping suggests that I've set store to true but I maybe
missing something here.
public class ESTest extends Assert {
private static final String TEST_INDEX = "mediaindex1";
private static final String TEST_TYPE = "object";
private static final String TEST_ID1 = "1";
private static final String MEDIA_TITLE_VALUE = "harry potters songs
of wackyness";
private static final String MEDIA_TITLE_FIELD = "title";
private static final String MEDIA_DESCRIPTION_FIELD = "description";
@Test
public void test() throws IOException {
Node node = createNode();
try {
Client client = node.client();
createMapping(client);
index(client);
client.admin().indices().refresh(refreshRequest()).actionGet();
get(client);
} finally {
node.close();
}
}
private Node createNode() {
// creates a new node
// setting client to true means no data will be held by this node
(only acts as a client)
Node node = NodeBuilder.nodeBuilder()
.client(true)
.local(false)
.node().start();
return node;
}
private void get(Client client) {
GetResponse get = null;
try {
get =
client.get(getRequest(TEST_INDEX).type(TEST_TYPE).id(TEST_ID1).fields(MEDIA_TITLE_FIELD)).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
assertNotNull(get);
if(get.isSourceEmpty()) {
System.out.println("source is empty in get response");
}
assertEquals(TEST_ID1, get.getId());
assertEquals(TEST_TYPE, get.getType());
assertEquals(MEDIA_TITLE_VALUE,
get.field(MEDIA_TITLE_FIELD).values().get(0));
}
private void index(Client client) throws IOException {
XContentBuilder json = jsonBuilder().startObject()
.field(MEDIA_TITLE_FIELD, MEDIA_TITLE_VALUE)
.endObject();
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 {
// create properties for the mapped type
XContentBuilder data = jsonBuilder().startObject()
.startObject("properties")
.startObject(MEDIA_TITLE_FIELD)
.field("type", "string")
.field("index", "not_analyzed")
.field("store", "yes")
.endObject()
.startObject(MEDIA_DESCRIPTION_FIELD)
.field("type", "string")
.field("index", "standard")
.field("store", "yes")
.endObject()
.endObject()
.endObject();
CreateIndexResponse mapping = client
.admin()
.indices()
.create(createIndexRequest(TEST_INDEX).mapping(TEST_TYPE, data))
.actionGet();
assertTrue(mapping.acknowledged());
}
}
Any help would be great.
Christian