Learning search engine (c) Lucene query code development index

First, the theoretical inquiry

Create a query: building contains a document domain and vocabulary unit of the document query object . (Example: fileName: Lucene )

Query process: according to the query object condition in the index to find the appropriate term , then find the corresponding term in accordance with the document id list .

Second, code analysis

Implementation code

/ ** 
 * Lucene entry 
 * query index 
 * / 
public  class QueryIndex { 

    / ** 
     * Query Index 
     * Step 1: Create a Directory object location specified index Library 
     * Step 2: Create a IndexReader (index reading) target , you need to specify Directory objects 
     * step 3: create a IndexSearcher (index search) object, you need to specify the object IndexReader 
     * step Four: create a TermQuery (domain query) object specifying the query domain and query keywords 
     * step Five: execute the query 
     * step Six: return query results, traverse the query results and output 
     * step Seven: Close IndexReader objects (off flow) 
     * / 
    @Test 
    public  void queryIndex () throws Exception { 

        / * step 1: create a Directory Object , specify the location of the index library * / 
        Directory Directory = FSDirectory.open ( new newFile ( "E: \\ zhanghaoBF luceneSolr \\ \\ indexLibrary") toPath ());. // path on disk (HDD) 

        / * Step 2: Create a IndexReader (index reading) the object * / 
        IndexReader IndexReader DirectoryReader.open = (Directory); // open the index database (this object is a stream object) 

        / * step 3: create a IndexSearcher (index search) Object * / 
        IndexSearcher IndexSearcher = new new IndexSearcher (IndexReader); 

        / * step Four : create a TermQuery (domain query) object specifying the query field and query keyword * / 
        query query = new new TermQuery ( new new Term ( "fileContent", "Lucene")); // PS: TermQuery is accurate query

         / * first five steps: execute the query * /
        TopDocs TopDocs = indexSearcher.search (query, 2); // The search query, returns the highest score 2 

        / * Step Six: returning query results, and outputs the result of the query to traverse * / 
        ScoreDoc [] scoreDocs = topDocs.scoreDocs; // the PS: Note that this document is returned id array
         for (scoreDoc scoreDoc: scoreDocs) {
             int the docId = scoreDoc.doc; // document ID 
            the document document indexSearcher.doc = (the docId); // document corresponding 

            String fileName = document.get ( "fileName"); // file name 
            System.out.println (fileName); 
            String fileSize = document.get ( "fileSize"); //File size 
            System.out.println (fileSize); 
            String filePath = document.get ( "filePath"); // file path 
            System.out.println (filePath); 
            System.out.println ( "------- ---------- --- gorgeous dividing line " ); 
        } 

        / * step Seven: Close Object IndexReader (off stream) * / 
        indexReader.close (); 
    } 
}

 

 

Popular search method

 

Guess you like

Origin www.cnblogs.com/riches/p/11445604.html