2016-03-15 10:07:53 -04:00
|
|
|
[[java-query-percolator-query]]
|
|
|
|
==== Percolator query
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2016-03-15 10:07:53 -04:00
|
|
|
See:
|
2016-03-21 09:09:30 -04:00
|
|
|
* {ref}/query-dsl-percolator-query.html[Percolator Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
//This is the query we're registering in the percolator
|
|
|
|
QueryBuilder qb = termQuery("content", "amazing");
|
|
|
|
|
|
|
|
//Index the query = register it in the percolator
|
2013-11-05 14:02:59 -05:00
|
|
|
client.prepareIndex("myIndexName", ".percolator", "myDesignatedQueryName")
|
2013-08-28 19:24:34 -04:00
|
|
|
.setSource(jsonBuilder()
|
|
|
|
.startObject()
|
|
|
|
.field("query", qb) // Register the query
|
|
|
|
.endObject())
|
|
|
|
.setRefresh(true) // Needed when the query shall be available immediately
|
|
|
|
.execute().actionGet();
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
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("doc").startObject(); //This is needed to designate the document
|
|
|
|
docBuilder.field("content", "This is amazing!");
|
|
|
|
docBuilder.endObject(); //End of the doc field
|
|
|
|
docBuilder.endObject(); //End of the JSON root object
|
2016-03-15 10:07:53 -04:00
|
|
|
// Percolate, by executing the percolator query in the query dsl:
|
|
|
|
SearchResponse response = client().prepareSearch("myIndexName")
|
|
|
|
.setQuery(QueryBuilders.percolatorQuery("myDocumentType", docBuilder.bytes()))
|
|
|
|
.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
|
|
|
}
|
|
|
|
--------------------------------------------------
|