Camel Components

Lucene

Since Camel 2.2

Only producer is supported

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 about 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

Maven users will need to add the following dependency to their pom.xml for this component:

<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

lucene:searcherName:insert[?options]
lucene:searcherName:query[?options]

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The Lucene component supports the following options which are listed below.

Name Description Default Type

analyzer (producer)

The Lucene Analyzer that breaks text into terms: insert uses it to analyze the fields of the documents it adds, and query uses it to analyze the query text, so use the same analyzer for both. Refer to an org.apache.lucene.analysis.Analyzer bean in the registry, such as #myAnalyzer. When not set, the Lucene StandardAnalyzer is used. Set it on the endpoint: a value set on the component is not used.

Analyzer

indexDir (producer)

The file system directory of the Lucene index: insert writes the index there (the directory is created when missing) and query searches the index there. Refer to a java.io.File bean in the registry, such as #myIndexDir: a plain path is looked up as a bean name. When not set, the relative path file:/indexDirectory is used, that is a directory named file: in the current working directory. Set it on the endpoint: a value set on the component is not used.

File

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

maxHits (producer)

The maximum number of hits that the query operation returns, the documents with the highest score first. Must be greater than 0, otherwise each query fails. When not set, 10 hits are returned. Not used by the insert operation. Set it on the endpoint: a value set on the component is not used.

int

srcDir (producer)

A directory of files that the insert operation adds to the index when the endpoint is created. Each file, also in subdirectories, is indexed as one Lucene document with the file text in the contents field and the file path in the path field; the document of a path that is already in the index is replaced. Not used by the query operation. Refer to a java.io.File bean in the registry, such as #mySourceDir: a plain path is looked up as a bean name. Set it on the endpoint: a value set on the component is not used.

File

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

config (advanced)

A shared configuration that each endpoint starts from. The endpoint sets all its options from its URI (an option missing in the URI gets its default), so the values set here are not used.

LuceneConfiguration

Endpoint Options

The Lucene endpoint is configured using URI syntax:

lucene:host:operation

With the following path and query parameters:

Path Parameters

Name Description Default Type

host (producer)

Required A name of your choice for the endpoint. Lucene runs embedded, so this is not a server address: the index is located by indexDir.

String

operation (producer)

Required The operation: insert adds the message to the index as a Lucene document, query searches the index with the query in the CamelLuceneQuery header.

Enum values:

  • insert

  • query

LuceneOperation

Query Parameters

Name Description Default Type

analyzer (producer)

The Lucene Analyzer that breaks text into terms: insert uses it to analyze the fields of the documents it adds, and query uses it to analyze the query text, so use the same analyzer for both. Refer to an org.apache.lucene.analysis.Analyzer bean in the registry, such as #myAnalyzer. When not set, the Lucene StandardAnalyzer is used. Set it on the endpoint: a value set on the component is not used.

Analyzer

indexDir (producer)

The file system directory of the Lucene index: insert writes the index there (the directory is created when missing) and query searches the index there. Refer to a java.io.File bean in the registry, such as #myIndexDir: a plain path is looked up as a bean name. When not set, the relative path file:/indexDirectory is used, that is a directory named file: in the current working directory. Set it on the endpoint: a value set on the component is not used.

File

maxHits (producer)

The maximum number of hits that the query operation returns, the documents with the highest score first. Must be greater than 0, otherwise each query fails. When not set, 10 hits are returned. Not used by the insert operation. Set it on the endpoint: a value set on the component is not used.

int

srcDir (producer)

A directory of files that the insert operation adds to the index when the endpoint is created. Each file, also in subdirectories, is indexed as one Lucene document with the file text in the contents field and the file path in the path field; the document of a path that is already in the index is replaced. Not used by the query operation. Refer to a java.io.File bean in the registry, such as #mySourceDir: a plain path is looked up as a bean name. Set it on the endpoint: a value set on the component is not used.

File

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

Message Headers

The Lucene component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelLuceneQuery (producer)

Constant: HEADER_QUERY

The query that the query operation runs against the index, in the syntax of the Lucene classic QueryParser: terms, phrases in quotes, wildcards, boolean operators and field:term. A term without a field searches the contents field, and the query text is analyzed with the analyzer of the endpoint. Required by the query operation.

String

CamelLuceneReturnLuceneDocs (producer)

Constant: HEADER_RETURN_LUCENE_DOCS

Set to true to include the Lucene Document of each hit, with all its stored fields, in the result of the query operation (the document of each Hit). Without it, a Hit holds only the score, the document number and the contents field.

String

Message Body

The message body at each side of the Lucene component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Producer input

any

insert indexes the body as text in the contents field: bytes, a stream or a file are read as text, another object as its toString(), and no body fails; each header is indexed too, as a field of its name. query does not use the body: the query is the CamelLuceneQuery header.

  • With operation=insert: text, bytes, stream or file

Producer output

unchanged or pojo

insert leaves the body unchanged. query gives the Hits: the number of hits and a List of Hit with the score and the contents of each document found (and the Lucene Document with the CamelLuceneReturnLuceneDocs header).

  • With operation=insert: unchanged

  • With operation=query: pojo (org.apache.camel.processor.lucene.support.Hits)

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Usage

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 the field contents. Each exchange is indexed as one document, which holds the body in the field contents, the exchange id in the field exchangeId, and each header in a field of the header name. Each file under the srcDir directory, including its subdirectories, is indexed as one document, which holds the file contents in the field contents and the file path, not tokenized, in the field path. 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 CamelLuceneQuery. The value of the header property CamelLuceneQuery is a Lucene Query. For more details on how to create Lucene Queries, check out Query Parser Classic syntax

Lucene Processor

There is a processor called LuceneQueryProcessor available to perform queries against lucene without the need to create a producer.

Examples

Lucene usage samples.

Example 1: Creating a Lucene index

  • Java

  • XML

  • YAML

from("direct:start")
    .to("lucene:whitespaceQuotesIndex:insert?analyzer=#whitespaceAnalyzer&indexDir=#whitespace&srcDir=#load_dir")
    .to("mock:result");
<route>
  <from uri="direct:start"/>
  <to uri="lucene:whitespaceQuotesIndex:insert?analyzer=#whitespaceAnalyzer&amp;indexDir=#whitespace&amp;srcDir=#load_dir"/>
  <to uri="mock:result"/>
</route>
- route:
    from:
      uri: direct:start
      steps:
        - to:
            uri: lucene:whitespaceQuotesIndex:insert
            parameters:
              analyzer: "#whitespaceAnalyzer"
              indexDir: "#whitespace"
              srcDir: "#load_dir"
        - to:
            uri: mock:result

Example 2: Binding beans in the registry of the Camel Context

Java-only: programmatic CamelContext and registry configuration
CamelContext context = new DefaultCamelContext();
Registry registry = context.getRegistry();
registry.bind("whitespace", new File("./whitespaceIndexDir"));
registry.bind("load_dir", new File("src/test/resources/sources"));
registry.bind("whitespaceAnalyzer", new WhitespaceAnalyzer());

Example 2: Performing searches using a Query Producer

  • Java

  • XML

  • YAML

from("direct:start")
    .setHeader("CamelLuceneQuery", constant("Seinfeld"))
    .to("lucene:searchIndex:query?analyzer=#whitespaceAnalyzer&indexDir=#whitespace&maxHits=20")
    .to("direct:next");

from("direct:next")
    .to("bean:searchResultProcessor")
    .to("mock:searchResult");
<route>
  <from uri="direct:start"/>
  <setHeader name="CamelLuceneQuery">
    <constant>Seinfeld</constant>
  </setHeader>
  <to uri="lucene:searchIndex:query?analyzer=#whitespaceAnalyzer&amp;indexDir=#whitespace&amp;maxHits=20"/>
  <to uri="direct:next"/>
</route>

<route>
  <from uri="direct:next"/>
  <to uri="bean:searchResultProcessor"/>
  <to uri="mock:searchResult"/>
</route>
- route:
    from:
      uri: direct:start
      steps:
        - setHeader:
            name: CamelLuceneQuery
            constant: Seinfeld
        - to:
            uri: lucene:searchIndex:query
            parameters:
              analyzer: "#whitespaceAnalyzer"
              indexDir: "#whitespace"
              maxHits: 20
        - to:
            uri: direct:next

- route:
    from:
      uri: direct:next
      steps:
        - to:
            uri: bean:searchResultProcessor
        - to:
            uri: mock:searchResult

Example 3: Performing searches using a Query Processor

Java-only: uses LuceneQueryProcessor
from("direct:start")
    .setHeader("CamelLuceneQuery", constant("Rodney Dangerfield"))
    .process(new LuceneQueryProcessor("target/stdindexDir", analyzer, null, 20))
    .to("direct:next");

from("direct:next")
    .to("bean:searchResultProcessor")
    .to("mock:searchResult");