我找到了解决此问题的方法。这个想法是标记输入字符串并删除停用词。对于最后一个标记,我使用关键字通配符创建了查询,对于所有之前的单词,我创建了TermQuery。这是完整的代码
BooleanQuery bQuery = new BooleanQuery(); Session session = persistence.currentManager(); FullTextSession fullTextSession = Search.getFullTextSession(session); Analyzer analyzer = fullTextSession.getSearchFactory().getAnalyzer("searchtokenanalyzer"); QueryParser parser = new QueryParser(Version.LUCENE_35, "title", analyzer); String[] tokenized=null; try { Query query= parser.parse(searchString); String cleanedText=query.toString("title"); tokenized = cleanedText.split("\s"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } QueryBuilder qBuilder = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity(LearningGoal.class).get(); for(int i=0;i<tokenized.length;i++){ if(i==(tokenized.length-1)){ Query query = qBuilder.keyword().wildcard().onField("title") .matching(tokenized[i] + "*").createQuery(); bQuery.add(query, BooleanClause.Occur.MUST); }else{ Term exactTerm = new Term("title", tokenized[i]); bQuery.add(new TermQuery(exactTerm), BooleanClause.Occur.MUST); } } for (LearningGoal exGoal : existingGoals) { Term omittedTerm = new Term("id", String.valueOf(exGoal.getId())); bQuery.add(new TermQuery(omittedTerm), BooleanClause.Occur.MUST_NOT); } org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( bQuery, LearningGoal.class);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)