5. Search Using Lucene 5.1 simple search code // IOException and ParseException must be caught. // Process search conditions Query query = QueryParser. parse ("", "text", analyzer ); // Search Searcher searcher = new IndexSearcher ("./index"); // "index" specifies the location of the index file Hits hits = searcher. search (query ); // Print the result value set For (int I = 0; I Doc = hits.doc (I ); String id = doc. get ("id "); System. out. println ("found" + "" + "on the id:" + id ); } 5.2 use Lucene's Retrieval Interface 5.2.1 Query and QueryParser Main usage: QueryParser. parse (String query, String field, Analyzer analyzer), for example: Query query = QueryParser. parse ("", "text", analyzer ); "Entry" is the query term, "text" is the name of the retrieved field, and analyzer is the analyzer. 5.2.2 Hits and Searcher Main APIs of Hits:
Interface Name |
Remarks |
Doc (int n) |
Returns all fields of the nth document. |
Length () |
Returns the number of available instances in this set. |
6. Other use of Lucene 6.1 Lucene index Modification The following code modifies the index: /** * Add a new index to an existing index. * @ Param idStr String: id to be modified * @ Param doc Document: value to be modified */ Public void addIndex (String idStr, String valueStr ){ StandardAnalyzer analyzer = new StandardAnalyzer (); IndexWriter writer = null; Try { Writer = new IndexWriter (indexPath, analyzer, false ); Writer. mergeFactor = 2; // fixed the lucene 1.4.2 bug. Otherwise, the modification cannot be accurately reflected. Document doc = new Document (); Doc. add (Field. UnIndexed ("id", idStr); // "id" indicates the Field name, and "1" indicates the Field value. Doc. add (Field. Text ("text", valueStr )); Writer. addDocument (doc ); Writer. optimize (); Writer. close (); } Catch (IOException ioe ){ Ioe. printStackTrace (); } } /** * Deleting an index * * @ Param idStr String */ Public void deleteIndex (String idStr ){ Try { Directory dirt = FSDirectory. getDirectory (indexPath, false ); IndexReader reader = IndexReader. open (dirt ); IndexXML. deleteIndex (idStr, reader ); Reader. close (); Dirt. close (); } Catch (IOException ioe ){ Ioe. printStackTrace (); } } 6.2 sorting of Lucene search results Lucene sorting mainly applies to org. apache. lucene. search. Sort. Sort can be directly generated based on the Field or the standard SortField. However, the Sort Field must meet the following conditions: unique value and Indexed. You can sort Integers, Floats, and Strings. To sort the search results of integer IDs, perform the following simple operations: Sort sort = new Sort ("id "); Hits hits = searcher. search (query, sort ); Users can also define more complex sorting based on their own. For details, refer to the API. 7. Conclusion Lucene brings great strength to full-text indexing of java. The above is only a brief introduction to Lucene. References: 1. Overview (Lucene 1.4-final API) 2. Add full-text search function to applications-JAVA-based full-text index engine Lucene Introduction
|