Skip to content

Instantly share code, notes, and snippets.

Created November 8, 2011 15:26
Show Gist options
  • Save anonymous/1348031 to your computer and use it in GitHub Desktop.
Save anonymous/1348031 to your computer and use it in GitHub Desktop.
an attemp to make range search work using lucene syntax
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
public class MappingRangeTest {
public static void main(String[] args) throws Exception {
Node node = nodeBuilder().node();
Client client = node.client();
client.admin().indices().prepareCreate("aa")
.addMapping("news",
jsonBuilder()
.startObject()
.startObject("properties")
.startObject("dd")
.field("type","long")
.field("store","yes")
.endObject()
.startObject("title")
.field("type","string")
.field("store","yes")
.endObject()
.endObject()
.endObject())
.execute().actionGet();
try {
client.prepareIndex("aa", "news", "1").setSource(
jsonBuilder()
.startObject()
.field("dd",20)
.field("title","news 1")
.endObject()).execute().actionGet();
client.prepareIndex("aa", "news", "2").setSource(
jsonBuilder()
.startObject()
.field("dd",30)
.field("title","news 2")
.endObject()).execute().actionGet();
Thread.sleep(1000);
System.out.println("mapping: "+
client.admin().cluster().prepareState().execute().actionGet()
.state().metaData().index("aa").mapping("news").source());
SearchHits hits = client.prepareSearch()
.setQuery(QueryBuilders.queryString("+dd:[10 to 40]"))
.setIndices("aa")
.execute().actionGet().getHits();
for(SearchHit h : hits) {
System.out.println(h.getSource());
}
}
finally {
client.admin().indices().prepareDelete("aa").execute().actionGet();
node.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment