Skip to content

Instantly share code, notes, and snippets.

@johtani
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johtani/8818938 to your computer and use it in GitHub Desktop.
Save johtani/8818938 to your computer and use it in GitHub Desktop.
# 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()"
}
}
}
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();
}
}
@jsbonline2006
Copy link

Hi

When I run your exact command I get following error while search
{

took: 47
timed_out: false
_shards: {
    total: 5
    successful: 3
    failed: 2
    failures: [
        {
            index: sample
            shard: 2
            status: 500
            reason: PropertyAccessException[[Error: unresolvable property or identifier: _index] [Near : {... _index['text']['java'].tf() ....}] ^ [Line: 1, Column: 1]]
        }
        {
            index: sample
            shard: 3
            status: 500
            reason: PropertyAccessException[[Error: unresolvable property or identifier: _index] [Near : {... _index['text']['java'].tf() ....}] ^ [Line: 1, Column: 1]]
        }
    ]
}
hits: {
    total: 2
    max_score: 0.26849622
    hits: [ ]
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment