简体   繁体   English

Spring MVC Hibernate搜索查询

[英]spring mvc Hibernate Search query

I am working with spring, using hibernate, maven and mysql, i'm trying to make a simple site for movies and actors. 我正在使用spring,使用hibernate,maven和mysql,我试图为电影和演员制作一个简单的网站。 What i'd like is a search method, where you type a word, or part of a word/name and you will get some results, this is my query: 我想要的是一种搜索方法,在其中键入一个单词或单词/名称的一部分,您会得到一些结果,这是我的查询:

@Transactional
public List<Movie> searchForMovie(String searchText) {
    FullTextSession fullTextSession = Search.getFullTextSession(getSession());

    QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Movie.class).get();
    org.apache.lucene.search.Query query = qb.keyword().onFields("year", "title").matching(searchText)
            .createQuery();

    org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Movie.class);

    List<Movie> results = hibQuery.list();
    return results;
}

As you can see, I am using 'keyword', this means you would have to search for the whole word/name, not only part of it. 如您所见,我使用的是“关键字”,这意味着您将不得不搜索整个单词/名称,而不仅仅是其中一部分。 For example, if I want to find the movie 'hunger games' I would have to type 'hunger' or 'games', what I want is it to be possible to only write 'hunge' and still get results. 例如,如果我想找到电影“饥饿游戏”,我必须输入“ hunger”或“ games”,我想要的是可以只写“ hunge”并仍然得到结果。 I cant seem to find a query that will do this, thoughts? 我似乎找不到可以执行此操作的查询,想法?

You can use hibernate wildcard queries for this. 您可以为此使用休眠通配符查询。

? - represents a single character
* - represents any character sequence.

Please also note that for performance purposes, it is recommended that query do not start with ? 另请注意,出于性能目的,建议查询不要以?开头。 or either *. 或*。

   QueryBuilder qb = fullTextSession.getSearchFactory()
                .buildQueryBuilder().forEntity(Movie.class).get();
        org.apache.lucene.search.Query query = qb
                .keyword().wildcard().onFields("year", "title")
                .matching(searchText + "*")
                .createQuery();

See hibernate docs for complete reference. 请参阅休眠文档以获取完整参考。
5.1.2.3 Wildcard Queries. 5.1.2.3通配符查询。
http://docs.jboss.org/hibernate/search/4.4/reference/en-US/html/search-query.html#search-query-querydsl http://docs.jboss.org/hibernate/search/4.4/reference/zh-CN/html/search-query.html#search-query-querydsl

Just change your .matching line to the following: 只需将.matching行更改为以下内容:

.matching("*"+searchText+"*")

Use wildcards to get your results in any combination you deem fit. 使用通配符以您认为合适的任何组合获得结果。

Hope that works for you. 希望对您有用。

You can use fuzzySearch of Hibernate Search on keyword. 您可以对关键字使用Hibernate Search的FuzzySearch。 Your code will be like this : 您的代码将如下所示:

QueryBuilder qb = fullTextSession.getSearchFactory()
           .buildQueryBuilder().forEntity(Movie.class).get();

         org.apache.lucene.search.Query query = qb
           .keyword().fuzzy().withEditDistanceUpTo(1).withPrefixLength(1).onFields("year","title")
           .matching(searchText)
           .createQuery();

See hibernate docs for complete reference. 请参阅休眠文档以获取完整参考。

5.1.2.2 Fuzzy Queries. 5.1.2.2模糊查询。 http://docs.jboss.org/hibernate/search/4.4/reference/en-US/html/search-query.html#search-query-querydsl http://docs.jboss.org/hibernate/search/4.4/reference/zh-CN/html/search-query.html#search-query-querydsl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM