实体类:
package com.nanjing.chaoxing.lucene.model; import java.io.File; import java.io.Reader; public class Book { private String bookid; private String bookname; private String author; private String subject; private Reader bookReader; private int year; public Book(){ } public Book(String bookid, String bookname, String author, String subject, int year,Reader bookReader) { this.bookid = bookid; this.bookname = bookname; this.author = author; this.subject = subject; this.year = year; this.bookReader = bookReader; } public String getBookid() { return bookid; } public void setBookid(String bookid) { this.bookid = bookid; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public Reader getBookReader() { return bookReader; } public void setBookReader(Reader bookReader) { this.bookReader = bookReader; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } @Override public String toString() { return "Book{" + "bookid='" + bookid + '\'' + ", bookname='" + bookname + '\'' + ", author='" + author + '\'' + ", subject='" + subject + '\'' + ", bookReader=" + bookReader + ", year='" + year + '\'' + '}'; } }
操作类:
package com.nanjing.chaoxing.lucene.model; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.NumericField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class BookUtil { public static File indexFile = new File("E:\\test\\indexFile"); private Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); public static List<Book> bookList = new ArrayList<Book>(); private IndexWriter indexWriter; public void createDocument() throws IOException { Book book1 = new Book("6270000080", "core java 1", "jams", "java book", 2001, new FileReader("E:\\test\\dataSourceFile\\1.txt")); Book book2 = new Book("6270000081", "core java 2", "jams", "java book", 2010, new FileReader("E:\\test\\dataSourceFile\\2.txt")); Book book3 = new Book("6270000082", "Thinking in java1", "tom", "java book", 2012, new FileReader("E:\\test\\dataSourceFile\\3.txt")); Book book4 = new Book("6270000083", "Thinking in java2", "tom", "java book", 2005, new FileReader("E:\\test\\dataSourceFile\\4.txt")); Book book5 = new Book("6270000084", "Thinking in java3", "tom", "java book", 2011, new FileReader("E:\\test\\dataSourceFile\\5.txt")); Book book6 = new Book("6270000085", "Thinking in java4", "tom", "java book", 1994, new FileReader("E:\\test\\dataSourceFile\\6.txt")); bookList.add(book1); bookList.add(book2); bookList.add(book3); bookList.add(book4); bookList.add(book5); bookList.add(book6); for (Book book : bookList) { Document doc = new Document(); doc.add(new Field("bookid", book.getBookid(), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field("bookname", book.getBookname(), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field("author", book.getAuthor(), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field("subject", book.getSubject(), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new NumericField("year",Field.Store.YES, true).setIntValue(book.getYear())); doc.add(new Field("content", book.getBookReader())); indexWriter.addDocument(doc); } indexWriter.close(); } public void createIndexWriter() throws IOException { Directory directory = FSDirectory.open(indexFile); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer); indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE); indexWriter = new IndexWriter(directory, indexWriterConfig); } }
测试类:
package com.nanjing.chaoxing.lucene; import com.nanjing.chaoxing.lucene.model.Book; import com.nanjing.chaoxing.lucene.model.BookUtil; import org.apache.log4j.Logger; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.*; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; public class LuceneQueryTest { private Logger logger = Logger.getLogger(LuceneQueryTest.class); @BeforeClass public static void init() throws IOException { BookUtil bookUtil = new BookUtil(); bookUtil.createIndexWriter(); bookUtil.createDocument(); } // @Test public void test() throws IOException, ParseException { termQuery("6270000081"); termRangeQuery(); numbericRangeQuery(); prefixQuery(); booleanQuery(); wildcardQuery(); queryParse(); } /** * 关键字查询--完全匹配 * * @param bookid * @throws IOException */ public void termQuery(String bookid) throws IOException { TermQuery termQuery = new TermQuery(new Term("bookname", "Thinking in java1")); outResult(termQuery, "term query"); } /** * 字符串范围查询 * @throws IOException */ public void termRangeQuery() throws IOException { TermRangeQuery termRangeQuery = new TermRangeQuery("bookname", "c", "j", true, true); outResult(termRangeQuery, "prefic query"); } /** * 数字范围查询 * @throws IOException */ public void numbericRangeQuery() throws IOException { NumericRangeQuery numericRangeQuery = NumericRangeQuery.newIntRange("year", 2008, 2012, true, true); IndexReader indexReader = IndexReader.open(FSDirectory.open(BookUtil.indexFile)); outResult(numericRangeQuery, "number query"); } /** * 字符串开头的搜索 */ public void prefixQuery() throws IOException { PrefixQuery prefixQuery = new PrefixQuery(new Term("bookname", "Thinking in")); outResult(prefixQuery, "prefic query"); } /** * 组合查询,其中BooleanClause.Occur.MUST是与,BooleanClause.Occur.SHOULD是或,BooleanClause.Occur.MUST_NOT是非 */ public void booleanQuery() throws IOException { TermQuery termQuery = new TermQuery(new Term("author", "tom")); NumericRangeQuery numericRangeQuery = NumericRangeQuery.newIntRange("year", 2011, 2012, true, false); BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(termQuery, BooleanClause.Occur.MUST); booleanQuery.add(numericRangeQuery, BooleanClause.Occur.MUST); outResult(booleanQuery, "boolean query"); } /** * 通配符查询:*--0或者多个字符,?--0或1个字符, */ public void wildcardQuery() throws IOException { WildcardQuery wildcardQuery = new WildcardQuery(new Term("bookname", "?or*")); outResult(wildcardQuery, "wildcard query"); } /** * 通配符查询:*--0或者多个字符,?--0或1个字符, */ // @Test public void fuzzyQuery() throws IOException { FuzzyQuery fuzzyQuery = new FuzzyQuery(new Term("author", "jam")); outResult(fuzzyQuery, "fuzzy query"); } /** * 用queryParse进行范围选择 a AND b=+a+b; a OR b=a b; a AND NOT b=+a-b; * 模糊匹配可以用~表示 */ @Test public void queryParse() throws ParseException, IOException { QueryParser queryParser=new QueryParser(Version.LUCENE_36,"bookid",new StandardAnalyzer(Version.LUCENE_36)); // Query query=queryParser.parse("bookid:(6270000081 6270000082) AND author:tom"); // Query query=queryParser.parse("bookid:(6270000081 6270000082) -author:tom"); // Query query=queryParser.parse("bookid:(6270000081 OR 6270000082) author:tom"); Query query=queryParser.parse("author:jam~"); outResult(query,"query parse range"); } /** * 输出匹配的结果 * * @param query * @param queryInfo * @throws IOException */ public void outResult(Query query, String queryInfo) throws IOException { IndexReader indexReader = IndexReader.open(FSDirectory.open(BookUtil.indexFile)); IndexSearcher indexSearcher=new IndexSearcher(indexReader); logger.info(queryInfo + " begin...."); TopDocs topDocs = indexSearcher.search(query, 1000); ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (int i = 0; i < scoreDocs.length; i++) { ScoreDoc scoreDoc = scoreDocs[i]; int doc = scoreDoc.doc; for (Book book : BookUtil.bookList) { if (indexSearcher.doc(doc).get("bookid").equals(book.getBookid())) { logger.info(book); } } } logger.info(queryInfo + " end....\n"); } }