2015-08-23 70 views
0

我的程序只返回準確的值等於與搜索字符串:Hibernate的搜索只返回精確值

trans = session.beginTransaction(); 
FullTextSession fullTextSession = Search.getFullTextSession(session); 
fullTextSession.createIndexer().startAndWait(); 
QueryBuilder qB = fullTextSession.getSearchFactory().buildQueryBuilder() 
.forEntity(Customer.class).get(); 

org.apache.lucene.search.Query luceneQuery = qB.keyword() 
.onFields("lastName").matching(searchString).createQuery(); 

Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery); 
list = fullTextQuery.list(); 

例如:當搜索字符串是「用戶」我將得到名單,但是當搜索字符串是「用戶」或'使用',我會得到空。

+0

對於一些示例,這也是一篇很棒的文章:https://dzone.com/articles/hibernate-search-based – MichaelCleverly

回答

2

我不知道你如何索引你的領域,似乎你沒有使用一個合適的分析器。閱讀有關它的文檔。

嘗試使用自定義分析器,使用hibernate搜索註釋和Solr分析器,Tokenizers和過濾器。

這是example可以幫助你開始:

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

    @Id 
    @GeneratedValue 
    @DocumentId 
    private Integer id; 

    @Field 
    @Analyzer(definition = "customanalyzer") 
    private String title; 

    @Field 
    @Analyzer(definition = "customanalyzer") 
    private String subtitle; 

    @IndexedEmbedded 
    @ManyToMany 
    private Set<Author> authors = new HashSet<Author>(); 

    @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES) 
    @DateBridge(resolution = Resolution.DAY) 
    private Date publicationDate; 

    public Book() { 
    } 

    // standard getters/setters follow here 
    ... 
} 

使用這個例子的分析,你就可以找到「用戶」和「用戶」 ......

爲了能夠找到「用戶」當您的關鍵字是「用」你CA:

  1. 使用其他分析儀像「EdgeNGramFilterFactory」。 。。

  2. ,或者使用wildcard queries這樣的:

    查詢luceneQuery = qb.keyword()通配符()onField( 「姓氏」)匹配( 「使用*」)的createQuery();

該文檔很簡單,學習Hibernate Search的最好方法就是閱讀它。