camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Lucene
Date Fri, 08 Jan 2010 04:22:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Lucene">Lucene</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~akarpe">Ashwin
Karpe</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="Lucene-Lucene%28IndexerandSearch%29Component"></a>Lucene
(Indexer and Search) Component</h2>

<p><b>Available as of Camel 2.2</b></p>

<p>The <b>lucene</b> 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 </p>
<ul>
	<li><a href="http://lucene.apache.org/java/docs/" rel="nofollow">http://lucene.apache.org/java/docs/</a></li>
	<li><a href="http://lucene.apache.org/java/docs/features.html" rel="nofollow">http://lucene.apache.org/java/docs/features.html</a></li>
</ul>


<p>The lucene component in camel facilitates integration and utilization of Lucene endpoints
in enterprise integration patterns and scenarios. The lucene component does the following</p>
<ul>
	<li>builds a searchable index of documents when payloads are sent to the Lucene Endpoint</li>
	<li>facilitates performing of indexed searches in Camel</li>
</ul>


<p>This component only supports producer endpoints. </p>

<h3><a name="Lucene-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
lucene:searcherName:insert[?options]
lucene:searcherName:query[?options]
</pre>
</div></div>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="Lucene-InsertOptions"></a>Insert Options</h3>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>analyzer</tt> </td>
<td class='confluenceTd'> <tt>StandardAnalyzer</tt> </td>
<td class='confluenceTd'> An Analyzer builds TokenStreams, which analyze text. It thus
represents a policy for extracting index terms from text. The value for analyzer can be any
class that extends the abstract class org.apache.lucene.analysis.Analyzer. Lucene also offers
a rich set of analyzers out of the box </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>indexDir</tt> </td>
<td class='confluenceTd'> <tt>./indexDirectory</tt> </td>
<td class='confluenceTd'> A file system directory in which index files are created upon
analysis of the document by the specified analyzer </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>srcDir</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> An optional directory containing files to be used to be analyzed
and added to the index at producer startup. </td>
</tr>
</tbody></table>

<h3><a name="Lucene-QueryOptions"></a>Query Options</h3>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>analyzer</tt> </td>
<td class='confluenceTd'> <tt>StandardAnalyzer</tt> </td>
<td class='confluenceTd'> An Analyzer builds TokenStreams, which analyze text. It thus
represents a policy for extracting index terms from text. The value for analyzer can be any
class that extends the abstract class org.apache.lucene.analysis.Analyzer. Lucene also offers
a rich set of analyzers out of the box </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>indexDir</tt> </td>
<td class='confluenceTd'> <tt>./indexDirectory</tt> </td>
<td class='confluenceTd'> A file system directory in which index files are created upon
analysis of the document by the specified analyzer </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxHits</tt> </td>
<td class='confluenceTd'> <tt>10</tt> </td>
<td class='confluenceTd'> An integer value that limits the result set of the search
operation </td>
</tr>
</tbody></table>

<h3><a name="Lucene-Sending%2FReceivingMessagesto%2Ffromthecache"></a>Sending/Receiving
Messages to/from the cache</h3>

<h4><a name="Lucene-MessageHeaders"></a>Message Headers</h4>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>QUERY</tt> </td>
<td class='confluenceTd'> The Lucene Query to performed on the index. The query may
include wildcards and phrases </td>
</tr>
</tbody></table>

<h4><a name="Lucene-LuceneProducers"></a>Lucene Producers </h4>
<p>This component supports 2 producer endpoints.</p>
<ul>
	<li>insert - The insert producer builds a searchable index by analyzing the body in
incoming exchanges and associating it with a token ("content").</li>
	<li>query - The query producer performs searches on a pre-created index. The query
uses the searchable index to perform score &amp; 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 <a href="http://lucene.apache.org/java/3_0_0/queryparsersyntax.html" rel="nofollow">http://lucene.apache.org/java/3_0_0/queryparsersyntax.html</a></li>
</ul>


<h4><a name="Lucene-LuceneProcessor"></a>Lucene Processor </h4>
<p>There is a processor called LuceneQueryProcessor available to perform queries against
lucene without the need to create a producer.</p>

<h3><a name="Lucene-LuceneUsageSamples"></a>Lucene Usage Samples</h3>

<h4><a name="Lucene-Example1%3ACreatingaLuceneindex"></a>Example 1: Creating
a Lucene index</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
RouteBuilder builder = <span class="code-keyword">new</span> RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
       from(<span class="code-quote">"direct:start"</span>).
           to(<span class="code-quote">"lucene:whitespaceQuotesIndex:insert?analyzer=#whitespaceAnalyzer&amp;indexDir=#whitespace&amp;srcDir=#load_dir"</span>).
           to(<span class="code-quote">"mock:result"</span>);
    }
};
</pre>
</div></div>

<h4><a name="Lucene-Example2%3ALoadingpropertiesintotheJNDIregistryintheCamelContext"></a>Example
2: Loading properties into the JNDI registry in the Camel Context </h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
@Override
<span class="code-keyword">protected</span> JndiRegistry createRegistry() <span
class="code-keyword">throws</span> Exception {
  JndiRegistry registry =
         <span class="code-keyword">new</span> JndiRegistry(createJndiContext());
  registry.bind(<span class="code-quote">"whitespace"</span>, <span class="code-keyword">new</span>
File(<span class="code-quote">"./whitespaceIndexDir"</span>));
  registry.bind(<span class="code-quote">"load_dir"</span>,
        <span class="code-keyword">new</span> File(<span class="code-quote">"src/test/resources/sources"</span>));
  registry.bind(<span class="code-quote">"whitespaceAnalyzer"</span>,
        <span class="code-keyword">new</span> WhitespaceAnalyzer());
  <span class="code-keyword">return</span> registry;
}
...
CamelContext context = <span class="code-keyword">new</span> DefaultCamelContext(createRegistry());
</pre>
</div></div>

<h4><a name="Lucene-Example2%3APerformingsearchesusingaQueryProducer"></a>Example
2: Performing searches using a Query Producer</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
RouteBuilder builder = <span class="code-keyword">new</span> RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
       from(<span class="code-quote">"direct:start"</span>).
          setHeader(<span class="code-quote">"QUERY"</span>, constant(<span
class="code-quote">"Seinfeld"</span>)).
          to(<span class="code-quote">"lucene:searchIndex:query?analyzer=#whitespaceAnalyzer&amp;indexDir=#whitespace&amp;maxHits=20"</span>).
          to(<span class="code-quote">"direct:next"</span>);
                
       from(<span class="code-quote">"direct:next"</span>).process(<span class="code-keyword">new</span>
Processor() {
          <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
             Hits hits = exchange.getIn().getBody(Hits.class);
             printResults(hits);
          }

          <span class="code-keyword">private</span> void printResults(Hits hits)
{
              LOG.debug(<span class="code-quote">"<span class="code-object">Number</span>
of hits: "</span> + hits.getNumberOfHits());
              <span class="code-keyword">for</span> (<span class="code-object">int</span>
i = 0; i &lt; hits.getNumberOfHits(); i++) {
                 LOG.debug(<span class="code-quote">"Hit "</span> + i + <span
class="code-quote">" Index Location:"</span> + hits.getHit().get(i).getHitLocation());
                 LOG.debug(<span class="code-quote">"Hit "</span> + i + <span
class="code-quote">" Score:"</span> + hits.getHit().get(i).getScore());
                 LOG.debug(<span class="code-quote">"Hit "</span> + i + <span
class="code-quote">" Data:"</span> + hits.getHit().get(i).getData());
              }
           }
       }).to(<span class="code-quote">"mock:searchResult"</span>);
   }
};
</pre>
</div></div>

<h4><a name="Lucene-Example3%3APerformingsearchesusingaQueryProcessor"></a>Example
3: Performing searches using a Query Processor </h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
RouteBuilder builder = <span class="code-keyword">new</span> RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {            
        <span class="code-keyword">try</span> {
            from(<span class="code-quote">"direct:start"</span>).
                setHeader(<span class="code-quote">"QUERY"</span>, constant(<span
class="code-quote">"Rodney Dangerfield"</span>)).
                process(<span class="code-keyword">new</span> LuceneQueryProcessor(<span
class="code-quote">"target/stdindexDir"</span>, analyzer, <span class="code-keyword">null</span>,
20)).
                to(<span class="code-quote">"direct:next"</span>);
        } <span class="code-keyword">catch</span> (Exception e) {
            e.printStackTrace();
        }
                
        from(<span class="code-quote">"direct:next"</span>).process(<span class="code-keyword">new</span>
Processor() {
            <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
                Hits hits = exchange.getIn().getBody(Hits.class);
                printResults(hits);
            }
                    
            <span class="code-keyword">private</span> void printResults(Hits hits)
{
                LOG.debug(<span class="code-quote">"<span class="code-object">Number</span>
of hits: "</span> + hits.getNumberOfHits());
                <span class="code-keyword">for</span> (<span class="code-object">int</span>
i = 0; i &lt; hits.getNumberOfHits(); i++) {
                    LOG.debug(<span class="code-quote">"Hit "</span> + i + <span
class="code-quote">" Index Location:"</span> + hits.getHit().get(i).getHitLocation());
                    LOG.debug(<span class="code-quote">"Hit "</span> + i + <span
class="code-quote">" Score:"</span> + hits.getHit().get(i).getScore());
                    LOG.debug(<span class="code-quote">"Hit "</span> + i + <span
class="code-quote">" Data:"</span> + hits.getHit().get(i).getData());
                }
            }
       }).to(<span class="code-quote">"mock:searchResult"</span>);
   }
};
</pre>
</div></div>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Lucene">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=10846211&revisedVersion=2&originalVersion=1">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Lucene?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message