【问题标题】:Hibernate lucene search include "the" and "a" as part of searchHibernate lucene 搜索包括“the”和“a”作为搜索的一部分
【发布时间】:2015-01-08 16:43:14
【问题描述】:

我正在使用 Hibernate Lucene 来索引表 MOVIE。我想按标题列进行索引和搜索,包括“the”和“a”作为搜索的一部分。例如,“最后一个站着的人”“通缉犯”。 “The”和“A”作为搜索结果的一部分是相关的。

问题是如果我在@Field 中使用了 index=Index.UN_TOKENIZED,则无法进行搜索。如果我使用 Index.TOKENIZED,则无法使用“the”和“a”进行搜索。

谁能给我一些指导?提前致谢。

下面是sn-p的代码:

@Field(index = Index.UN_TOKENIZED, store = Store.NO)

要搜索的代码:

@Field(index = Index.UN_TOKENIZED, store = Store.NO)  <<< I've tried Index.TOKENIZED also.

@Column(name = "TITLE") 私有字符串标题;

要搜索的代码: FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);

QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Movie.class).get(); 查询 luceneQuery = queryBuilder.keyword().onField("title") .matching(title.toLowerCase()).createQuery();

return fullTextEntityManager.createFullTextQuery(luceneQuery, Movie.class) .getResultList();

【问题讨论】:

  • 我想我找到了解决方案,通过定义分析器

标签: hibernate search hibernate-search


【解决方案1】:

我认为这是一种解决方案,通过定义分析器。这将标记化,但不会排除常用词“the”。 此外,在搜索代码中,使用方法 QueryBuilder.phrase() 而不是 keyword(),以便根据搜索词组执行搜索。

@AnalyzerDef(name = "custom", tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class), filters = { @TokenFilterDef(工厂 = LowerCaseFilterFactory.class), @TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = { @Parameter(name = "language", value = "English") }) }) 公共类电影{

@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Analyzer(definition="custom")
private String title;

... }

要搜索的代码: QueryBuilder qb = fullTextEntityMgr.getSearchFactory() .buildQueryBuilder().forEntity(Movie.class).get();

查询 luceneQuery = qb.phrase().onField("title").sentence(term.toLowerCase()).createQuery(); javax.persistence.Query 查询 = fullTextEntityMgr.createFullTextQuery(luceneQuery, Movie.class);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    相关资源
    最近更新 更多