【发布时间】:2015-09-17 03:02:42
【问题描述】:
我想实现一个在数据库中搜索文本的搜索查询。 但是我总是得到一个空的结果,尽管文本出现在 DataSet 中。
public List<Object> getSearchVorgangResult( final int mandantId, final String searchValue, final List<Integer> projektIds,
final Field field,
final boolean isGrossKlein )
{
final EntityManager em = getEntityManager();
final FullTextEntityManager fullTextEntityManager =
org.hibernate.search.jpa.Search.getFullTextEntityManager( em );
// create native Lucene query unsing the query DSL
// alternatively you can write the Lucene query using the Lucene query parser
// or the Lucene programmatic API. The Hibernate Search DSL is recommended though
final QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity( Vorgang.class ).get();
final List<String> fieldNames = new ArrayList<>();
System.out.println( field );
if ( field == null )
{
for ( final Field classField : Vorgang.class.getDeclaredFields() )
{
fieldNames.add( classField.getName() );
}
}
else
{
fieldNames.add( field.getName() );
}
String[] fields = new String[fieldNames.size()];
fields = fieldNames.toArray( fields );
final org.apache.lucene.search.Query luceneQuery = qb
.keyword()
.onFields( fields ).ignoreAnalyzer()
.matching( searchValue )
.createQuery();
// wrap Lucene query in a javax.persistence.Query
final javax.persistence.Query jpaQuery =
fullTextEntityManager.createFullTextQuery( luceneQuery, Vorgang.class );
// execute search
return jpaQuery.getResultList();
}
我不知道为什么结果总是空的。
【问题讨论】:
-
在我看来,您创建了一个查询,但从不执行它。目录 dir = new RAMDirectory(); IndexReader idxReader = new IndexReader(dir); idxSearcher idxSearcher = new IndexSearcher(idxReader) 查询 q = new TermQuery(new Term(“field”, “value”)); idxSearcher.search(q);从这里:link
-
您有什么理由使用ignoreAnalyzer()?您可以尝试删除 ignoreAnalyzer() 并尝试 .matching( searchValue.toUpperCase() ) 或 .matching( "%SOMEVALUETHATEXISTS%" )。您还可以检查索引是否正确
-
如果我删除了 ignoreAnalyzer(),我会收到以下错误:org.hibernate.search.exception.EmptyQueryException: HSEARCH000146: 应用于字段 'anfrageEingangAm' 的查询字符串 ' ' 没有有意义的标记匹配。根据应用于此字段的分析器验证查询输入。 @SujitChaitanya
-
您所说的“在数据库中搜索文本”是什么意思?你把文本编入索引了吗?
-
我还添加了:“fullTextEntityManager.createIndexer(Vorgang.class).startAndWait();”现在我得到了 900 多个结果。那太多了。 @BoutayaBilal
标签: java hibernate search full-text-search hibernate-search