| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 | [[java-query-percolate-query]]==== Percolate QuerySee: * {ref}/query-dsl-percolate-query.html[Percolate Query][source,java]--------------------------------------------------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)));--------------------------------------------------Before the `percolate` query can be used an `percolator` mapping should be added anda document containing a percolator query should be indexed:[source,java]--------------------------------------------------// create an index with a percolator field with the name 'query':client.admin().indices().prepareCreate("myIndexName")                        .addMapping("query", "query", "type=percolator")                        .addMapping("docs", "content", "type=text")                        .get();//This is the query we're registering in the percolatorQueryBuilder qb = termQuery("content", "amazing");//Index the query = register it in the percolatorclient.prepareIndex("myIndexName", "query", "myDesignatedQueryName")    .setSource(jsonBuilder()        .startObject()            .field("query", qb) // Register the query        .endObject())    .setRefreshPolicy(RefreshPolicy.IMMEDIATE) // Needed when the query shall be available immediately    .get();--------------------------------------------------This indexes the above term query under the name*myDesignatedQueryName*.In order to check a document against the registered queries, use thiscode:[source,java]--------------------------------------------------//Build a document to check against the percolatorXContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();docBuilder.field("content", "This is amazing!");docBuilder.endObject(); //End of the JSON root objectPercolateQueryBuilder percolateQuery = new PercolateQueryBuilder("query", "docs", docBuilder.bytes());// Percolate, by executing the percolator query in the query dsl:SearchResponse response = client().prepareSearch("myIndexName")        .setQuery(percolateQuery))        .get();//Iterate over the resultsfor(SearchHit hit : response.getHits()) {    // Percolator queries as hit}--------------------------------------------------
 |