1

有没有办法在 Hibernate Search 中找到所有搜索词的结果?我的意思是,例如,我搜索一个给定的术语,如“house”,然后得到一个“Houses”列表。但是,如果我想优化搜索并查找“house”和“pool”,我想获得“house”和“pool”的列表,而不是“house”或“pool”的列表. 我不确定我是否足够清楚。所以,基本上,我需要的是用一个以上的词进行搜索,搜索必须考虑所有的词,而不仅仅是一个词。那可能吗?谢谢

4

5 回答 5

4

也许这就是你想要的,假设你有一个字符串数组要搜索。

BooleanQuery finalLuceneQuery=new BooleanQuery();
for(String term:terms)
{
  TermQuery query = new TermQuery(new Term("your search field", term));

  finalLuceneQuery.add(query,BooleanClause.Occur.MUST);
}

FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(finalLuceneQuery, <your class>.class);

干杯。

于 2013-08-26T08:48:31.140 回答
2

在多个字段上搜索时,可以指定在每个字段中出现的 MUST、SHOULD 或 MUST_NOT 术语。以下代码基于 Lucene 4.10.3。

// fields we are interested in
String[] fields = {"description", "content"};
// terms SHOULD appear in description OR MUST appear in content
BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD, BooleanClause.Occur.MUST};
// besides the StandardAnalyzer there are analysers for many languages like
// FrenchAnalyser, RussianAnalyser and so on
Analyzer analyzer = new StandardAnalyzer();
// create a multi field query using parse static method
org.apache.lucene.search.Query luceneQuery = 
    MultiFieldQueryParser.parse("house pool", fields, flags, analyzer);
// convert to Hibernate query
org.hibernate.search.FullTextQuery hibQuery =
    session.createFullTextQuery(luceneQuery, YourClass.class);

如果使用 JPA,您需要将其转换为 JPA 查询:

// get an EntityManager from your EntityManagerFactory (emf)
javax.persistence.EntityManager em = emf.createEntityManager();
// Convert it to a FullTextEntityManager
org.hibernate.search.jpa.FullTextEntityManager ftem =
    org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
// convert it to a JPA query
javax.persistence.Query jpaQuery =
    ftem.createFullTextQuery(luceneQuery, YourClass.class);

获取 Lucene 查询的另一种方法(可能更简单?):

// fields we are interested in
String[] fields = {"description", "content"};
// besides the StandardAnalyzer there are analysers for many languages like
// FrenchAnalyser, RussianAnalyser and so on
Analyzer analyzer = new StandardAnalyzer();
// to set MUST for all fields we need an instance of the parser
MultiFieldQueryParser mfqp = new MultiFieldQueryParser(fields, analyzer);
mfqp.setDefaultOperator(QueryParser.Operator.AND);
org.apache.lucene.search.Query luceneQuery = mfqp.parse("house pool");
于 2015-01-09T22:57:51.543 回答
2

您可以使用 QueryParser 将默认运算符设置为 AND:

QueryParser queryParser = new QueryParser(Version.LUCENE_36, "yourfield",standardAnalyzer);     
// query terms should ANDed by default
queryParser.setDefaultOperator(Operator.AND);
Query luceneQuery = queryParser.parse("house pool");
// turn it into an hibernate search query
org.hibernate.search.FullTextQuery hibQuery =
session.createFullTextQuery(luceneQuery, YourClass.class);
于 2013-10-24T12:47:37.007 回答
0

这里不需要使用任何Analyzer因为 Hibernate 隐含地使用StandardAnalyzer它来拆分单词,white spaces所以这里的解决方案设置为AnalyzeNO会自动执行的字段Multi Phrase Search

@Column(name="skill")
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO)
@Analyzer(definition="SkillsAnalyzer")
private String skill;
于 2017-05-01T11:25:58.907 回答
0

在查询构建期间使用关键字 MUST。Hibernate Search 还支持使用各种策略组合查询:

-应该:查询应该包含子查询的匹配元素

-MUST:查询必须包含子查询的匹配元素

-MUST NOT:查询不能包含子查询的匹配元素

聚合类似于布尔值 AND、OR 和 NOT。

    Query combinedQuery = queryBuilder
   .bool()

   .must(queryBuilder.phrase()
   .onField("productName).sentence("samsung galaxy s8")
   .createQuery())

   .must(queryBuilder.keyword()
   .onField("productCategory").matching("smartphone")
   .createQuery())

   .createQuery();


    // wrap Lucene query in an Hibernate Query object
    org.hibernate.search.jpa.FullTextQuery jpaQuery =
    fullTextEntityManager.createFullTextQuery(combinedQuery, Product.class);

    // execute search and return results (sorted by relevance as default)
    @SuppressWarnings("unchecked")
    List<Product> results = jpaQuery.getResultList();

此参考来自链接http://www.baeldung.com/hibernate-search

于 2018-01-14T06:45:18.413 回答