• <tfoot id='smh3v'></tfoot>

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

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

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

        如何在 Lucene 搜索中匹配精确文本?

        时间:2023-09-29
        • <tfoot id='lVvEm'></tfoot>
            <bdo id='lVvEm'></bdo><ul id='lVvEm'></ul>
            <i id='lVvEm'><tr id='lVvEm'><dt id='lVvEm'><q id='lVvEm'><span id='lVvEm'><b id='lVvEm'><form id='lVvEm'><ins id='lVvEm'></ins><ul id='lVvEm'></ul><sub id='lVvEm'></sub></form><legend id='lVvEm'></legend><bdo id='lVvEm'><pre id='lVvEm'><center id='lVvEm'></center></pre></bdo></b><th id='lVvEm'></th></span></q></dt></tr></i><div id='lVvEm'><tfoot id='lVvEm'></tfoot><dl id='lVvEm'><fieldset id='lVvEm'></fieldset></dl></div>
              <tbody id='lVvEm'></tbody>

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

                <legend id='lVvEm'><style id='lVvEm'><dir id='lVvEm'><q id='lVvEm'></q></dir></style></legend>
                • 本文介绍了如何在 Lucene 搜索中匹配精确文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试匹配 TITLE 列中的文本Config migration from ASA5505 8.2 to ASA5516.

                  Im trying to match a text Config migration from ASA5505 8.2 to ASA5516 in column TITLE.

                  我的程序是这样的.

                  Directory directory = FSDirectory.open(indexDir);
                  
                  MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_35,new String[] {"TITLE"}, new StandardAnalyzer(Version.LUCENE_35));        
                  IndexReader reader = IndexReader.open(directory);
                  IndexSearcher searcher = new IndexSearcher(reader);       
                  queryParser.setPhraseSlop(0);
                  queryParser.setLowercaseExpandedTerms(true);
                  Query query = queryParser.parse("TITLE:Config migration from ASA5505 8.2 to ASA5516");
                  System.out.println(queryStr);
                  TopDocs topDocs = searcher.search(query,100);
                  System.out.println(topDocs.totalHits);
                  ScoreDoc[] hits = topDocs.scoreDocs;
                  System.out.println(hits.length + " Record(s) Found");
                  for (int i = 0; i < hits.length; i++) {
                      int docId = hits[i].doc;
                      Document d = searcher.doc(docId);
                      System.out.println(""Title :" " +d.get("TITLE") );
                  }
                  

                  但它的回归

                  "Title :" Config migration from ASA5505 8.2 to ASA5516
                  "Title :" Firewall  migration from ASA5585 to  ASA5555
                  "Title :" Firewall  migration from ASA5585 to  ASA5555
                  

                  第二个 2 结果不是预期的.所以需要什么修改才能匹配确切的文本配置从 ASA5505 8.2 迁移到 ASA5516

                  Second 2 results are not expected.So what modification required to match exact text Config migration from ASA5505 8.2 to ASA5516

                  我的索引函数看起来像这样

                  And my indexing function looks like this

                  public class Lucene {
                  public static final String INDEX_DIR = "./Lucene";
                  private static final String JDBC_DRIVER = "oracle.jdbc.OracleDriver";
                  private static final String CONNECTION_URL = "jdbc:oracle:thin:xxxxxxx"
                  
                  private static final String USER_NAME = "localhost";
                  private static final String PASSWORD = "localhost";
                  private static final String QUERY = "select * from TITLE_TABLE";
                  
                  public static void main(String[] args) throws Exception {
                      File indexDir = new File(INDEX_DIR);
                      Lucene indexer = new Lucene();
                      try {
                          Date start = new Date();
                          Class.forName(JDBC_DRIVER).newInstance();
                          Connection conn = DriverManager.getConnection(CONNECTION_URL, USER_NAME, PASSWORD);
                          SimpleAnalyzer analyzer = new SimpleAnalyzer(Version.LUCENE_35);
                          IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
                          IndexWriter indexWriter = new IndexWriter(FSDirectory.open(indexDir), indexWriterConfig);
                          System.out.println("Indexing to directory '" + indexDir + "'...");
                          int indexedDocumentCount = indexer.indexDocs(indexWriter, conn);
                          indexWriter.close();
                          System.out.println(indexedDocumentCount + " records have been indexed successfully");
                          System.out.println("Total Time:" + (new Date().getTime() - start.getTime()) / (1000));
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
                  
                  int indexDocs(IndexWriter writer, Connection conn) throws Exception {
                      String sql = QUERY;
                      Statement stmt = conn.createStatement();
                      stmt.setFetchSize(100000);
                      ResultSet rs = stmt.executeQuery(sql);
                      int i = 0;
                      while (rs.next()) {
                          System.out.println("Addind Doc No:" + i);
                          Document d = new Document();
                          System.out.println(rs.getString("TITLE"));
                          d.add(new Field("TITLE", rs.getString("TITLE"), Field.Store.YES, Field.Index.ANALYZED));
                          d.add(new Field("NAME", rs.getString("NAME"), Field.Store.YES, Field.Index.ANALYZED));
                          writer.addDocument(d);
                          i++;
                      }
                      return i;
                  }
                  }
                  

                  推荐答案

                  PVR 是正确的,在这里使用短语查询可能是正确的解决方案,但是他们错过了如何使用 PhraseQuery 类.不过,您已经在使用 QueryParser,因此只需将搜索文本括在引号中即可使用查询解析器语法:

                  PVR is correct, that using a phrase query is probably the right solution here, but they missed on how to use the PhraseQuery class. You are already using QueryParser though, so just use the query parser syntax by enclosing you search text in quotes:

                  Query query = queryParser.parse("TITLE:"Config migration from ASA5505 8.2 to ASA5516"");
                  

                  <小时>

                  根据您的更新,您在索引时和查询时使用了不同的分析器.SimpleAnalyzerStandardAnalyzer 不做同样的事情.除非您有很好的理由不这样做,否则您应该在索引和查询时以相同的方式进行分析.


                  Based on your update, you are using a different analyzer at index-time and query-time. SimpleAnalyzer and StandardAnalyzer don't do the same things. Unless you have a very good reason to do otherwise, you should analyze the same way when indexing and querying.

                  因此,将索引代码中的分析器更改为 StandardAnalyzer(反之亦然,在查询时使用 SimpleAnalyzer),您应该会看到更好的结果.

                  So, change the analyzer in your indexing code to StandardAnalyzer (or vice-versa, use SimpleAnalyzer when querying), and you should see better results.

                  这篇关于如何在 Lucene 搜索中匹配精确文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:打开 lucene 索引时出错:映射失败 下一篇:如何在lucene中将RAMDirectory集成到FSDirectory

                  相关文章

                  最新文章

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

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

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