Search in sources :

Example 6 with Hits

use of org.apache.camel.processor.lucene.support.Hits in project camel by apache.

the class LuceneQueryProcessorTest method testPhraseSearcher.

@Test
public void testPhraseSearcher() throws Exception {
    final StandardAnalyzer analyzer = new StandardAnalyzer();
    MockEndpoint mockSearchEndpoint = getMockEndpoint("mock:searchResult");
    context.stop();
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            try {
                from("direct:start").setHeader("QUERY", constant("Rodney Dangerfield")).process(new LuceneQueryProcessor("target/stdindexDir", analyzer, null, 20)).to("direct:next");
            } catch (Exception e) {
                e.printStackTrace();
            }
            from("direct:next").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    Hits hits = exchange.getIn().getBody(Hits.class);
                    printResults(hits);
                }

                private void printResults(Hits hits) {
                    LOG.debug("Number of hits: " + hits.getNumberOfHits());
                    for (int i = 0; i < hits.getNumberOfHits(); i++) {
                        LOG.debug("Hit " + i + " Index Location:" + hits.getHit().get(i).getHitLocation());
                        LOG.debug("Hit " + i + " Score:" + hits.getHit().get(i).getScore());
                        LOG.debug("Hit " + i + " Data:" + hits.getHit().get(i).getData());
                    }
                }
            }).to("mock:searchResult");
        }
    });
    context.start();
    LOG.debug("------------Beginning Phrase + Standard Analyzer Search Test---------------");
    sendRequest();
    mockSearchEndpoint.assertIsSatisfied();
    LOG.debug("------------Completed Phrase + Standard Analyzer Search Test---------------");
    context.stop();
}
Also used : Exchange(org.apache.camel.Exchange) Hits(org.apache.camel.processor.lucene.support.Hits) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Test(org.junit.Test)

Example 7 with Hits

use of org.apache.camel.processor.lucene.support.Hits in project camel by apache.

the class LuceneQueryProcessorTest method testWildcardSearcher.

@Test
public void testWildcardSearcher() throws Exception {
    final WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer();
    MockEndpoint mockSearchEndpoint = getMockEndpoint("mock:searchResult");
    context.stop();
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            try {
                from("direct:start").setHeader("QUERY", constant("Carl*")).process(new LuceneQueryProcessor("target/simpleindexDir", analyzer, null, 20)).to("direct:next");
            } catch (Exception e) {
                e.printStackTrace();
            }
            from("direct:next").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    Hits hits = exchange.getIn().getBody(Hits.class);
                    printResults(hits);
                }

                private void printResults(Hits hits) {
                    LOG.debug("Number of hits: " + hits.getNumberOfHits());
                    for (int i = 0; i < hits.getNumberOfHits(); i++) {
                        LOG.debug("Hit " + i + " Index Location:" + hits.getHit().get(i).getHitLocation());
                        LOG.debug("Hit " + i + " Score:" + hits.getHit().get(i).getScore());
                        LOG.debug("Hit " + i + " Data:" + hits.getHit().get(i).getData());
                    }
                }
            }).to("mock:searchResult");
        }
    });
    context.start();
    LOG.debug("------------Beginning Wildcard + Simple Analyzer Phrase Searcher Test---------------");
    sendRequest();
    mockSearchEndpoint.assertIsSatisfied();
    LOG.debug("------------Completed Wildcard + Simple Analyzer Phrase Searcher Test---------------");
    context.stop();
}
Also used : WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) Exchange(org.apache.camel.Exchange) Hits(org.apache.camel.processor.lucene.support.Hits) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 8 with Hits

use of org.apache.camel.processor.lucene.support.Hits in project camel by apache.

the class LuceneQueryProducer method process.

public void process(Exchange exchange) throws Exception {
    Hits hits;
    String phrase = exchange.getIn().getHeader("QUERY", String.class);
    String returnLuceneDocs = exchange.getIn().getHeader("RETURN_LUCENE_DOCS", String.class);
    boolean isReturnLuceneDocs = (returnLuceneDocs != null && returnLuceneDocs.equalsIgnoreCase("true")) ? true : false;
    if (phrase != null) {
        searcher.open(indexDirectory, analyzer);
        hits = searcher.search(phrase, maxNumberOfHits, config.getLuceneVersion(), isReturnLuceneDocs);
    } else {
        throw new IllegalArgumentException("SearchPhrase for LucenePhraseQuerySearcher not set. Set the Header value: QUERY");
    }
    exchange.getIn().setBody(hits);
}
Also used : Hits(org.apache.camel.processor.lucene.support.Hits)

Aggregations

Hits (org.apache.camel.processor.lucene.support.Hits)8 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5 RouteBuilder (org.apache.camel.builder.RouteBuilder)5 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)1 LuceneSearcher (org.apache.camel.component.lucene.LuceneSearcher)1 Hit (org.apache.camel.processor.lucene.support.Hit)1 WhitespaceAnalyzer (org.apache.lucene.analysis.core.WhitespaceAnalyzer)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 Document (org.apache.lucene.document.Document)1 ScoreDoc (org.apache.lucene.search.ScoreDoc)1