How to store fields


(Algoncalves) #1

Hi All,

I´m new in the list and also in ElasticSearch. My doubt is basic, but
I couldn´t find an answer in the documentation.

I´ve inslalled ES and the server is running. However when I indexed
some document using Java API fields are not stored. How can I do this
using Java API.

Thanks,

Alex


(lowang) #2

did you use any mapping? what is the answer when you try to fetch the
documents using curl ?

On Jan 13, 7:22 pm, Alex alexandre.l.goncal...@gmail.com wrote:

Hi All,

I´m new in the list and also in ElasticSearch. My doubt is basic, but
I couldn´t find an answer in the documentation.

I´ve inslalled ES and the server is running. However when I indexed
some document using Java API fields are not stored. How can I do this
using Java API.

Thanks,

Alex


(Karussell) #3

try something like that:

Node node = nodeBuilder().
local(true).
settings(ImmutableSettings.settingsBuilder().
put("number_of_shards", 3).
put("number_of_replicas", 1).
//put("gateway.type", "none").
build()).
build().
start();

    String indexName = "tweetindex";
    String indexType = "tweet";

// put this under _default/tweet.json or use it directly in java:
String fileAsString = "{"
+ ""tweet" : {"
+ " "properties" : {"
+ " "longval" : { "type" : "long",
"null_value" : -1}"
+ "}}}";

    Client client = node.client();

    // create index
    client.admin().indices().
            create(new

CreateIndexRequest(indexName).mapping(indexType, fileAsString)).
actionGet();
client.admin().cluster().health(new
ClusterHealthRequest(indexName).waitForYellowStatus()).actionGet();

    XContentBuilder docBuilder =

XContentFactory.jsonBuilder().startObject();
docBuilder.field("longval", 124L);
docBuilder.endObject();

    // feed previously created doc
    IndexRequestBuilder irb = client.prepareIndex(indexName,

indexType, "1").
setConsistencyLevel(WriteConsistencyLevel.DEFAULT).
setSource(docBuilder);
irb.execute().actionGet();

    // make doc available for sure
    client.admin().indices().refresh(new

RefreshRequest(indexName)).actionGet();

    // query for this doc
    XContentQueryBuilder qb = QueryBuilders.matchAllQuery();
    TermFilterBuilder fb = FilterBuilders.termFilter("longval",

124L);
SearchRequestBuilder srb = client.prepareSearch(indexName).
setSearchType(SearchType.QUERY_AND_FETCH).
setQuery(QueryBuilders.filteredQuery(qb, fb));
SearchResponse response = srb.execute().actionGet();
System.out.println("failed shards:" +
response.getFailedShards());
Object num = response.getHits().hits()
[0].getSource().get("longval");
System.out.println("longval:" + num);
node.stop();

On 13 Jan., 19:22, Alex alexandre.l.goncal...@gmail.com wrote:

Hi All,

I´m new in the list and also in ElasticSearch. My doubt is basic, but
I couldn´t find an answer in the documentation.

I´ve inslalled ES and the server is running. However when I indexed
some document using Java API fields are not stored. How can I do this
using Java API.

Thanks,

Alex


(Algoncalves) #4

It works. Meanwhile thanks. I´ll try to evolve my understanding about mapping.

Alex


(system) #5