Search in sources :

Example 1 with PercolateResponse

use of org.elasticsearch.action.percolate.PercolateResponse in project apex-malhar by apache.

the class ElasticSearchPercolateTest method checkPercolateResponse.

/**
 */
private void checkPercolateResponse() {
    ElasticSearchPercolatorOperator oper = new ElasticSearchPercolatorOperator();
    oper.hostName = HOST_NAME;
    oper.port = PORT;
    oper.indexName = INDEX_NAME;
    oper.documentType = DOCUMENT_TYPE;
    oper.setup(null);
    String[] messages = { "{content:'This will match only with malhar'}", "{content:'This will match only with github'}", "{content:'This will match with both github and malhar'}", "{content:'This will not match with any of them'}" };
    String[][] matches = { { MALHAR_TOPIC }, { GITHUB_TOPIC }, { GITHUB_TOPIC, MALHAR_TOPIC }, {} };
    CollectorTestSink<PercolateResponse> sink = new CollectorTestSink<PercolateResponse>();
    oper.outputPort.setSink((CollectorTestSink) sink);
    for (String message : messages) {
        oper.inputPort.process(message);
    }
    int i = 0;
    for (PercolateResponse response : sink.collectedTuples) {
        List<String> matchIds = new ArrayList<String>();
        for (Match match : response.getMatches()) {
            matchIds.add(match.getId().toString());
        }
        Collections.sort(matchIds);
        Assert.assertArrayEquals(matchIds.toArray(), matches[i]);
        i++;
    }
}
Also used : ArrayList(java.util.ArrayList) PercolateResponse(org.elasticsearch.action.percolate.PercolateResponse) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Match(org.elasticsearch.action.percolate.PercolateResponse.Match)

Example 2 with PercolateResponse

use of org.elasticsearch.action.percolate.PercolateResponse in project storm by apache.

the class EsPercolateBolt method process.

/**
     * {@inheritDoc}
     * Tuple should have relevant fields (source, index, type) for storeMapper to extract ES document.<br/>
     * If there exists non-empty percolate response, EsPercolateBolt will emit tuple with original source
     * and Percolate.Match for each Percolate.Match in PercolateResponse.
     */
@Override
public void process(Tuple tuple) {
    try {
        String source = tupleMapper.getSource(tuple);
        String index = tupleMapper.getIndex(tuple);
        String type = tupleMapper.getType(tuple);
        PercolateResponse response = client.preparePercolate().setIndices(index).setDocumentType(type).setPercolateDoc(PercolateSourceBuilder.docBuilder().setDoc(source)).execute().actionGet();
        if (response.getCount() > 0) {
            for (PercolateResponse.Match match : response) {
                collector.emit(new Values(source, match));
            }
        }
        collector.ack(tuple);
    } catch (Exception e) {
        collector.reportError(e);
        collector.fail(tuple);
    }
}
Also used : Values(org.apache.storm.tuple.Values) PercolateResponse(org.elasticsearch.action.percolate.PercolateResponse)

Example 3 with PercolateResponse

use of org.elasticsearch.action.percolate.PercolateResponse 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

PercolateResponse (org.elasticsearch.action.percolate.PercolateResponse)3 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)1 Values (org.apache.storm.tuple.Values)1 PercolateRequestBuilder (org.elasticsearch.action.percolate.PercolateRequestBuilder)1 Match (org.elasticsearch.action.percolate.PercolateResponse.Match)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)1