How to match exact text in Lucene search?

后端 未结 3 1550
执笔经年
执笔经年 2021-01-06 16:33

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

My program looks like this.



        
相关标签:
3条回答
  • 2021-01-06 16:52

    Here is what i have written for you which works perfectly:

    USE: queryParser.parse("\"Config migration from ASA5505 8.2 to ASA5516\"");

    1. To create indexes

      public static void main(String[] args) 
      {
      
          IndexWriter writer = getIndexWriter();
          Document doc = new Document();
          Document doc1 = new Document();
          Document doc2 = new Document();
          doc.add(new Field("TITLE", "Config migration from ASA5505 8.2 to ASA5516",Field.Store.YES,Field.Index.ANALYZED));
          doc1.add(new Field("TITLE", "Firewall  migration from ASA5585 to ASA5555",Field.Store.YES,Field.Index.ANALYZED));
          doc2.add(new Field("TITLE", "Firewall  migration from ASA5585 to ASA5555",Field.Store.YES,Field.Index.ANALYZED));
          try 
          {
              writer.addDocument(doc);
              writer.addDocument(doc1);
              writer.addDocument(doc2);
              writer.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      
      public static IndexWriter getIndexWriter()
      {
          IndexWriter indexWriter=null;
      
          try 
          {
          File file=new File("D://index//");
          if(!file.exists())
              file.mkdir();
          IndexWriterConfig conf=new IndexWriterConfig(Version.LUCENE_34, new StandardAnalyzer(Version.LUCENE_34));
          Directory directory=FSDirectory.open(file);
          indexWriter=new IndexWriter(directory, conf);
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          return indexWriter;
      }
      

      }

    2.To search string

        public static void main(String[] args) 
        {
    
        IndexReader reader=getIndexReader();
    
        IndexSearcher searcher = new IndexSearcher(reader);
    
        QueryParser parser = new QueryParser(Version.LUCENE_34, "TITLE" ,new StandardAnalyzer(Version.LUCENE_34));
    
        Query query;
        try 
        {
        query = parser.parse("\"Config migration from ASA5505 8.2 to ASA5516\"");
    
        TopDocs hits = searcher.search(query,3);
    
        ScoreDoc[] document = hits.scoreDocs;
        int i=0;
        for(i=0;i<document.length;i++)
        {
            Document doc = searcher.doc(i);
    
            System.out.println("TITLE=" + doc.get("TITLE"));
        }
            searcher.close();
    
        } 
        catch (Exception e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
                }
    
    public static IndexReader getIndexReader()
    {
        IndexReader reader=null;
    
        Directory dir;
        try 
        {
            dir = FSDirectory.open(new File("D://index//"));
            reader=IndexReader.open(dir);
        } catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return reader;
    }   
    
    0 讨论(0)
  • 2021-01-06 16:54

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

    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.

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

    0 讨论(0)
  • 2021-01-06 17:15

    Try PhraseQuery as follow:

    BooleanQuery mainQuery= new BooleanQuery(); 
    String searchTerm="config migration from asa5505 8.2 to asa5516";
    String strArray[]= searchTerm.split(" ");
    for(int index=0;index<strArray.length;index++)
    {
        PhraseQuery query1 = new PhraseQuery();
         query1.add(new Term("TITLE",strArray[index]));
         mainQuery.add(query1,BooleanClause.Occur.MUST);
    }
    

    And then execute the mainQuery.

    Check out this thread of stackoverflow, It may help you to use PhraseQuery for exact search.

    0 讨论(0)
提交回复
热议问题