索引时如何添加前缀和后缀
How to add prefix and suffix when indexing
如何在索引期间为 Hibernate Search 中的实体添加后缀和前缀?
我需要它来执行精确搜索。
例如。如果正在搜索 "this is a test",则会找到以下条目:
* 这是一个测验
* 这是一个测试......
所以我想到了在索引过程中为整个值添加前缀和后缀的想法,例如:
_____这是一个测试_____
如果有人正在搜索 "this is a test" 并启用精确搜索复选框,我会将搜索字符串更改为_
“_____ 这是一个测试 _____”
我为此创建了一个 FilterFactory,但它为每个术语添加了前缀和后缀:
public boolean incrementToken() throws IOException {
if (!this.input.incrementToken()) {
return false;
} else {
String input = termAtt.toString();
// add "_____" at the beginning and ending of the phrase for exact match searching
input = "_____ " + input + " _____";
char[] newBuffer = input.toLowerCase().toCharArray();
termAtt.setEmpty();
termAtt.copyBuffer(newBuffer, 0, newBuffer.length);
return true;
}
}
这不是你应该做的。
你需要的是你索引的字符串被认为是一个唯一的标记。这样,您将只会得到具有确切标记的结果。
为此,您需要定义一个基于 KeywordTokenizer 的分析器。
@Entity
@AnalyzerDefs({
@AnalyzerDef(name = "keyword",
tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class)
)
})
@Indexed
public class YourEntity {
@Fields({
@Field, // your default field with default analyzer if you need it
@Field(name = "propertyKeyword", analyzer = @Analyzer(definition = "keyword"))
})
private String property;
}
那么您应该在 propertyKeyword 字段上进行搜索。请注意,分析器定义是全局的,因此您只需声明一个实体的定义,它就可用于您的所有实体。
查看有关分析器的文档:http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#example-analyzer-def .
了解分析器的用途很重要,因为通常默认的分析器并不完全是您要查找的那个。
如何在索引期间为 Hibernate Search 中的实体添加后缀和前缀?
我需要它来执行精确搜索。 例如。如果正在搜索 "this is a test",则会找到以下条目: * 这是一个测验 * 这是一个测试......
所以我想到了在索引过程中为整个值添加前缀和后缀的想法,例如: _____这是一个测试_____
如果有人正在搜索 "this is a test" 并启用精确搜索复选框,我会将搜索字符串更改为_ “_____ 这是一个测试 _____”
我为此创建了一个 FilterFactory,但它为每个术语添加了前缀和后缀:
public boolean incrementToken() throws IOException {
if (!this.input.incrementToken()) {
return false;
} else {
String input = termAtt.toString();
// add "_____" at the beginning and ending of the phrase for exact match searching
input = "_____ " + input + " _____";
char[] newBuffer = input.toLowerCase().toCharArray();
termAtt.setEmpty();
termAtt.copyBuffer(newBuffer, 0, newBuffer.length);
return true;
}
}
这不是你应该做的。
你需要的是你索引的字符串被认为是一个唯一的标记。这样,您将只会得到具有确切标记的结果。
为此,您需要定义一个基于 KeywordTokenizer 的分析器。
@Entity
@AnalyzerDefs({
@AnalyzerDef(name = "keyword",
tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class)
)
})
@Indexed
public class YourEntity {
@Fields({
@Field, // your default field with default analyzer if you need it
@Field(name = "propertyKeyword", analyzer = @Analyzer(definition = "keyword"))
})
private String property;
}
那么您应该在 propertyKeyword 字段上进行搜索。请注意,分析器定义是全局的,因此您只需声明一个实体的定义,它就可用于您的所有实体。
查看有关分析器的文档:http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#example-analyzer-def .
了解分析器的用途很重要,因为通常默认的分析器并不完全是您要查找的那个。