• <bdo id='Y0DGl'></bdo><ul id='Y0DGl'></ul>
  • <legend id='Y0DGl'><style id='Y0DGl'><dir id='Y0DGl'><q id='Y0DGl'></q></dir></style></legend>

      <tfoot id='Y0DGl'></tfoot>

        <small id='Y0DGl'></small><noframes id='Y0DGl'>

        <i id='Y0DGl'><tr id='Y0DGl'><dt id='Y0DGl'><q id='Y0DGl'><span id='Y0DGl'><b id='Y0DGl'><form id='Y0DGl'><ins id='Y0DGl'></ins><ul id='Y0DGl'></ul><sub id='Y0DGl'></sub></form><legend id='Y0DGl'></legend><bdo id='Y0DGl'><pre id='Y0DGl'><center id='Y0DGl'></center></pre></bdo></b><th id='Y0DGl'></th></span></q></dt></tr></i><div id='Y0DGl'><tfoot id='Y0DGl'></tfoot><dl id='Y0DGl'><fieldset id='Y0DGl'></fieldset></dl></div>

        如何在 Hibernate Search 中使用通配符和空格搜索字段

        How to search fields with wildcard and spaces in Hibernate Search(如何在 Hibernate Search 中使用通配符和空格搜索字段)

        <small id='vuim4'></small><noframes id='vuim4'>

          • <legend id='vuim4'><style id='vuim4'><dir id='vuim4'><q id='vuim4'></q></dir></style></legend>
            <i id='vuim4'><tr id='vuim4'><dt id='vuim4'><q id='vuim4'><span id='vuim4'><b id='vuim4'><form id='vuim4'><ins id='vuim4'></ins><ul id='vuim4'></ul><sub id='vuim4'></sub></form><legend id='vuim4'></legend><bdo id='vuim4'><pre id='vuim4'><center id='vuim4'></center></pre></bdo></b><th id='vuim4'></th></span></q></dt></tr></i><div id='vuim4'><tfoot id='vuim4'></tfoot><dl id='vuim4'><fieldset id='vuim4'></fieldset></dl></div>
              <tbody id='vuim4'></tbody>
                <bdo id='vuim4'></bdo><ul id='vuim4'></ul>
              • <tfoot id='vuim4'></tfoot>
                  本文介绍了如何在 Hibernate Search 中使用通配符和空格搜索字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个搜索框,它根据给定的输入对标题字段进行搜索,因此用户推荐了所有以插入的文本开头的可用标题.它基于 Lucene 和 Hibernate Search.在输入空间之前它工作正常.然后结果消失.例如,我希望Learning H"给我Learning Hibernate"作为结果.但是,这不会发生.你能告诉我我应该在这里用什么吗?

                  I have a search box that performs a search on title field based on the given input, so the user has recommended all available titles starting with the text inserted.It is based on Lucene and Hibernate Search. It works fine until space is entered. Then the result disapear. For example, I want "Learning H" to give me "Learning Hibernate" as the result. However, this doesn't happen. could you please advice me what should I use here instead.

                  查询生成器:

                  QueryBuilder qBuilder = fullTextSession.getSearchFactory()
                          .buildQueryBuilder().forEntity(LearningGoal.class).get();
                    Query query = qBuilder.keyword().wildcard().onField("title")
                          .matching(searchString + "*").createQuery();
                  
                    BooleanQuery bQuery = new BooleanQuery();
                    bQuery.add(query, 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);
                    }
                    @SuppressWarnings("unused")
                    org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(
                          query, LearningGoal.class);
                  

                  休眠类:

                  @AnalyzerDef(name = "searchtokenanalyzer",tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
                  filters = {
                    @TokenFilterDef(factory = StandardFilterFactory.class),
                    @TokenFilterDef(factory = LowerCaseFilterFactory.class),
                    @TokenFilterDef(factory = StopFilterFactory.class,params = { 
                        @Parameter(name = "ignoreCase", value = "true") }) })
                        @Analyzer(definition = "searchtokenanalyzer")
                  public class LearningGoal extends Node {
                  

                  推荐答案

                  我找到了解决这个问题的方法.这个想法是对输入字符串进行标记并删除停用词.对于最后一个标记,我使用关键字通配符创建了一个查询,对于之前的所有单词,我创建了一个 TermQuery.这是完整的代码

                  I found workaround for this problem. The idea is to tokenize input string and remove stop words. For the last token I created a query using keyword wildcard, and for the all previous words I created a TermQuery. Here is the full code

                      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);
                  

                  这篇关于如何在 Hibernate Search 中使用通配符和空格搜索字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Lucene Porter Stemmer not public(Lucene Porter Stemmer 未公开)
                  How to index pdf, ppt, xl files in lucene (java based or python or php any of these is fine)?(如何在 lucene 中索引 pdf、ppt、xl 文件(基于 java 或 python 或 php 中的任何一个都可以)?)
                  KeywordAnalyzer and LowerCaseFilter/LowerCaseTokenizer(KeywordAnalyzer 和 LowerCaseFilter/LowerCaseTokenizer)
                  How to search between dates (Hibernate Search)?(如何在日期之间搜索(休眠搜索)?)
                  How to get positions from a document term vector in Lucene?(如何从 Lucene 中的文档术语向量中获取位置?)
                  Java Lucene 4.5 how to search by case insensitive(Java Lucene 4.5如何按不区分大小写进行搜索)
                    <bdo id='lEQKn'></bdo><ul id='lEQKn'></ul>

                          <tbody id='lEQKn'></tbody>

                        1. <i id='lEQKn'><tr id='lEQKn'><dt id='lEQKn'><q id='lEQKn'><span id='lEQKn'><b id='lEQKn'><form id='lEQKn'><ins id='lEQKn'></ins><ul id='lEQKn'></ul><sub id='lEQKn'></sub></form><legend id='lEQKn'></legend><bdo id='lEQKn'><pre id='lEQKn'><center id='lEQKn'></center></pre></bdo></b><th id='lEQKn'></th></span></q></dt></tr></i><div id='lEQKn'><tfoot id='lEQKn'></tfoot><dl id='lEQKn'><fieldset id='lEQKn'></fieldset></dl></div>

                          <small id='lEQKn'></small><noframes id='lEQKn'>

                          • <legend id='lEQKn'><style id='lEQKn'><dir id='lEQKn'><q id='lEQKn'></q></dir></style></legend><tfoot id='lEQKn'></tfoot>