percolate.asciidoc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. [[percolate]]
  2. == Percolate API
  3. The percolator allows to register queries against an index, and then
  4. send `percolate` requests which include a doc, and getting back the
  5. queries that match on that doc out of the set of registered queries.
  6. Read the main {ref}/search-percolate.html[percolate]
  7. documentation before reading this guide.
  8. [source,java]
  9. --------------------------------------------------
  10. //This is the query we're registering in the percolator
  11. QueryBuilder qb = termQuery("content", "amazing");
  12. //Index the query = register it in the percolator
  13. client.prepareIndex("_percolator", "myIndexName", "myDesignatedQueryName")
  14. .setSource(jsonBuilder()
  15. .startObject()
  16. .field("query", qb) // Register the query
  17. .endObject())
  18. .setRefresh(true) // Needed when the query shall be available immediately
  19. .execute().actionGet();
  20. --------------------------------------------------
  21. This indexes the above term query under the name
  22. *myDesignatedQueryName*.
  23. In order to check a document against the registered queries, use this
  24. code:
  25. [source,java]
  26. --------------------------------------------------
  27. //Build a document to check against the percolator
  28. XContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();
  29. docBuilder.field("doc").startObject(); //This is needed to designate the document
  30. docBuilder.field("content", "This is amazing!");
  31. docBuilder.endObject(); //End of the doc field
  32. docBuilder.endObject(); //End of the JSON root object
  33. //Percolate
  34. PercolateResponse response =
  35. client.preparePercolate("myIndexName", "myDocumentType").setSource(docBuilder).execute().actionGet();
  36. //Iterate over the results
  37. for(String result : response) {
  38. //Handle the result which is the name of
  39. //the query in the percolator
  40. }
  41. --------------------------------------------------