mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-04-14 03:10:38 +00:00
Also replaced the PercolatorQueryRegistry with the new PercolatorQueryCache. The PercolatorFieldMapper stores the rewritten form of each percolator query's xcontext in a binary doc values field. This make sure that the query rewrite happens only during indexing (some queries for example fetch shapes, terms in remote indices) and the speed up the loading of the queries in the percolator query cache. Because the percolator now works inside the search infrastructure a number of features (sorting fields, pagination, fetch features) are available out of the box. The following feature requests are automatically implemented via this refactoring: Closes #10741 Closes #7297 Closes #13176 Closes #13978 Closes #11264 Closes #10741 Closes #4317
45 lines
1.6 KiB
Plaintext
45 lines
1.6 KiB
Plaintext
[[java-query-percolator-query]]
|
|
==== Percolator query
|
|
|
|
See:
|
|
* {ref}/query-percolator-query.html[Percolator Query]
|
|
|
|
[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
|
|
client.prepareIndex("myIndexName", ".percolator", "myDesignatedQueryName")
|
|
.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
|
|
// Percolate, by executing the percolator query in the query dsl:
|
|
SearchResponse response = client().prepareSearch("myIndexName")
|
|
.setQuery(QueryBuilders.percolatorQuery("myDocumentType", docBuilder.bytes()))
|
|
.get();
|
|
//Iterate over the results
|
|
for(SearchHit hit : response.getHits()) {
|
|
// Percolator queries as hit
|
|
}
|
|
--------------------------------------------------
|