Search in sources :

Example 1 with Hits

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

the class LuceneSearcher method search.

public Hits search(String searchPhrase, int maxNumberOfHits, Version luceneVersion, boolean returnLuceneDocs) throws Exception {
    Hits searchHits = new Hits();
    int numberOfHits = doSearch(searchPhrase, maxNumberOfHits, luceneVersion);
    searchHits.setNumberOfHits(numberOfHits);
    for (ScoreDoc hit : hits) {
        Document selectedDocument = indexSearcher.doc(hit.doc);
        Hit aHit = new Hit();
        if (returnLuceneDocs) {
            aHit.setDocument(selectedDocument);
        }
        aHit.setHitLocation(hit.doc);
        aHit.setScore(hit.score);
        aHit.setData(selectedDocument.get("contents"));
        searchHits.getHit().add(aHit);
    }
    return searchHits;
}
Also used : Hits(org.apache.camel.processor.lucene.support.Hits) Hit(org.apache.camel.processor.lucene.support.Hit) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 2 with Hits

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

the class LuceneQueryProcessor 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 = new LuceneSearcher();
        searcher.open(indexDirectory, analyzer);
        hits = searcher.search(phrase, maxNumberOfHits, luceneVersion, isReturnLuceneDocs);
    } else {
        throw new IllegalArgumentException("SearchPhrase for LuceneQueryProcessor not set. Set the Header value: QUERY");
    }
    exchange.getIn().setBody(hits);
}
Also used : LuceneSearcher(org.apache.camel.component.lucene.LuceneSearcher) Hits(org.apache.camel.processor.lucene.support.Hits)

Example 3 with Hits

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

the class LuceneIndexAndQueryProducerTest method testLuceneWildcardQueryProducer.

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

        public void configure() {
            from("direct:start").setHeader("QUERY", constant("Grouc?? Marx")).to("lucene:searchIndex:query?analyzer=#stdAnalyzer&indexDir=#std&maxHits=20").to("direct:next");
            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  LuceneQueryProducer Wildcard Test---------------");
    sendQuery();
    mockSearchEndpoint.assertIsSatisfied();
    LOG.debug("------------Completed LuceneQueryProducer Wildcard 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) Test(org.junit.Test)

Example 4 with Hits

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

the class LuceneIndexAndQueryProducerTest method testReturnLuceneDocsQueryProducer.

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

        public void configure() {
            from("direct:start").setHeader("QUERY", constant("Grouc?? Marx")).setHeader("RETURN_LUCENE_DOCS", constant("true")).to("lucene:searchIndex:query?analyzer=#stdAnalyzer&indexDir=#std&maxHits=20").to("direct:next");
            from("direct:next").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    Hits hits = exchange.getIn().getBody(Hits.class);
                    try {
                        printResults(hits);
                    } catch (Exception e) {
                        LOG.error(e.getMessage());
                        exchange.getOut().setBody(null);
                    }
                }

                private void printResults(Hits hits) throws Exception {
                    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());
                        if (hits.getHit().get(i).getDocument() == null) {
                            throw new Exception("Failed to return lucene documents");
                        }
                    }
                }
            }).to("mock:searchResult").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    Hits hits = exchange.getIn().getBody(Hits.class);
                    if (hits == null) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("NO_LUCENE_DOCS_ERROR", "NO LUCENE DOCS FOUND");
                        exchange.getContext().setProperties(map);
                    }
                    LOG.debug("Number of hits: " + hits.getNumberOfHits());
                }
            });
        }
    });
    context.start();
    LOG.debug("------------Beginning  LuceneQueryProducer Wildcard with Return Lucene Docs Test---------------");
    sendQuery();
    mockSearchEndpoint.assertIsSatisfied();
    Map<String, String> errorMap = mockSearchEndpoint.getCamelContext().getProperties();
    LOG.debug("------------Completed LuceneQueryProducer Wildcard with Return Lucene Docs Test---------------");
    context.stop();
    assertTrue(errorMap.get("NO_LUCENE_DOCS_ERROR") == null);
}
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) HashMap(java.util.HashMap) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 5 with Hits

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

the class LuceneIndexAndQueryProducerTest method testLucenePhraseQueryProducer.

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

        public void configure() {
            from("direct:start").setHeader("QUERY", constant("Seinfeld")).to("lucene:searchIndex:query?analyzer=#whitespaceAnalyzer&indexDir=#whitespace&maxHits=20").to("direct:next");
            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 LuceneQueryProducer Phrase Test---------------");
    sendQuery();
    mockSearchEndpoint.assertIsSatisfied();
    LOG.debug("------------Completed LuceneQueryProducer Phrase 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) Test(org.junit.Test)

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