1

在我的项目中,我们使用带有 lucene-analyzers 和 solar 的 hibernate search 4.5。我为我的客户提供一个文本字段。当他们输入短语时,我想查找User名称中包含给定短语的所有实体。

例如,考虑在数据库中有以下标题的条目列表:

[ Alan Smith, John Cane, Juno Taylor, Tom Caner Junior ]

jun应该返回Juno Taylor并且Tom Caner Junior

an应该返回Alan SmithJohn Cane并且Tom Caner Junior

    @AnalyzerDef(name = "customanalyzer", tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class), filters = {
            @TokenFilterDef(factory = LowerCaseFilterFactory.class),
            @TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = { @Parameter(name = "language", value = "English") })

    })
@Analyzer(definition = "customanalyzer")
    public class Student implements Serializable {

        @Column(name = "Fname")
        @Field(index = Index.YES, store = Store.YES, analyze = Analyze.YES)
        private String fname;

        @Column(name = "Lname")
        @Field(index = Index.YES, store = Store.YES, analyze = Analyze.YES)
        private String lname;

    }

我尝试过通配符搜索,但是

通配符查询不会将分析器应用于匹配项。否则*或的风险?被破坏太高了。

Query luceneQuery = mythQB
    .keyword()
      .wildcard()
    .onFields("fname")
    .matching("ju*")
    .createQuery();

我怎样才能做到这一点?

4

2 回答 2

5

首先,您没有将分析器分配给您的字段,因此当前未使用它。您应该使用@Field.analyzer。

其次,要回答您的问题,最好使用EdgeNGramFilter. 您应该将此过滤器添加到分析器定义中。

编辑:另外,为了防止诸如“sathya”之类的查询匹配“sanchana”,您应该在查询时使用不同的分析器。

下面是一个完整的例子。

@AnalyzerDef(name = "customanalyzer", tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class), filters = {
        @TokenFilterDef(factory = LowerCaseFilterFactory.class),
        @TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = { @Parameter(name = "language", value = "English") })
        @TokenFilterDef(factory = EdgeNGramFilterFactory.class, params = { @Parameter(name = "maxGramSize", value = "15") })

})
@AnalyzerDef(name = "customanalyzer_query", tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class), filters = {
        @TokenFilterDef(factory = LowerCaseFilterFactory.class),
        @TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = { @Parameter(name = "language", value = "English") })

})
public class Student implements Serializable {

    @Column(name = "Fname")
    @Field(index = Index.YES, store = Store.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "customanalyzer"))
    private String fname;

    @Column(name = "Lname")
    @Field(index = Index.YES, store = Store.YES, analyze = Analyze.YES, analyzer = @Analyzer(definition = "customanalyzer")))
    private String lname;

}

然后特别提到你想在构建查询时使用这个“查询”分析器:

QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Student.class)
    // Here come the assignments of "query" analyzers
    .overridesForField( "fname", "customanalyzer_query" )
    .overridesForField( "lname", "customanalyzer_query" )
    .get();
// Then it's business as usual
Query luceneQuery = queryBuilder.keyword().onFields("fname", "lname").matching("sathya").createQuery();
FullTextQuery query = fullTextEntityManager.createFullTextQuery(luceneQuery, Student.class);

另请参阅:https ://stackoverflow.com/a/43047342/6692043


顺便说一句,如果您的数据仅包含名字和姓氏,则不应使用词干 ( SnowballPorterFilterFactory):它只会无缘无故地降低搜索的准确性。

于 2017-05-17T15:45:10.437 回答
0

为什么不使用标准TypedQuery

String term您的搜索词在哪里)

TypedQuery<Student> q = em.createQuery(
        "SELECT s " +
        "FROM Student s " +
        "WHERE s.fname like :search " +
        "OR s.lname like :search";
q.setParameter("search", "%" + term + "%");

没有测试这个,但是像这样的东西应该可以解决问题。

于 2017-05-17T14:53:30.170 回答