I. Preface
Do a simple Lucene CRUD test, record
2, Code + effect
1. Index batch generation
Generate several indexes in batch for later testing
Code 1.
package com.cun.test;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class Indexer {
// Document field
private Integer ids[] = { 1, 2, 3, 4 };
private String names[] = { "Java", "Python", "JavaScript", "PHP" };
private String descs[] = { "Java Is an object-oriented programming language, not only absorbed C++All the advantages of language have been abandoned C++It is difficult to understand the concept of multiple inheritance, pointer, etc., so Java Language has two characteristics: powerful function and easy to use.", "Python Is the preferred language of AI", "JavaScript Is a literal script language, is a dynamic type, weak type, prototype based language, built-in support type.", "PHP Is the best language in the world." };
// Directory to hold generated documents
private Directory dir;
// Get index instance
private IndexWriter getWriter() throws Exception {
// Analyzer analyzer=new StandardAnalyzer(); / / standard word breaker (English word breaker)
SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();// Chinese word breaker
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
IndexWriter writer = new IndexWriter(dir, iwc);
return writer;
}
// Add index
private void index(String indexDir) throws Exception {
// Instance directory
dir = FSDirectory.open(Paths.get(indexDir));
// Get index
IndexWriter writer = getWriter();
for (int i = 0; i < ids.length; i++) {
// create documents
Document document = new Document();
// Document join document domain
document.add(new IntField("id", ids[i], Field.Store.YES));// YES means it will be saved to the document, NO means it will not
document.add(new StringField("name", names[i], Field.Store.YES));// StringField will not be worded
document.add(new TextField("desc", descs[i], Field.Store.YES));// TextField will be segmented
// Index join document
writer.addDocument(document); // Add document
}
// Close index
writer.close();
}
public static void main(String[] args) throws Exception {
// As long as the specified C and D disks exist in the computer, enter the directory below. If they do not exist, they will be created automatically
new Indexer().index("C:\\LLLLLLLLLLLLLLLLLLL\\886");
System.out.println("Index created successfully");
}
}
② Console output
2. Look for
Code 1.
@Test
public void TestSearch() throws Exception {
// Document path
String path = "C:\\LLLLLLLLLLLLLLLLLLL\\886";
Directory directory = FSDirectory.open(Paths.get(path));
DirectoryReader reader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(reader);
// Document fields to query
String fld = "desc";
// Keywords to query
String text = "language";
Term term = new Term(fld, text);
Query query = new TermQuery(term);
int n = 10;
TopDocs topDocs = indexSearcher.search(query, n);
System.out.println("matching '" + query + "',Total queries" + topDocs.totalHits + "Document");
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
String arg0 = "name";
System.out.println(document.get(arg0));
}
reader.close();
}
② Console output
3, increase
Code 1.
@Test
public void TestAdd() throws Exception {
// Document path
String path = "C:\\LLLLLLLLLLLLLLLLLLL\\886";
Directory directory = FSDirectory.open(Paths.get(path));
SmartChineseAnalyzer smartChineseAnalyzer = new SmartChineseAnalyzer();// Chinese word breaker
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(smartChineseAnalyzer);
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
// Documents to add
Document document = new Document();
// Document fields for documents
document.add(new IntField("id", 5, Field.Store.YES));// YES means it will be saved to the document, NO means it will not
document.add(new StringField("name", "GO", Field.Store.YES));// StringField will not be worded
document.add(new TextField("desc", "Go Is a new language, a concurrent, with garbage collection, rapid compilation of the language.", Field.Store.YES));// TextField will be segmented
// Add to
indexWriter.addDocument(document);
System.out.println("Add success");
// Close
indexWriter.close();
}
② Console output
③ Run the query in 2 again: it is found that GO has been added, and attention should be paid to the order. It is not added at the back, nor random, but arranged according to the relevance of keywords
4, modify
Code 1.
@Test
public void TestUpdate() throws Exception { // Update is essentially: delete + add
// Document path
String path = "C:\\LLLLLLLLLLLLLLLLLLL\\886";
Directory directory = FSDirectory.open(Paths.get(path));
SmartChineseAnalyzer smartChineseAnalyzer = new SmartChineseAnalyzer();// Chinese word breaker
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(smartChineseAnalyzer);
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
// Modified document
Document document = new Document();
// Modified document fields
document.add(new IntField("id", 6, Field.Store.YES));// YES means it will be saved to the document, NO means it will not
document.add(new StringField("name", "C language", Field.Store.YES));// StringField will not be worded
document.add(new TextField("desc", "C Language is a general computer programming language, widely used.", Field.Store.YES));// TextField will be segmented
//New document replace old document
indexWriter.updateDocument(new Term("name","GO"),document);
System.out.println("GO Change language to C Language over");
//Close
indexWriter.close();
}
② Console output
③ Run the query in 2 again: find that GO language is gone, and change to C language
5, delete
Code 1.
@Test
public void TestDelete() throws Exception {
// Document path
String path = "C:\\LLLLLLLLLLLLLLLLLLL\\886";
Directory directory = FSDirectory.open(Paths.get(path));
SmartChineseAnalyzer smartChineseAnalyzer = new SmartChineseAnalyzer();// Chinese word breaker
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(smartChineseAnalyzer);
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
System.out.println("Total documents before deletion:" + indexWriter.maxDoc());
System.out.println("Number of valid documents before deletion:" + indexWriter.numDocs());
indexWriter.deleteDocuments(new Term("name", "C language"));
indexWriter.commit();
System.out.println("After deletion, total documents:" + indexWriter.maxDoc());
System.out.println("Number of valid documents after deletion:" + indexWriter.numDocs());
indexWriter.close();
}
② Console output
③ Run the query in 2 again, and return to the data before adding, deleting, and modifying the query