如何在多个字段中使用通配符进行搜索? [英] How to search with wildcards in multiple fields?

查看:151
本文介绍了如何在多个字段中使用通配符进行搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现基于hibernate-search的搜索。
我设法根据某些条件合并搜索字段,但在这种情况下,我无法使用通配符进行搜索。

 列表< String> spalten = new ArrayList< String>(); 

FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search
.getFullTextEntityManager(em);

QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder()。forEntity(BeitragVO.class).get();

列表< BeitragVO>结果;
//添加搜索字段
List< String> fields = new ArrayList< String>();

if(student){
logger.info(hit hit student);
spalten.add(user.surname);
spalten.add(user.givenname);
}
if(company){
logger.info(hit company);
spalten.add(company.name);


for(String string:fields){
logger.info(string);
}
logger.info(Searchterm:+ searchterm);
org.apache.lucene.search.Query luceneQuery = qb.keyword()
.onFields(fields.toArray(new String [fields.size()]))
.matching(searchterm ).createQuery();

logger.info(Query:+ luceneQuery.toString());

javax.persistence.Query jpaQuery = fullTextEntityManager
.createFullTextQuery(luceneQuery,ArticleVO.class);

results = jpaQuery.getResultList();

在我的搜索输入框中,我有六个复选框,用户可以选中或不选。
如果用户检查学生,搜索仅适用于学生的表格。这工作非常好!

问题:
搜索输入:Alexan
(对于Alexander)



没有结果

我试过类似的东西,用于通配符搜索:

  TermContext tc = qb.keyword(); 

WildcardContext wcc = tc.wildcard();
if(student){

TermMatchingContext tmc = wcc.onField(user.surname);
tmc = tmc.andField(user.givenname);
luceneQuery = tmc.matching(searchterm).createQuery();



$ b 这是正确的语法,我得到正确的查询(user.surname:alexander user.givenname:alexander)。但仍然是一个空的结果列表。



我非常喜欢没有通配符的尝试。有没有办法在这种尝试中添加通配符搜索?

解决方案

通配符'*'应该可以工作。实际上,您需要构建如下内容:

  qb.keyword()
.wildcard()
。 onField(user.givenname)
.matching(Alex *)
.createQuery();

显然,您需要分解DSL来添加一些条件逻辑,但它应该起作用。您确定您的示例中的'searchterm'是否添加了通配符?



分解DSL的另一种方法是创建多个独立查询并通过 BooleanQuery 。我认为这会更具可读性。



问题/错误/功能已修复!你可以在这里看到:



https: //hibernate.atlassian.net/browse/HSEARCH-1811


I'm trying to implement a search based on hibernate-search. I managed to combine search-fields based on some condition, but in this case, I can not search with wildcards.

    List<String> spalten = new ArrayList<String>();

    FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search
            .getFullTextEntityManager(em);

    QueryBuilder qb = fullTextEntityManager.getSearchFactory()
            .buildQueryBuilder().forEntity(BeitragVO.class).get();

    List<BeitragVO> results;
        // add fields for search
        List<String> fields= new ArrayList<String>();

        if (student) {
            logger.info("hit student");
            spalten.add("user.surname");
            spalten.add("user.givenname");
        }
        if (company) {
            logger.info("hit company");
            spalten.add("company.name");
        }

        for (String string : fields) {
            logger.info(string);
        }
        logger.info("Searchterm:" + searchterm);
        org.apache.lucene.search.Query luceneQuery = qb.keyword()
                .onFields(fields.toArray(new String[fields.size()]))
                .matching(searchterm).createQuery();

        logger.info("Query: " + luceneQuery.toString());

        javax.persistence.Query jpaQuery = fullTextEntityManager
                .createFullTextQuery(luceneQuery, ArticleVO.class);

        results= jpaQuery.getResultList();

Under my search input, I got six checkboxes, the user can check or not. If the user checks student, the search only applies for the table for students. This works totally fine!

Problem: Search input: Alexan (for Alexander)

No result is found!

I tried something like this, for wildcard search:

    TermContext tc = qb.keyword();

    WildcardContext wcc = tc.wildcard();
       if (student) {

            TermMatchingContext tmc = wcc.onField("user.surname");
            tmc = tmc.andField("user.givenname");
            luceneQuery = tmc.matching(searchterm).createQuery();
        }

This is correct syntax and I get the correct query (user.surname:alexander user.givenname: alexander). But still an empty resultlist.

I really like the attempt without wildcards. Is there a way to add wildcard-search on this attempt?

解决方案

With the wildcard '*' it should work. Effectively you need to build something like:

qb.keyword()
  .wildcard()
  .onField( "user.givenname" )
  .matching( "Alex*" )
  .createQuery(); 

Apparently, you need to decompose the DSL to add some conditional logic, but it should work. Are you sure that 'searchterm' in your example has the wildcard added?

An alternative to decomposing the DSL could be to create multiple independent queries and combine them via a BooleanQuery. I think that would be more readable.

The issue/bug/feature got fixed! You can see it here:

https://hibernate.atlassian.net/browse/HSEARCH-1811

这篇关于如何在多个字段中使用通配符进行搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆