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;
}
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);
}
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();
}
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);
}
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();
}
Aggregations