Created
April 6, 2011 12:38
-
-
Save buggyvelarde/905570 to your computer and use it in GitHub Desktop.
Retrieving selected _source fields from a search using Java API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"query":{"field":{"organism_id":1}}} | |
region1 | |
{"uniqueName":"region1","organism_id":1,"sequence":"atgc"} | |
region2 | |
{"uniqueName":"region2","organism_id":1,"sequence":"atgcatgc"} | |
region3 | |
{"uniqueName":"region3","organism_id":1,"sequence":"atgcatgcatgc"} | |
{"query":{"field":{"organism_id":1}},"fields":["uniqueName","organism_id"]} | |
region1 | |
null | |
region2 | |
null | |
region3 | |
null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"took" : 400, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 3, | |
"max_score" : 1.0, | |
"hits" : [ { | |
"_index" : "annotations", | |
"_type" : "region", | |
"_id" : "region1", | |
"_score" : 1.0, | |
"fields" : { | |
"organism_id" : 1, | |
"uniqueName" : "region1" | |
} | |
}, { | |
"_index" : "annotations", | |
"_type" : "region", | |
"_id" : "region2", | |
"_score" : 1.0, | |
"fields" : { | |
"organism_id" : 1, | |
"uniqueName" : "region2" | |
} | |
}, { | |
"_index" : "annotations", | |
"_type" : "region", | |
"_id" : "region3", | |
"_score" : 1.0, | |
"fields" : { | |
"organism_id" : 1, | |
"uniqueName" : "region3" | |
} | |
} ] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -XGET 'http://localhost:9200/annotations/region/_search?pretty=true' -d '{"query":{"field":{"organism_id":1}},"fields":["uniqueName","organism_id"]}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.genedb.test; | |
import java.io.IOException; | |
import org.elasticsearch.ElasticSearchException; | |
import org.elasticsearch.action.search.SearchResponse; | |
import org.elasticsearch.client.action.search.SearchRequestBuilder; | |
import org.elasticsearch.client.transport.TransportClient; | |
import org.elasticsearch.index.query.xcontent.FieldQueryBuilder; | |
import org.elasticsearch.index.query.xcontent.QueryBuilders; | |
import org.elasticsearch.search.SearchHit; | |
import org.elasticsearch.search.SearchHits; | |
import org.elasticsearch.client.Client; | |
import org.elasticsearch.common.transport.InetSocketTransportAddress; | |
import org.elasticsearch.common.xcontent.ToXContent; | |
import org.elasticsearch.common.xcontent.json.JsonXContent; | |
import static org.elasticsearch.common.xcontent.XContentFactory.*; | |
public class TestES { | |
Client client; | |
public static void main(String[] args) throws ElasticSearchException, IOException { | |
TestES test = new TestES(); | |
test.index("region1", 1, "atgc"); | |
test.index("region2", 1, "atgcatgc"); | |
test.index("region3", 1, "atgcatgcatgc"); | |
// works, will bring back the source | |
test.search(1, null); | |
// specifying fields results in an empty source | |
test.search(1, new String[]{"uniqueName", "organism_id"}); | |
} | |
TestES() { | |
client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("localhost", 9300)); | |
} | |
void index(String key, int organism_id, String sequence) throws ElasticSearchException, IOException { | |
client.prepareIndex("annotations", "region", key) | |
.setSource(jsonBuilder() | |
.startObject() | |
.field("uniqueName", key) | |
.field("organism_id", organism_id) | |
.field("sequence", sequence) | |
.endObject() | |
) | |
.execute() | |
.actionGet(); | |
} | |
void search(int organism_id, String[] fields) { | |
FieldQueryBuilder organismQuery = | |
QueryBuilders.fieldQuery("organism_id", organism_id); | |
SearchRequestBuilder srb = client | |
.prepareSearch("annotations") | |
.setTypes("region") | |
.setQuery(organismQuery); | |
// if any fields are specified, the source is not returned | |
if (fields != null) { | |
srb.addFields(fields); | |
} | |
System.out.println(toString(srb.internalBuilder())); | |
SearchResponse response = srb.execute() | |
.actionGet(); | |
SearchHits hits = response.getHits(); | |
for (SearchHit hit : hits) { | |
// always returns fine | |
System.out.println(hit.getId()); | |
// will be null if any fields specified | |
System.out.println(hit.sourceAsString()); | |
} | |
} | |
static String toString(ToXContent tmp) { | |
try { | |
return tmp.toXContent(JsonXContent.unCachedContentBuilder(), | |
ToXContent.EMPTY_PARAMS).prettyPrint().string(); | |
} catch (Exception ex) { | |
return "<ERROR:" + ex.getMessage() + ">"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment