The percolator of the Javaapi of Elasticsearch

Source: Internet
Author: User

The percolator of the Javaapi of Elasticsearch

Percolator allows one to register queries in index and then sends a request containing DOC to return a query that has been registered in index and that matches doc

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 is available immediately
. Execute (). Actionget ();

In the index above, the query is named mydesignatedqueryname.

In order to check the document registration query, use this code:

Build a document to check against the percolator
Xcontentbuilder Docbuilder = Xcontentfactory.jsonbuilder (). StartObject ();
Docbuilder.field ("Doc"). StartObject (); This was 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
Percolateresponse response = Client.preparepercolate ()
. SetIndices ("Myindexname")
. Setdocumenttype ("Mydocumenttype")
. SetSource (Docbuilder). Execute (). Actionget ();
Iterate over the results
for (Percolateresponse.match match:response) {
Handle the result which is the name of
The query in the Percolator
}

Traditionally design documents based on data and store them in a single index, and then obtain these documents by searching for queries defined by the API. Percolator the opposite, first you store a query to index and then PERCOLATORAPI to get those queries.

The reason that queries can be stored comes from the fact that both document and query are defined in JSON format in Elasticsearch. This allows you to embed query into document with the index API. Elasticsearch can rely on percolator to extract queries through document. Since document is also defined as JSON, you can define a percolator in the document request.

Percolator and most of its functions work in real time, so percolator query is deposited, so long you can use Percolator

According to mapping, create an index, field:message

Curl-xput ' Localhost:9200/my-index '-d ' {
"Mappings": {
"My-type": {
"Properties": {
"Message": {
' Type ': ' String '
}
}
}
}
}
Register a query into percolator:
Curl-xput ' LOCALHOST:9200/MY-INDEX/.PERCOLATOR/1 '-d ' {
"Query": {
"Match": {
"Message": "Bonsai Tree"
}
}
}‘

Use a document that complies with the registered Percolator query:

Curl-xget ' localhost:9200/my-index/message/_percolate '-d ' {
"Doc": {
"Message": "A new bonsai tree in the Office"
}
}‘

The above request will return the following information:

{
"Took": 19,
"_shards": {
"Total": 5,
"Successful": 5,
"Failed": 0
},
"Total": 1,
"Matches": [
{
"_index": "My-index",
"_id": "1"
}
]
}

Original address:

Http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-percolate.html#search-percolate

Http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/percolate.html

I hope it will not be misunderstood by the poor translation

The percolator of the Javaapi of Elasticsearch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.