Hi Sanne,
I have similar problem. FieldBridge is not broken, but analyzer defined for the field is not applied.
Versions:
Hibernate Search - 3.4.0.Final
Hibernate Search Analyzers - 3.4.0.Final
Hibernate Core - 3.6.3.Final
Code:
Apps.java
-------------------------------------------------------------------------------
Code:
@AnalyzerDefs({
@AnalyzerDef(name="stemmedAnalyzer",
tokenizer = @TokenizerDef(factory= StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory=LowerCaseFilterFactory.class),
//@TokenFilterDef(factory=StandardFilterFactory.class),
@TokenFilterDef(factory=SnowballPorterFilterFactory.class,
params = @Parameter(name="language", value="English"))
}),
@AnalyzerDef(name="normalAnalyzer",
tokenizer = @TokenizerDef(factory= StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory=LowerCaseFilterFactory.class)
})
})
@Entity
@Table(name = "apps_readonly", uniqueConstraints = @UniqueConstraint(columnNames = {
"name", "appid" }))
@Indexed(index="Apps")
@Analyzer(definition="stemmedAnalyzer")
public class Apps implements java.io.Serializable {
@OneToOne
@JoinColumn(referencedColumnName="appid", name="appid")
@IndexedEmbedded(depth=1, prefix="metadata_")
public Metadata getMetadata(){
return this.metadata;
}
}
Metadata.java
--------------------------------------------------------
Code:
@Entity
@Table(name = "metadata", uniqueConstraints = @UniqueConstraint(columnNames = "appid"))
public class Metadata implements java.io.Serializable {
@Column(name = "description", nullable = false, length = 10000)
@Fields({
@Field(name = "stemmed_desc", index = Index.TOKENIZED, store = Store.NO),
@Field(name = "unstemmed_desc", index = Index.TOKENIZED, store = Store.NO, analyzer=@Analyzer(definition="normalAnalyzer"))
})
@FieldBridge(impl=ChunkDescriptionBridge.class, params=
{@Parameter( name="chunkSize", value="100" ), @Parameter( name="delimeter", value=" " )})
@Boost(value=0.0f)
public String getDescription() {
return this.description;
}
}
ChunkDescriptionBridge
--------------------------------------------------------------------
Code:
public class ChunkDescriptionBridge implements ParameterizedBridge, FieldBridge {
private Integer chunkSize = null;
private String delimeter = null;
private Integer noOfChunks = null;
@Override
public void set(String name, Object value, Document document,
LuceneOptions luceneOptions) {
String description = (String) value;
String desc1 = "";
String desc2 = "";
String desc3 = "";
String desc4 = "";
if(StringUtils.isNotEmpty(description)){
String[] descSplitArray = description.split("\\s+");
Integer descArrayLength = descSplitArray.length;
desc1 = StringUtils.join(descSplitArray, delimeter, 0, descArrayLength > chunkSize ? chunkSize: descArrayLength);
if(noOfChunks > 1 && descArrayLength >= chunkSize){
desc2 = StringUtils.join(descSplitArray, delimeter, chunkSize, descArrayLength > 2 * chunkSize ?
2 * chunkSize: descArrayLength);
if(descArrayLength >= 2 * chunkSize){
desc3 = StringUtils.join(descSplitArray, delimeter, 2 * chunkSize,
descArrayLength > 3 * chunkSize ? 3 * chunkSize: descArrayLength);
}
if(descArrayLength > 3 * chunkSize){
desc4 = StringUtils.join(descSplitArray, delimeter, 3 * chunkSize, descArrayLength);
}
}
}
Field descField = new Field(name + "1", desc1, luceneOptions.getStore(), luceneOptions.getIndex(),
luceneOptions.getTermVector());
descField.setBoost(0.3f);
document.add(descField);
if(noOfChunks > 1 ) {
descField = new Field(name + "2", desc2, luceneOptions.getStore(), luceneOptions.getIndex(),
luceneOptions.getTermVector());
descField.setBoost(0.03f);
document.add(descField);
descField = new Field(name + "3", desc3, luceneOptions.getStore(), luceneOptions.getIndex(),
luceneOptions.getTermVector());
descField.setBoost(0.003f);
document.add(descField);
descField = new Field(name + "4", desc4, luceneOptions.getStore(), luceneOptions.getIndex(),
luceneOptions.getTermVector());
descField.setBoost(0.003f);
document.add(descField);
}
}
@Override
public void setParameterValues(Map parameters) {
this.delimeter = (parameters.get("delimeter") == null ? " " : (String)parameters.get("delimeter"));
this.chunkSize = (parameters.get("chunkSize") == null ? 100 : Integer.parseInt((String)parameters.get("chunkSize")));
this.noOfChunks = (parameters.get("noOfChunks") == null ? 1 : Integer.parseInt((String)parameters.get("noOfChunks")));
}
}
Though I have applied normalAnalyzer on Metadata.description field, it still creates stemmed indices. But the field bridge was worked.
NOTE: Just to make sure I have tried with the option suggested by Michael. But it also did not work.
Code:
if(isNotEmpty(desc1)){
luceneOptions.addFieldToDocument(name + "1", desc1, document);
}