2016-05-20 17:53:51 -04:00
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2016-12-20 19:07:36 -05:00
|
|
|
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
|
|
|
|
TransportClient client = new PreBuiltTransportClient(settings);
|
|
|
|
client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 9300)));
|
2016-05-20 17:53:51 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
Before the `percolate` query can be used an `percolator` mapping should be added and
|
|
|
|
a document containing a percolator query should be indexed:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2016-04-19 09:59:09 -04:00
|
|
|
// create an index with a percolator field with the name 'query':
|
|
|
|
client.admin().indices().prepareCreate("myIndexName")
|
|
|
|
.addMapping("query", "query", "type=percolator")
|
2016-12-20 19:07:36 -05:00
|
|
|
.addMapping("docs", "content", "type=text")
|
2016-04-19 09:59:09 -04:00
|
|
|
.get();
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
//This is the query we're registering in the percolator
|
|
|
|
QueryBuilder qb = termQuery("content", "amazing");
|
|
|
|
|
|
|
|
//Index the query = register it in the percolator
|
2016-04-19 09:59:09 -04:00
|
|
|
client.prepareIndex("myIndexName", "query", "myDesignatedQueryName")
|
2013-08-28 19:24:34 -04:00
|
|
|
.setSource(jsonBuilder()
|
|
|
|
.startObject()
|
|
|
|
.field("query", qb) // Register the query
|
|
|
|
.endObject())
|
2016-12-20 19:07:36 -05:00
|
|
|
.setRefreshPolicy(RefreshPolicy.IMMEDIATE) // Needed when the query shall be available immediately
|
2016-04-19 09:59:09 -04:00
|
|
|
.get();
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
This indexes the above term query under the name
|
|
|
|
*myDesignatedQueryName*.
|
|
|
|
|
|
|
|
In order to check a document against the registered queries, use this
|
|
|
|
code:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
//Build a document to check against the percolator
|
|
|
|
XContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();
|
|
|
|
docBuilder.field("content", "This is amazing!");
|
|
|
|
docBuilder.endObject(); //End of the JSON root object
|
2016-12-20 19:07:36 -05:00
|
|
|
|
|
|
|
PercolateQueryBuilder percolateQuery = new PercolateQueryBuilder("query", "docs", docBuilder.bytes());
|
|
|
|
|
2016-03-15 10:07:53 -04:00
|
|
|
// Percolate, by executing the percolator query in the query dsl:
|
|
|
|
SearchResponse response = client().prepareSearch("myIndexName")
|
2016-12-20 19:07:36 -05:00
|
|
|
.setQuery(percolateQuery))
|
2016-03-15 10:07:53 -04:00
|
|
|
.get();
|
2013-08-28 19:24:34 -04:00
|
|
|
//Iterate over the results
|
2016-03-15 10:07:53 -04:00
|
|
|
for(SearchHit hit : response.getHits()) {
|
|
|
|
// Percolator queries as hit
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|