...
The lucene component is based on the Apache Lucene project. Apache Lucene is a powerful high-performance, full-featured text search engine library written entirely in Java. For more details check outabout Lucene, please see the following links
The lucene component in camel facilitates integration and utilization of Lucene endpoints in enterprise integration patterns and scenarios. The lucene component does the following
- builds a searchable index of documents when payloads are sent to the Lucene Endpoint
- facilitates performing of indexed searches in Camel when the payload header contains a QUERY.
This component only supports 2 producer endpoints and a query processor.producer endpoints.
Maven users will need to add the following dependency to their pom.xml
for this component:
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-lucene</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
|
URI format
Code Block |
---|
lucene:searcherName:insert[?options]
lucene:searcherName:query[?options]
|
You can append query options to the URI in the following format, ?option=value&option=value&...
Insert Options
Div | |||
---|---|---|---|
| |||
|
...
|
...
1000
...
The numer of elements that may be stored in the defined cache
...
memoryStoreEvictionPolicy
...
MemoryStoreEvictionPolicy.LFU
...
The number of elements that may be stored in the defined cache. Options include
- MemoryStoreEvictionPolicy.LFU - Least frequently used
- MemoryStoreEvictionPolicy.LRU - Least recently used
- MemoryStoreEvictionPolicy.FIFO - first in first out, the oldest element by creation time
...
overflowToDisk
...
true
...
Specifies whether cache may overflow to disk
...
eternal
...
false
...
Sets whether elements are eternal. If eternal, timeouts are ignored and the
element is never expired.
...
timeToLiveSeconds
...
300
...
The maximum time between creation time and when an element expires.
Is only used if the element is not eternal
...
timeToIdleSeconds
...
300
...
The maximum amount of time between accesses before an element expires
...
diskPersistent
...
true
...
Whether the disk store persists between restarts of the Virtual Machine.
The default value is false.
...
diskExpiryThreadIntervalSeconds
...
120
...
The number of seconds between runs of the disk expiry thread. The default value
is 120 seconds
|
Query Options
Div | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||
|
Sending/Receiving Messages to/from the cache
Message Headers
Div | ||
---|---|---|
| ||
|
...
|
...
|
...
|
...
|
...
- ADD
- UPDATE
- DELETE
- DELETEALL
...
CACHE_KEY
...
The cache key used to store the Message in the cache. The cache key is optional if the CACHE_OPERATION is DELETEALL
|
Lucene Producers
This component supports 2 producer endpoints.
- insert - The insert producer builds a searchable index by analyzing the body in incoming exchanges and associating it with a token ("content").
- query - The query producer performs searches on a pre-created index. The query uses the searchable index to perform score & relevance based searches. Queries are sent via the incoming exchange contains a header property name called 'QUERY'. The value of the header property 'QUERY' is a Lucene Query. For more details on how to create Lucene Queries check out http://lucene.apache.org/java/3_0_0/queryparsersyntax.html
Lucene Processor
There is a processor called LuceneQueryProcessor available to perform queries against lucene without the need to create a producer.
Lucene Usage Samples
Example 1: Creating a Lucene index
Code Block |
---|
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("direct:start").
|
Cache Producer
Sending data to the cache involves the ability to direct payloads in exchanges to be stored in a pre-existing or created-on- demand cache. The mechanics of doing this involve
- setting the Message Exchange Headers shown above.
- ensuring that the Message Exchange Body contains the message directed to the cache
Cache Consumer
Receiving data from the cache involves the ability of the CacheConsumer to listen on a pre-existing or created-on-demand Cache using an event Listener and receive automatic notifications when any cache activity take place (i.e ADD/UPDATE/DELETE/DELETEALL). Upon such an activity taking place
- an exchange containing Message Exchange Headers and a Message Exchange Body containing the just added/updated payload is placed and sent.
- in case of a DELETEALL operation, the Message Exchange Header CACHE_KEY and the Message Exchange Body are not populated.
Cache Processors
There are a set of nice processors with the ability to perform cache lookups and selectively replace payload content at the
- body
- token
- xpath level
Cache Usage Samples
Example 1: Configuring the cache
Code Block |
---|
from("cache://MyApplicationCache" + "?maxElementsInMemory=1000" + "&memoryStoreEvictionPolicy=" + to("lucene:whitespaceQuotesIndex:insert? "MemoryStoreEvictionPolicy.LFU" + analyzer=#whitespaceAnalyzer&indexDir=#whitespace&srcDir=#load_dir"). "&overflowToDisk=true" + "&eternal=true" + to("mock:result"); } }; |
Example 2: Loading properties into the JNDI registry in the Camel Context
Code Block |
---|
@Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry registry = new JndiRegistry(createJndiContext()); "&timeToLiveSeconds=300" + registry.bind("whitespace", new File("./whitespaceIndexDir")); registry.bind("load_dir", new "&timeToIdleSeconds=true" + File("src/test/resources/sources")); registry.bind("whitespaceAnalyzer", "&diskPersistent=true" +new WhitespaceAnalyzer()); return registry; } ... CamelContext context = "&diskExpiryThreadIntervalSeconds=300")new DefaultCamelContext(createRegistry()); |
Example 2:
...
Performing searches using a Query Producer
Code Block |
---|
RouteBuilder builder = new RouteBuilder() { public void configure() { from("direct:start"). . setHeader("CACHE_OPERATIONQUERY", constant("ADDSeinfeld")). .setHeader("CACHE_KEY", constant("Ralph_Waldo_Emerson")) to("lucene:searchIndex:query? analyzer=#whitespaceAnalyzer&indexDir=#whitespace&maxHits=20"). . to("cachedirect://TestCache1next"); } }; |
Example 2: Updating existing keys in a cache
Code Block |
---|
RouteBuilder builder = new RouteBuilder from("direct:next").process(new Processor() { public void configureprocess(Exchange exchange) throws Exception { from("direct:start") Hits hits = exchange.getIn().getBody(Hits.class); .setHeader("CACHE_OPERATION", constant("UPDATE")) .setHeader("CACHE_KEY", constant("Ralph_Waldo_Emerson")) printResults(hits); } private void printResults(Hits hits) { .to("cache://TestCache1") } }; |
Example 3: Deleting existing keys in a cache
Code Block |
---|
RouteBuilder builder = new RouteBuilder() LOG.debug("Number of hits: " + hits.getNumberOfHits()); for (int i = 0; i < hits.getNumberOfHits(); i++) { public void configure() { from("direct:start") LOG.debug("Hit " + i + " Index Location:" + hits.getHit().get(i).getHitLocation()); LOG.setHeaderdebug("CACHE_OPERATION", constant("DELETE")) Hit " + i + " Score:" + hits.getHit().get(i).getScore()); .setHeader("CACHE_KEY", constant("Ralph_Waldo_Emerson")) .to("cache://TestCache1") LOG.debug("Hit " + i + " Data:" + hits.getHit().get(i).getData()); } } }).to("mock:searchResult"); } }; |
Example
...
3: Performing searches using a Query Processor
Code Block |
---|
RouteBuilder builder = new RouteBuilder() { public void configure() { try { from("direct:start"). . setHeader("CACHE_OPERATIONQUERY", constant("DELETEALLRodney Dangerfield")). process(new .toLuceneQueryProcessor("cache:target//TestCache1"); } }; |
Example 5: Notifying any changes registering in a Cache to Processors and other Producers
Code Block |
---|
RouteBuilder builder = new RouteBuilder() { stdindexDir", analyzer, null, 20)). public void configure() { fromto("cachedirect://TestCache1next"); .process(new Processor( } catch (Exception e) { public void process(Exchange exchange) e.printStackTrace(); } throws Exception { String operation = from(String) exchange.getIn().getHeader("CACHE_OPERATION");"direct:next").process(new Processor() { String keypublic =void process(String)Exchange exchange.getIn().getHeader("CACHE_KEY"); ) throws Exception { ObjectHits bodyhits = exchange.getIn().getBody(Hits.class); // Do somethingprintResults(hits); } }) } }; |
Example 6: Using Processors to selectively replace payload with cache values
Code Block |
---|
RouteBuilder builder = new RouteBuilder() { publicprivate void configureprintResults(Hits hits) { //Message Body Replacer from("cache://TestCache1") LOG.filterdebug(header("CACHE_KEY").isEqualTo("greeting")) "Number of hits: " + hits.getNumberOfHits()); .process(new CacheBasedMessageBodyReplacer("cache://TestCache1","farewell")) .to("direct:next"); //Message Token replacer from("cache://TestCache1") .filter(header("CACHE_KEY").isEqualTo("quote")) .process(new CacheBasedTokenReplacer("cache://TestCache1","novel","#novel#")) .process(new CacheBasedTokenReplacer("cache://TestCache1","author","#author#")) .process(new CacheBasedTokenReplacer("cache://TestCache1","number","#number#")) .to("direct:next"); //Message XPath replacer from("cache://TestCache1"). .filter(header("CACHE_KEY").isEqualTo("XML_FRAGMENT")) .process(new CacheBasedXPathReplacer("cache://TestCache1","book1","/books/book1")) .process (new CacheBasedXPathReplacer("cache://TestCache1","book2","/books/book2")) .to("direct:next 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"); } }; |