Skip to content

Commit cac7581

Browse files
committed
Improved search configuration, removed class dependency to deprecated
SnowBall analyzer.
1 parent 30551b2 commit cac7581

File tree

5 files changed

+70
-58
lines changed

5 files changed

+70
-58
lines changed

src/org/opencms/configuration/CmsSearchConfiguration.java

+22-42
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@
4444
import java.util.ArrayList;
4545
import java.util.Collections;
4646
import java.util.Iterator;
47-
import java.util.List;
4847
import java.util.Locale;
49-
import java.util.Map;
48+
import java.util.Map.Entry;
5049

5150
import org.apache.commons.digester.Digester;
5251

@@ -370,10 +369,7 @@ public Element generateXml(Element parent) {
370369

371370
// <documenttypes>
372371
Element documenttypesElement = searchElement.addElement(N_DOCUMENTTYPES);
373-
List docTypeKeyList = m_searchManager.getDocumentTypeConfigs();
374-
Iterator docTypeIterator = docTypeKeyList.iterator();
375-
while (docTypeIterator.hasNext()) {
376-
CmsSearchDocumentType currSearchDocType = (CmsSearchDocumentType)docTypeIterator.next();
372+
for (CmsSearchDocumentType currSearchDocType : m_searchManager.getDocumentTypeConfigs()) {
377373
// add the next <documenttype> element
378374
Element documenttypeElement = documenttypesElement.addElement(N_DOCUMENTTYPE);
379375
// add <name> element
@@ -383,30 +379,30 @@ public Element generateXml(Element parent) {
383379
// add <mimetypes> element
384380
Element mimetypesElement = documenttypeElement.addElement(N_MIMETYPES);
385381
// get the list of mimetypes to trigger the document factory class
386-
Iterator mimeTypesIterator = currSearchDocType.getMimeTypes().iterator();
382+
Iterator<String> mimeTypesIterator = currSearchDocType.getMimeTypes().iterator();
387383
while (mimeTypesIterator.hasNext()) {
388384
// add <mimetype> element(s)
389-
mimetypesElement.addElement(N_MIMETYPE).addText((String)mimeTypesIterator.next());
385+
mimetypesElement.addElement(N_MIMETYPE).addText(mimeTypesIterator.next());
390386
}
391387
// add <resourcetypes> element
392388
Element restypesElement = documenttypeElement.addElement(N_RESOURCETYPES);
393389
// get the list of Cms resource types to trigger the document factory
394-
Iterator resTypesIterator = currSearchDocType.getResourceTypes().iterator();
390+
Iterator<String> resTypesIterator = currSearchDocType.getResourceTypes().iterator();
395391
while (resTypesIterator.hasNext()) {
396392
// add <resourcetype> element(s)
397-
restypesElement.addElement(N_RESOURCETYPE).addText((String)resTypesIterator.next());
393+
restypesElement.addElement(N_RESOURCETYPE).addText(resTypesIterator.next());
398394
}
399395
}
400396
// </documenttypes>
401397

402398
// <analyzers>
403399
Element analyzersElement = searchElement.addElement(N_ANALYZERS);
404-
List analyzerLocaleList = new ArrayList(m_searchManager.getAnalyzers().keySet());
400+
ArrayList<Locale> analyzerLocaleList = new ArrayList<Locale>(m_searchManager.getAnalyzers().keySet());
405401
// sort Analyzers in ascending order
406402
Collections.sort(analyzerLocaleList, CmsLocaleComparator.getComparator());
407-
Iterator analyzersLocaleInterator = analyzerLocaleList.iterator();
403+
Iterator<Locale> analyzersLocaleInterator = analyzerLocaleList.iterator();
408404
while (analyzersLocaleInterator.hasNext()) {
409-
CmsSearchAnalyzer searchAnalyzer = m_searchManager.getCmsSearchAnalyzer((Locale)analyzersLocaleInterator.next());
405+
CmsSearchAnalyzer searchAnalyzer = m_searchManager.getCmsSearchAnalyzer(analyzersLocaleInterator.next());
410406
// add the next <analyzer> element
411407
Element analyzerElement = analyzersElement.addElement(N_ANALYZER);
412408
// add <class> element
@@ -422,9 +418,7 @@ public Element generateXml(Element parent) {
422418

423419
// <indexes>
424420
Element indexesElement = searchElement.addElement(N_INDEXES);
425-
Iterator indexIterator = m_searchManager.getSearchIndexes().iterator();
426-
while (indexIterator.hasNext()) {
427-
CmsSearchIndex searchIndex = (CmsSearchIndex)indexIterator.next();
421+
for (CmsSearchIndex searchIndex : m_searchManager.getSearchIndexes()) {
428422
// add the next <index> element
429423
Element indexElement = indexesElement.addElement(N_INDEX);
430424
// add class attribute (if required)
@@ -447,10 +441,10 @@ public Element generateXml(Element parent) {
447441
// add <sources> element
448442
Element sourcesElement = indexElement.addElement(N_SOURCES);
449443
// iterate above sourcenames
450-
Iterator sourcesIterator = searchIndex.getSourceNames().iterator();
444+
Iterator<String> sourcesIterator = searchIndex.getSourceNames().iterator();
451445
while (sourcesIterator.hasNext()) {
452446
// add <source> element
453-
sourcesElement.addElement(N_SOURCE).addText((String)sourcesIterator.next());
447+
sourcesElement.addElement(N_SOURCE).addText(sourcesIterator.next());
454448
}
455449
// iterate additional params
456450
CmsParameterConfiguration indexConfiguration = searchIndex.getConfiguration();
@@ -462,10 +456,7 @@ public Element generateXml(Element parent) {
462456

463457
// <indexsources>
464458
Element indexsourcesElement = searchElement.addElement(N_INDEXSOURCES);
465-
List indexSources = new ArrayList(m_searchManager.getSearchIndexSources().values());
466-
Iterator indexsourceIterator = indexSources.iterator();
467-
while (indexsourceIterator.hasNext()) {
468-
CmsSearchIndexSource searchIndexSource = (CmsSearchIndexSource)indexsourceIterator.next();
459+
for (CmsSearchIndexSource searchIndexSource : m_searchManager.getSearchIndexSources().values()) {
469460
// add <indexsource> element(s)
470461
Element indexsourceElement = indexsourcesElement.addElement(N_INDEXSOURCE);
471462
// add <name> element
@@ -474,39 +465,32 @@ public Element generateXml(Element parent) {
474465
Element indexerElement = indexsourceElement.addElement(N_INDEXER).addAttribute(
475466
N_CLASS,
476467
searchIndexSource.getIndexerClassName());
477-
Map params = searchIndexSource.getParams();
478-
Iterator paramIterator = params.entrySet().iterator();
479-
while (paramIterator.hasNext()) {
480-
Map.Entry entry = (Map.Entry)paramIterator.next();
481-
String name = (String)entry.getKey();
482-
String value = (String)entry.getValue();
468+
for (Entry<String, String> entry : searchIndexSource.getParams().entrySet()) {
483469
// add <param name=""> element(s)
484470
indexerElement.addElement(I_CmsXmlConfiguration.N_PARAM).addAttribute(
485471
I_CmsXmlConfiguration.A_NAME,
486-
name).addText(value);
472+
entry.getKey()).addText(entry.getValue());
487473
}
488474
// add <resources> element
489475
Element resourcesElement = indexsourceElement.addElement(N_RESOURCES);
490-
Iterator resourceIterator = searchIndexSource.getResourcesNames().iterator();
476+
Iterator<String> resourceIterator = searchIndexSource.getResourcesNames().iterator();
491477
while (resourceIterator.hasNext()) {
492478
// add <resource> element(s)
493-
resourcesElement.addElement(N_RESOURCE).addText((String)resourceIterator.next());
479+
resourcesElement.addElement(N_RESOURCE).addText(resourceIterator.next());
494480
}
495481
// add <documenttypes-indexed> element
496482
Element doctypes_indexedElement = indexsourceElement.addElement(N_DOCUMENTTYPES_INDEXED);
497-
Iterator doctypesIterator = searchIndexSource.getDocumentTypes().iterator();
483+
Iterator<String> doctypesIterator = searchIndexSource.getDocumentTypes().iterator();
498484
while (doctypesIterator.hasNext()) {
499485
// add <name> element(s)
500-
doctypes_indexedElement.addElement(N_NAME).addText((String)doctypesIterator.next());
486+
doctypes_indexedElement.addElement(N_NAME).addText(doctypesIterator.next());
501487
}
502488
}
503489
// </indexsources>
504490

505491
// <fieldconfigurations>
506492
Element fieldConfigurationsElement = searchElement.addElement(N_FIELDCONFIGURATIONS);
507-
Iterator configs = m_searchManager.getFieldConfigurations().iterator();
508-
while (configs.hasNext()) {
509-
CmsSearchFieldConfiguration fieldConfiguration = (CmsSearchFieldConfiguration)configs.next();
493+
for (CmsSearchFieldConfiguration fieldConfiguration : m_searchManager.getFieldConfigurations()) {
510494
Element fieldConfigurationElement = fieldConfigurationsElement.addElement(N_FIELDCONFIGURATION);
511495
// add class attribute (if required)
512496
if (!fieldConfiguration.getClass().equals(CmsSearchFieldConfiguration.class)) {
@@ -518,9 +502,7 @@ public Element generateXml(Element parent) {
518502
}
519503
// search fields
520504
Element fieldsElement = fieldConfigurationElement.addElement(N_FIELDS);
521-
Iterator fields = fieldConfiguration.getFields().iterator();
522-
while (fields.hasNext()) {
523-
CmsSearchField field = (CmsSearchField)fields.next();
505+
for (CmsSearchField field : fieldConfiguration.getFields()) {
524506
Element fieldElement = fieldsElement.addElement(N_FIELD);
525507
fieldElement.addAttribute(A_NAME, field.getName());
526508
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(field.getDisplayNameForConfiguration())) {
@@ -562,9 +544,7 @@ public Element generateXml(Element parent) {
562544
fieldElement.addAttribute(A_ANALYZER, className);
563545
}
564546
// field mappings
565-
Iterator mappings = field.getMappings().iterator();
566-
while (mappings.hasNext()) {
567-
CmsSearchFieldMapping mapping = (CmsSearchFieldMapping)mappings.next();
547+
for (CmsSearchFieldMapping mapping : field.getMappings()) {
568548
Element mappingElement = fieldElement.addElement(N_MAPPING);
569549
mappingElement.addAttribute(A_TYPE, mapping.getType().toString());
570550
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(mapping.getDefaultValue())) {

src/org/opencms/search/CmsSearchAnalyzer.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class CmsSearchAnalyzer {
4545
/** A locale as a key to select the analyzer. */
4646
private Locale m_locale;
4747

48-
/** The stemmer algorithm to be used. */
48+
/** The stemmer algorithm to be used (only for the deprecated SnowBall analyzer). */
4949
private String m_stemmerAlgorithm;
5050

5151
/**
@@ -83,6 +83,9 @@ public String getLocaleString() {
8383
/**
8484
* Returns the stemmer algorithm.<p>
8585
*
86+
* This is required only for the SnowBall analyzer, which is deprecated in Lucene and should not longer be
87+
* used since all languages have special analyzers now.<p>
88+
*
8689
* @return the stemmer algorithm
8790
*/
8891
public String getStemmerAlgorithm() {
@@ -125,6 +128,9 @@ public void setLocaleString(String locale) {
125128
/**
126129
* Sets the stemmer algorithm.<p>
127130
*
131+
* This is required only for the SnowBall analyzer, which is deprecated in Lucene and should not longer be
132+
* used since all languages have special analyzers now.<p>
133+
*
128134
* @param stemmerAlgorithm the stemmer algorithm
129135
*/
130136
public void setStemmerAlgorithm(String stemmerAlgorithm) {

src/org/opencms/search/CmsSearchIndex.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public synchronized IndexReader reopen() throws CorruptIndexException, IOExcepti
207207
public static final String LUCENE_USE_COMPOUND_FILE = "lucene.UseCompoundFile";
208208

209209
/** The Lucene Version used to create Query parsers and such. */
210-
public static final Version LUCENE_VERSION = Version.LUCENE_33;
210+
public static final Version LUCENE_VERSION = Version.LUCENE_34;
211211

212212
/** Constant for additional parameter for controlling how many hits are loaded at maximum (default: 1000). */
213213
public static final String MAX_HITS = CmsSearchIndex.class.getName() + ".maxHits";

src/org/opencms/search/CmsSearchManager.java

+39-13
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272

7373
import org.apache.commons.logging.Log;
7474
import org.apache.lucene.analysis.Analyzer;
75-
import org.apache.lucene.analysis.snowball.SnowballAnalyzer;
7675
import org.apache.lucene.analysis.standard.StandardAnalyzer;
7776
import org.apache.lucene.index.IndexWriter;
7877
import org.apache.lucene.search.Similarity;
@@ -500,6 +499,20 @@ public CmsSearchManager() {
500499
}
501500
}
502501

502+
/**
503+
* Returns an analyzer for the given class name.<p>
504+
*
505+
* @param className the class name of the analyzer
506+
*
507+
* @return the appropriate lucene analyzer
508+
*
509+
* @throws Exception if something goes wrong
510+
*/
511+
public static Analyzer getAnalyzer(String className) throws Exception {
512+
513+
return getAnalyzer(className, null);
514+
}
515+
503516
/**
504517
* Returns an analyzer for the given class name.<p>
505518
*
@@ -508,13 +521,16 @@ public CmsSearchManager() {
508521
* This method will create analyzers by name for the {@link CmsSearchIndex#LUCENE_VERSION} version.<p>
509522
*
510523
* @param className the class name of the analyzer
511-
* @param snowballStemmer the optional snowball stemmer parameter required for the snowball analyzer
524+
* @param stemmer the optional stemmer parameter required for the snowball analyzer
512525
*
513526
* @return the appropriate lucene analyzer
514527
*
515528
* @throws Exception if something goes wrong
529+
*
530+
* @deprecated The stemmer parameter is used only by the snownall analyzer, which is deprecated in Lucene 3.
516531
*/
517-
public static Analyzer getAnalyzer(String className, String snowballStemmer) throws Exception {
532+
@Deprecated
533+
public static Analyzer getAnalyzer(String className, String stemmer) throws Exception {
518534

519535
Analyzer analyzer = null;
520536
Class<?> analyzerClass;
@@ -532,12 +548,10 @@ public static Analyzer getAnalyzer(String className, String snowballStemmer) thr
532548
} else if (CmsGallerySearchAnalyzer.class.equals(analyzerClass)) {
533549
// OpenCms gallery multiple language analyzer
534550
analyzer = new CmsGallerySearchAnalyzer(CmsSearchIndex.LUCENE_VERSION);
535-
} else if ((snowballStemmer != null) && SnowballAnalyzer.class.equals(analyzerClass)) {
536-
// the Snowball analyzer is used
537-
analyzer = new SnowballAnalyzer(CmsSearchIndex.LUCENE_VERSION, snowballStemmer);
538551
} else {
539552
boolean hasEmpty = false;
540553
boolean hasVersion = false;
554+
boolean hasVersionWithString = false;
541555
// another analyzer is used, check if we find a suitable constructor
542556
Constructor<?>[] constructors = analyzerClass.getConstructors();
543557
for (int i = 0; i < constructors.length; i++) {
@@ -551,15 +565,27 @@ public static Analyzer getAnalyzer(String className, String snowballStemmer) thr
551565
// a constructor with a Lucene version parameter has been found
552566
hasVersion = true;
553567
}
554-
if (hasVersion) {
555-
// a constructor with a Lucene version parameter has been found
556-
analyzer = (Analyzer)analyzerClass.getDeclaredConstructor(new Class[] {Version.class}).newInstance(
557-
CmsSearchIndex.LUCENE_VERSION);
558-
} else if (hasEmpty) {
559-
// an empty constructor has been found
560-
analyzer = (Analyzer)analyzerClass.newInstance();
568+
if ((stemmer != null)
569+
&& (parameters.length == 2)
570+
&& parameters[0].equals(Version.class)
571+
&& parameters[1].equals(String.class)) {
572+
// a constructor with a Lucene version parameter and a String has been found
573+
hasVersionWithString = true;
561574
}
562575
}
576+
if (hasVersionWithString) {
577+
// a constructor with a Lucene version parameter and a String has been found
578+
analyzer = (Analyzer)analyzerClass.getDeclaredConstructor(new Class[] {Version.class, String.class}).newInstance(
579+
CmsSearchIndex.LUCENE_VERSION,
580+
stemmer);
581+
} else if (hasVersion) {
582+
// a constructor with a Lucene version parameter has been found
583+
analyzer = (Analyzer)analyzerClass.getDeclaredConstructor(new Class[] {Version.class}).newInstance(
584+
CmsSearchIndex.LUCENE_VERSION);
585+
} else if (hasEmpty) {
586+
// an empty constructor has been found
587+
analyzer = (Analyzer)analyzerClass.newInstance();
588+
}
563589
}
564590
return analyzer;
565591
}

src/org/opencms/search/fields/CmsSearchField.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ public void setAnalyzer(Analyzer analyzer) {
593593
*/
594594
public void setAnalyzer(String analyzer) throws Exception {
595595

596-
setAnalyzer(CmsSearchManager.getAnalyzer(analyzer, null));
596+
setAnalyzer(CmsSearchManager.getAnalyzer(analyzer));
597597
}
598598

599599
/**

0 commit comments

Comments
 (0)