In order to do searching you need a lucene Directory where the lucene generated
index resides.
Moreover you must know the lucene Analyzer which has been used for
indexing, and which will be used for searching.
Knowing this you can may start searching having a query which is parsable
by an QueryParser, and having the name of the default field to use in
searching.
This class returns an Hit object as its search result.
| Field Summary |
|---|
| protected static final String | ANALYZER_CLASSNAME_CONFIG | Configuration element name of lucene's Analyzer class.
Its value is
analyzer-classname.
|
| protected static final String | ANALYZER_CLASSNAME_DEFAULT | Configuration element default value of lucene's Analyzer class.
Its value is,
org.apache.lucene.analysis.standard.StandardAnalyzer.
|
| protected static final String | DEFAULT_SEARCH_FIELD_CONFIG | Configuration element name of default search field.
Its value is
default-seach-field.
|
| protected static final String | DEFAULT_SEARCH_FIELD_DEFAULT | Configuration element default value of lucene's default search field.
Its value is body.
|
| protected static final String | DEFAULT_QUERY_CONFIG | Configuration element name of default-query.
Its value is
default-query.
|
| protected static final String | DEFAULT_QUERY_DEFAULT | Configuration element default value of default-query.
Its value is null.
|
| protected static final String | QUERYPARSER_CLASSNAME_CONFIG | Configuration element name of query parser class name.
Its value is
queryparser-classname.
|
| protected static final String | QUERYPARSER_CLASSNAME_DEFAULT | Configuration element default value of queryparser-classname.
Its value is
org.apache.lucene.queryParser.QueryParser.
|
| protected static final String | DIRECTORY_CONFIG | Configuration element name of lucene's default filesystem default
directory.
Its value is directory.
|
| protected static final String | DIRECTORY_DEFAULT | Configuration element default value of filesystem default directory.
Its value is null.
|
| protected ServiceManager | manager | The service manager instance |
| Method from org.apache.cocoon.components.search.SimpleLuceneCocoonSearcherImpl Detail: |
public void configure(Configuration conf) throws ConfigurationException {
Configuration child;
String value;
child = conf.getChild(ANALYZER_CLASSNAME_CONFIG, false);
if (child != null) {
// fix Bugzilla Bug 25277, use child.getValue
// and in all following blocks
value = child.getValue(ANALYZER_CLASSNAME_DEFAULT);
if (value != null) {
analyzerClassnameDefault = value;
try {
analyzer = (Analyzer) ClassUtils.newInstance(analyzerClassnameDefault);
} catch (Exception e) {
throw new ConfigurationException("Cannot create analyzer of class " +
analyzerClassnameDefault, e);
}
}
}
child = conf.getChild(DEFAULT_SEARCH_FIELD_CONFIG, false);
if (child != null) {
value = child.getValue(DEFAULT_SEARCH_FIELD_DEFAULT);
if (value != null) {
defaultSearchFieldDefault = value;
}
}
child = conf.getChild(DEFAULT_QUERY_CONFIG, false);
if (child != null) {
value = child.getValue(DEFAULT_QUERY_DEFAULT);
if (value != null) {
defaultQueryDefault = value;
}
}
/*
child = conf.getChild(QUERYPARSER_CLASSNAME_CONFIG, false);
if (child != null) {
value = child.getValue(QUERYPARSER_CLASSNAME_DEFAULT);
if (value != null) {
queryparserClassnameDefault = value;
}
}
*/
child = conf.getChild(DIRECTORY_CONFIG, false);
if (child != null) {
value = child.getValue(DIRECTORY_DEFAULT);
if (value != null) {
directoryDefault = value;
try {
setDirectory(FSDirectory.getDirectory(new File(directoryDefault), false));
} catch (IOException ioe) {
throw new ConfigurationException("Cannot set index directory " + directoryDefault, ioe);
}
}
}
}
|
public void dispose() {
releaseIndexSearcher();
releaseIndexReaderCache();
}
Dispose this component, releasing IndexSearcher, and IndexReaderCache. |
public Analyzer getAnalyzer() {
return this.analyzer;
}
|
public IndexReader getReader() throws IOException {
if (indexReaderCache == null) {
indexReaderCache = new IndexReaderCache();
}
return indexReaderCache.getIndexReader(directory);
}
Get an IndexReader.
As an IndexReader might be cached, it is check if the indexReader is
still valid.
|
public void recycle() {
releaseIndexSearcher();
releaseIndexReaderCache();
}
Recycle this component, releasing IndexSearcher, and IndexReaderCache. |
public Hits search(Query query) throws ProcessingException {
Hits hits = null;
try {
// release index searcher for each new search
releaseIndexSearcher();
IndexSearcher indexSearcher = new IndexSearcher(getReader());
hits = indexSearcher.search(query);
// do not close indexSearcher now, as using hits needs an
// opened indexSearcher indexSearcher.close();
} catch (IOException ioe) {
throw new ProcessingException("Cannot access hits", ioe);
}
return hits;
}
Search lucene index.
This method is designed to be used by other components, or Flowscripts |
public Hits search(String query_string,
String default_field) throws ProcessingException {
Hits hits = null;
if (query_string == null) {
query_string = defaultQueryDefault;
}
if (default_field == null) {
default_field = defaultSearchFieldDefault;
}
try {
Query query = QueryParser.parse(query_string, default_field, analyzer);
// release index searcher for each new search
releaseIndexSearcher();
IndexSearcher indexSearcher = new IndexSearcher(getReader());
hits = indexSearcher.search(query);
// do not close indexSearcher now, as using hits needs an
// opened indexSearcher indexSearcher.close();
} catch (ParseException pe) {
throw new ProcessingException("Cannot parse query " + query_string, pe);
} catch (IOException ioe) {
throw new ProcessingException("Cannot access hits", ioe);
}
return hits;
}
|
public void service(ServiceManager manager) throws ServiceException {
this.manager = manager;
}
Set the current ServiceManager instance used by this
Serviceable. |
public void setAnalyzer(Analyzer analyzer) {
this.analyzer = analyzer;
}
set an analyzer, overriding the analyzerClassnameDefault. |
public void setDirectory(Directory directory) {
this.directory = directory;
if (indexReaderCache != null) {
indexReaderCache.close();
indexReaderCache = null;
}
}
Sets the directory attribute of the SimpleLuceneCocoonSearcherImpl object |