【发布时间】:2014-12-29 14:29:15
【问题描述】:
我将 hibernate search 4.2.0 与 hibernate 4.2.15 和 spring 3.2.10 一起使用。 使用休眠搜索 (lucene) 查询时,我有一个奇怪的行为。
在数据库中,我为字段内容设置了这个值:“méchant”。 当我使用“mechant”进行查询时,它工作正常,我得到了对象。 但是当我使用“méchant”时它不起作用......
映射:
@Entity
@Indexed
@AnalyzerDef(name = "customAnalyzer",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class)
})
@Table(name = "MESSAGE")
public class Message {
...
@Id
@DocumentId
@GeneratedValue
@Column(name = "ID_MESSAGE")
public Integer getId() {
return id;
}
@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
@Analyzer(definition="customAnalyzer")
@Column(name = "CONTENT", length = 65535, columnDefinition = "Text")
public String getContent() {
return content;
}
...
]
休眠配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="customDataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.bytecode.provider">javassist</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">${hibernate.showSql}</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
<prop key="hibernate.search.default.indexBase">${indexLucene.path}</prop>
</props>
</property>
</bean>
查询:
FullTextSession searchSession = Search.getFullTextSession(getSessionFactory().getCurrentSession());
QueryBuilder qb = searchSession.getSearchFactory().buildQueryBuilder().forEntity(Message.class).get();
BooleanJunction<BooleanJunction> bool = qb.bool();
...
bool.must(qb.keyword().boostedTo(4f)
.onFields("content")
.matching(messageCriteria.getQuery())
.createQuery());
...
org.apache.lucene.search.Query luceneQuery =bool.createQuery();
FullTextQuery jpaQuery = searchSession.createFullTextQuery(luceneQuery, Message.class);
有人可以帮助我吗?
[编辑] 谢谢大家,我解决了这个问题:这不是由于休眠搜索,而是我的 http 请求的字符集。我将检查如何解决我的字符集问题。 抱歉浪费了时间...
【问题讨论】:
-
似乎很清楚,您在查询时使用了错误的分析器。我真的不太清楚 Hibernate 在查询时如何处理分析器,所以这里只是猜测,但是如果你将
onFields("content")更改为onField("content")会发生什么? -
如果我使用
onField("content"),没有变化:/
标签: java lucene hibernate-search accent-insensitive