Last active
August 29, 2015 13:56
-
-
Save johtani/8818938 to your computer and use it in GitHub Desktop.
Term Freqency sample using script fields.See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-advanced-scripting.html#_term_statistics
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
# Note: Please paste this sample to Sense, chrome plugin or Marvel. | |
# Note: I use 0.90.10 and 1.0.0.RC1 | |
PUT /sample | |
PUT /sample/doc_count/a | |
{ | |
"text": "Java, Java, Java, Java is great." | |
} | |
PUT /sample/doc_count/b | |
{ | |
"text": "Java, Java is coffee." | |
} | |
GET /sample/doc_count/_search | |
{ | |
"query": { | |
"query_string": { | |
"default_field": "text", | |
"query": "java" | |
} | |
}, | |
"script_fields": { | |
"term_count": { | |
"script": "_index['text']['java'].tf()" | |
} | |
} | |
} | |
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
public class TermFreqQuerySample { | |
public static void main(String... args){ | |
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name","test-es-cluster").build(); | |
Client client = new TransportClient(settings) | |
.addTransportAddress(new InetSocketTransportAddress("192.168.30.114",9300)); | |
QueryBuilder query = QueryBuilders.queryString("java") | |
.defaultField("text"); | |
SearchResponse sr = client | |
.prepareSearch("sample") | |
.setQuery(query) | |
.addScriptField("term_count", "_index['text']['java'].tf()") | |
.execute() | |
.actionGet(); | |
long hits = sr.getHits().getTotalHits(); | |
System.out.println("hits:["+hits+"]"); | |
for(SearchHit hit : sr.getHits()){ | |
System.out.println("--------------------"); | |
System.out.println("term_count:"+hit.field("term_count").getValue()); | |
} | |
client.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
When I run your exact command I get following error while search
{
}