This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'search-replace-indexing' into search-replace
- Loading branch information
Showing
4 changed files
with
157 additions
and
57 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
server/zanata-model/src/main/java/org/zanata/hibernate/search/StringListBridge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.zanata.hibernate.search; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.document.Field; | ||
import org.hibernate.search.bridge.FieldBridge; | ||
import org.hibernate.search.bridge.LuceneOptions; | ||
import org.hibernate.search.bridge.ParameterizedBridge; | ||
import org.jboss.seam.annotations.Logger; | ||
import org.jboss.seam.log.Log; | ||
|
||
/** | ||
* Index a list of strings in multiple fields, appending the string index to the | ||
* field name to produce unique fields. | ||
* | ||
* e.g. For a field labeled 'fieldName' for a list of 3 strings | ||
* <ul> | ||
* <li>First string is indexed as 'fieldName0'</li> | ||
* <li>Second string is indexed as 'fieldName1'</li> | ||
* <li>Third string is indexed as 'fieldName2'</li> | ||
* </ul> | ||
* | ||
* @author David Mason, damason@redhat.com | ||
* | ||
*/ | ||
public class StringListBridge implements FieldBridge, ParameterizedBridge | ||
{ | ||
|
||
@Logger | ||
Log log; | ||
|
||
private ConfigurableNgramAnalyzer analyzer; | ||
|
||
@Override | ||
public void setParameterValues(@SuppressWarnings("rawtypes") Map parameters) | ||
{ | ||
if (parameters.containsKey("case")) | ||
{ | ||
String caseBehaviour = (String) parameters.get("case"); | ||
if ("fold".equals(caseBehaviour)) | ||
{ | ||
analyzer = new DefaultNgramAnalyzer(); | ||
} | ||
else if ("preserve".equals(caseBehaviour)) | ||
{ | ||
analyzer = new CaseSensitiveNgramAnalyzer(); | ||
} | ||
else | ||
{ | ||
log.warn("invalid value for parameter \"case\": \"{0}\", default will be used", caseBehaviour); | ||
analyzer = new DefaultNgramAnalyzer(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void set(String name, Object value, Document luceneDocument, LuceneOptions luceneOptions) | ||
{ | ||
if (analyzer == null) | ||
{ | ||
analyzer = new DefaultNgramAnalyzer(); | ||
} | ||
|
||
if (!(value instanceof List<?>)) | ||
{ | ||
throw new IllegalArgumentException("this bridge must be applied to a List"); | ||
} | ||
List<String> strings = (List<String>) value; | ||
for (int i = 0; i < strings.size(); i++) | ||
{ | ||
addStringField(name + i, strings.get(i), luceneDocument, luceneOptions); | ||
} | ||
} | ||
|
||
private void addStringField(String fieldName, String fieldValue, Document luceneDocument, LuceneOptions luceneOptions) | ||
{ | ||
Field field = new Field(fieldName, fieldValue, luceneOptions.getStore(), luceneOptions.getIndex(), luceneOptions.getTermVector()); | ||
field.setBoost(luceneOptions.getBoost()); | ||
|
||
// manually apply token stream from analyzer, as hibernate search does not | ||
// apply the specified analyzer properly | ||
try | ||
{ | ||
field.setTokenStream(analyzer.reusableTokenStream(fieldName, new StringReader(fieldValue))); | ||
} | ||
catch (IOException e) | ||
{ | ||
log.error("Failed to get token stream from analyzer for field \"{0}\" with content \"{1}\"", e, fieldName, fieldValue); | ||
} | ||
luceneDocument.add(field); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.