percolate.asciidoc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. [[percolate]]
  2. == Percolate API
  3. The percolator allows one to register queries against an index, and then
  4. send `percolate` requests which include a doc, 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("myIndexName", ".percolator", "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 = client.preparePercolate()
  35. .setIndices("myIndexName")
  36. .setDocumentType("myDocumentType")
  37. .setSource(docBuilder).execute().actionGet();
  38. //Iterate over the results
  39. for(PercolateResponse.Match match : response) {
  40. //Handle the result which is the name of
  41. //the query in the percolator
  42. }
  43. --------------------------------------------------