Search in sources :

Example 1 with PercolateRequestBuilder

use of org.elasticsearch.action.percolate.PercolateRequestBuilder in project play2-elasticsearch by cleverage.

the class IndexService method getPercolatorsForDoc.

/**
     * Get percolator match this Object
     *
     * @param indexable
     * @return
     * @throws IOException
     */
public static List<String> getPercolatorsForDoc(Index indexable) {
    PercolateRequestBuilder percolateRequestBuilder = new PercolateRequestBuilder(IndexClient.client, PercolateAction.INSTANCE);
    percolateRequestBuilder.setDocumentType(indexable.getIndexPath().type);
    XContentBuilder doc = null;
    try {
        doc = jsonBuilder().startObject().startObject("doc").startObject(indexable.getIndexPath().type);
        Map<String, Object> map = indexable.toIndex();
        for (String key : map.keySet()) {
            if (key != null && map.get(key) != null) {
                doc.field(key, map.get(key));
            }
        }
        doc.endObject().endObject().endObject();
    } catch (Exception e) {
        Logger.debug("Elasticsearch : Error when get percolator for ");
    }
    percolateRequestBuilder.setSource(doc);
    PercolateResponse percolateResponse = percolateRequestBuilder.execute().actionGet();
    if (percolateResponse == null) {
        return null;
    }
    List<String> matchedQueryIds = new ArrayList<String>();
    PercolateResponse.Match[] matches = percolateResponse.getMatches();
    for (PercolateResponse.Match match : matches) {
        matchedQueryIds.add(match.getId().string());
    }
    return matchedQueryIds;
}
Also used : ArrayList(java.util.ArrayList) PercolateRequestBuilder(org.elasticsearch.action.percolate.PercolateRequestBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IOException(java.io.IOException) PercolateResponse(org.elasticsearch.action.percolate.PercolateResponse)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 PercolateRequestBuilder (org.elasticsearch.action.percolate.PercolateRequestBuilder)1 PercolateResponse (org.elasticsearch.action.percolate.PercolateResponse)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)1