<tfoot id='s8cfE'></tfoot>

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

      <bdo id='s8cfE'></bdo><ul id='s8cfE'></ul>

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

      1. Java Lucene 4.5如何按不区分大小写进行搜索

        Java Lucene 4.5 how to search by case insensitive(Java Lucene 4.5如何按不区分大小写进行搜索)

      2. <tfoot id='kfzf1'></tfoot>

        1. <legend id='kfzf1'><style id='kfzf1'><dir id='kfzf1'><q id='kfzf1'></q></dir></style></legend>
            <tbody id='kfzf1'></tbody>

        2. <small id='kfzf1'></small><noframes id='kfzf1'>

            • <bdo id='kfzf1'></bdo><ul id='kfzf1'></ul>
              <i id='kfzf1'><tr id='kfzf1'><dt id='kfzf1'><q id='kfzf1'><span id='kfzf1'><b id='kfzf1'><form id='kfzf1'><ins id='kfzf1'></ins><ul id='kfzf1'></ul><sub id='kfzf1'></sub></form><legend id='kfzf1'></legend><bdo id='kfzf1'><pre id='kfzf1'><center id='kfzf1'></center></pre></bdo></b><th id='kfzf1'></th></span></q></dt></tr></i><div id='kfzf1'><tfoot id='kfzf1'></tfoot><dl id='kfzf1'><fieldset id='kfzf1'></fieldset></dl></div>
                  本文介绍了Java Lucene 4.5如何按不区分大小写进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们已经实现了 Java Lucene 搜索引擎 4.5,即使字段值不区分大小写,我也会尝试搜索内容(例如,如果我搜索名称为Banglore"的城市,我会得到结果,但是当我搜索名称为banglore"的城市我得到 0 个结果).

                  We have implemented Java Lucene search engine 4.5, I am trying to search the content even if the field value is case insensitive (e.g., if I search a city with name "Banglore" I get a result, but when I search a city with name "banglore" I get 0 results).

                  我使用 StandardAnalyzer 分析数据并使用 WildcardQuery 来匹配 Like 条件(我尝试了上述 这里没有成功).

                  I have used StandardAnalyzer for analyzing the data and WildcardQuery to match a Like condition (I tried as mentioned here without success).

                  我不确定我哪里出错了.感谢任何有关解决此区分大小写问题的指导.

                  I am not sure where I have gone wrong. I appreciate any guidance on fixing this case sensitivity problem.

                  public SearchHelper
                  {
                      Analyzer analyzer;
                  
                      Directory index;
                      public IndexSearcher searcher = null;
                      public IndexWriter indexWriter = null;
                      public QueryParser parser = null;
                      private static int hitsPerPage = 100;
                  
                      /**
                       * @param indexFileLocation
                       * @throws IOException
                       */
                      public SearchHelper(String indexFileLocation) throws IOException
                      {
                  //        this.analyzer =new StandardAnalyzer();
                          this.analyzer = new CaseStandardAnalyzer();
                  //        analyzer = new ThaiAnalyzer();
                          this.index = FSDirectory.open(java.nio.file.Paths.get(indexFileLocation));
                      }
                  
                      /**
                       * @param create
                       * @return
                       * @throws IOException
                       */
                      public IndexWriter getIndexWriter(boolean create) throws IOException
                      {
                          if (indexWriter == null)
                          {
                              IndexWriterConfig iwc = new IndexWriterConfig(this.analyzer);
                              this.indexWriter = new IndexWriter(this.index, iwc);
                          }
                          return this.indexWriter;
                      } //End of getIndexWriter
                  
                      /**
                       * @throws IOException
                       */
                      public void closeIndexWriter() throws IOException
                      {
                          if (this.indexWriter != null)
                          {
                               this.indexWriter.commit();//optimize(); LUCENE_36
                               this.indexWriter.close();
                          }
                      } //End closeIndexWriter
                  
                      /**
                       * @param indexFileLocation
                       * @throws CorruptIndexException
                       * @throws IOException
                       */
                      public void startSearch(String indexFileLocation) throws CorruptIndexException, IOException
                      {
                  //        searcher = new IndexSearcher(FSDirectory.open(new File(indexFileLocation)));
                  
                          IndexReader reader = DirectoryReader.open(FSDirectory.open(java.nio.file.Paths.get(indexFileLocation)));
                  //        IndexReader.open(this.index);
                  //        open(getIndexWriter(true), true);
                          this.searcher = new IndexSearcher(reader);
                      }
                  
                      /**
                       * @param fieldNames
                       * @param fieldValues
                       * @return
                       * @throws IOException
                       * @throws ParseException
                       * 
                       * <p></p>
                       * https://stackoverflow.com/questions/2005084/how-to-specify-two-fields-in-lucene-queryparser
                       */
                      public ScoreDoc[] searchSEO(String[] fieldNames, String[] fieldValues, int limitSize) throws IOException, ParseException
                      {
                          this.analyzer = new StandardAnalyzer();
                          int searchFieldSize = (null == fieldNames) ? 0 : fieldNames.length;
                  
                          BooleanQuery booleanQuery = new BooleanQuery();
                  
                          for (int i = 0; i < searchFieldSize; i++)
                          {
                               Query query1 = searchIndexWithWildcardQuery(fieldNames[i], fieldValues[i]);                
                               addQueries(booleanQuery, query1, 2);               
                          }
                  
                          TopScoreDocCollector collector = null; // Or use by default hitsPerPage instead limitSize
                  
                          if (limitSize > 0)
                          {
                              collector = TopScoreDocCollector.create(limitSize);
                          } else {
                              collector = TopScoreDocCollector.create(hitsPerPage);
                          }
                  
                          this.searcher.search(booleanQuery,collector);
                  
                          return  collector.topDocs().scoreDocs;
                      }
                  
                      /**
                       * @param whichField
                       * @param searchString
                       * @return
                       * @throws IOException
                       * @throws ParseException
                       */
                      public Query searchIndexWithWildcardQuery(String whichField, String searchString) throws IOException, ParseException
                      {
                          Term term = addTerm(whichField, "*" + searchString + "*");
                          Query query = new WildcardQuery(term);
                          return query;
                      }
                  
                      /**
                       * @param whichField
                       * @param searchString
                       * @return
                       */
                      public Term addTerm(String whichField, String searchString)
                      {
                          Term term = new Term(whichField, searchString);
                          return term;
                      }
                  
                      /**
                       * @param searchString
                       * @param operation
                       * @return
                       * @throws ParseException
                       */
                      public Query addConditionOpertaion(String searchString, String operation) throws ParseException
                      {
                          Query query = null;
                          if ("and".equals(operation))
                          {
                              parser.setDefaultOperator(QueryParser.AND_OPERATOR);
                          } else if("or".equals(operation)) {
                              parser.setDefaultOperator(QueryParser.AND_OPERATOR);
                          }
                  
                          query = parser.parse(searchString);
                          return query;
                      }
                  
                      /**
                       * @param booleanQuery <code>BooleanQuery</code>
                       * @param q <code>Query</code>
                       * @param type <code>int</code> , 1--> Must, 2-->Should, 3 --> Must Not
                       */
                      public void addQueries(BooleanQuery booleanQuery, Query q, int type)
                      {
                          switch(type)
                          {
                              case 1: booleanQuery.add(q, Occur.MUST);
                                      break;
                              case 2: booleanQuery.add(q, Occur.SHOULD);
                                      break;
                              default:booleanQuery.add(q, Occur.MUST_NOT);
                                      break;
                          } //End of switch
                      }
                  
                      public QueryParser getParser()
                      {
                          return parser;
                      }
                  
                      public void setParser(String fieldName)
                      {
                          this.parser = new QueryParser(fieldName, this.analyzer);
                      }
                  
                      public void getDefaultByStatus(int status)
                      {
                          this.analyzer = new StandardAnalyzer();
                          this.parser = new QueryParser("status", this.analyzer);
                      }
                  
                      protected void doClear(File dir,boolean deleteSubDir)
                      {
                          for (File file: dir.listFiles())
                          {
                              if (file.isDirectory() && deleteSubDir)
                              {
                                  doClear(file,deleteSubDir);
                              }
                              file.delete();
                          }
                      } //End of doClear();
                  
                      protected void doClose() throws IOException
                      {
                          this.searcher.getIndexReader().close();
                      }
                  
                      public boolean add(Object Obj) throws Exception
                      {
                          User currentUser = (User)Obj;
                          boolean isAdded = false;
                  
                          org.apache.lucene.document.Document luceneDoc = new org.apache.lucene.document.Document();
                          luceneDoc.add(new IntField("oid", currentUser.getOid(), Field.Store.YES));
                          luceneDoc.add(new IntField("status", currentUser.getStatus(), Field.Store.YES));
                          luceneDoc.add(new StringField("login", currentUser.getLogin(), Field.Store.YES));
                          luceneDoc.add(new StringField("fName", currentUser.getFirstName(), Field.Store.YES));
                          luceneDoc.add(new StringField("lName", currentUser.getLastName(), Field.Store.NO));
                          luceneDoc.add(new StringField("email", currentUser.getEmailId(), Field.Store.YES));
                          luceneDoc.add(new StringField("city", currentUser.getCity(), Field.Store.YES));
                  
                  //        addRelatedFields(luceneDoc,city.getStateCode());
                  
                          IndexWriter writer = getIndexWriter(false);
                          writer.addDocument(luceneDoc);
                  
                          closeIndexWriter();
                  
                          isAdded = true;
                          System.out.println(isAdded);
                          return isAdded;
                      } // End of add
                  
                      public boolean update(Object Obj) throws Exception
                      {
                          boolean isUpdated = false;
                          User currentUser = (User) Obj;
                  
                          org.apache.lucene.document.Document luceneDoc = new org.apache.lucene.document.Document();
                  //        luceneDoc.add(new IntField("oid", currentUser.getOid(), Field.Store.YES));
                          luceneDoc.add(new IntField("oid", currentUser.getOid(), Field.Store.YES));
                          luceneDoc.add(new StringField("login", currentUser.getLogin(), Field.Store.YES));
                          luceneDoc.add(new IntField("status", currentUser.getStatus(), Field.Store.YES));
                          luceneDoc.add(new StringField("fName", currentUser.getFirstName(), Field.Store.YES));
                          luceneDoc.add(new StringField("lName", currentUser.getLastName(), Field.Store.NO));
                          luceneDoc.add(new StringField("email", currentUser.getEmailId(), Field.Store.YES));
                          luceneDoc.add(new StringField("city", currentUser.getCity(), Field.Store.YES));
                  
                  //        addRelatedFields(luceneDoc,city.getStateCode());
                  
                          IndexWriter writer = getIndexWriter(false);
                          writer.updateDocument(new Term("login", currentUser.getLogin()),luceneDoc); 
                          closeIndexWriter();
                  
                          isUpdated = true;
                          return isUpdated;
                      } // End of update
                  
                      public boolean delete(Object Obj) throws Exception
                      {
                          boolean isDeleted = false;
                          User currentUser = (User) Obj;      
                  
                          Term deleteTerm = new Term("login", currentUser.getLogin());
                  
                          IndexWriter writer = getIndexWriter(false);
                          writer.deleteDocuments(deleteTerm); // Or use Query
                          writer.forceMergeDeletes();
                          closeIndexWriter();
                  
                          isDeleted = true;
                  
                          return isDeleted;
                      } // End of delete
                  
                      @Override
                      public Object search(String[] fieldNames, String[] fieldValues, int returnType, int limit) throws Exception
                      {
                          Object obj = null;
                          org.apache.lucene.search.ScoreDoc[] hits =  searchSEO(fieldNames,fieldValues,  limit);
                          int hitSize = (null == hits) ? 0 : hits.length;
                  
                          System.out.println("total:" + hitSize);
                  
                          doClose();
                          return obj;
                      } // End of search
                  
                      public void addThreadUser()
                      {
                          User user = new User();
                          addUserPojo(user);    
                          add(user);
                      }
                  
                      public void updateThreadUser()
                      {
                          User user = new User();
                          addUserPojo(user);
                          update(user);
                      }
                  
                      public void deleteThreadUser()
                      {
                          User user = new User();
                          addUserPojo(user);   
                          delete(user);
                      }
                  
                      private void addUserPojo(User user)
                      {
                          user.setOid(3);
                          user.setLogin("senthil");
                          user.setFirstName("Semthil");
                          user.setLastName("Semthil");
                          user.setStatus(1);
                          user.setCity("Combiatore");
                          user.setEmailId("semthil@xyz.com");
                      }
                  
                      public void searchUser()
                      {
                          searchUser(new String[] {"login"}, new String[] {"Se"}, null);
                      }
                  
                      public static void main(String[] args)
                      {
                          SearchHelper test = new SearchHelper();
                          test.searchUser();
                      }
                  }
                  

                  推荐答案

                  您正在使用StringField 来索引您的数据,但该字段将绕过分析器链并始终索引无论您的分析仪如何,您的术语逐字作为一个标记.如果你想分析你的数据并且 StandardAnalyzer 已经做了小写,你应该使用 TextField.除此之外,WildcardQuery 确实 分析其术语,因此如果您搜索 Banglore,它将与索引中现在小写的 Banglore 不匹配.您必须自己将搜索词小写(或对其使用分析器).

                  You are usingStringField to index your data but this field will bypass the analyzer chain and always index your term verbatim as one token, regardless of your analyzer. You should use TextField if you want to have your data analyzed and the StandardAnalyzer already does lower-casing. Other than that, the WildcardQuery does not analyze its term, so if you search for Banglore, it won't match the now-lower-case banglore from the index. You have to lowercase the searchterm yourself (or use an analyzer on it).

                  这篇关于Java Lucene 4.5如何按不区分大小写进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 中的文档术语向量中获取位置?)
                  solrj: how to store and retrieve Listlt;POJOgt; via multivalued field in index(solrj:如何存储和检索Listlt;POJOgt;通过索引中的多值字段)

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

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

                      <tbody id='W2uBc'></tbody>
                          <bdo id='W2uBc'></bdo><ul id='W2uBc'></ul>
                          <tfoot id='W2uBc'></tfoot>

                          <legend id='W2uBc'><style id='W2uBc'><dir id='W2uBc'><q id='W2uBc'></q></dir></style></legend>