luceneクエリー

12288 ワード

エンティティークラス:
 
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....
"); } }