带重音的休眠搜索查询

时间:2014-11-02 23:26:48

标签: java lucene hibernate-search accent-insensitive

我在hibernate 4.2.15和spring 3.2.10中使用了hibernate search 4.2.0。 当使用hibernate搜索(lucene)查询时,我有一个奇怪的行为。

在数据库中,我为字段内容提供了此值:“méchant”。 当我用“mechant”进行查询时,它运行正常,我得到了objet。 但是当我使用“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;
    }

    ...
]

hibernate配置:

<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);

有人可以帮助我吗?

[编辑]谢谢大家,我解决了问题:这不是因为hibernate搜索而是我的http请求的字符集。我将检查如何修复我的charset问题。 抱歉浪费时间......

1 个答案:

答案 0 :(得分:0)

在您的示例中,您只使用SnowballPorterFilterFactory而未指定语言参数。这默认为英语,这可能不是你想要的。您是否尝试将其更改为您定位的语言?