package cn.edu360.lucene;
import org.apache.lucene.document.*;
import org.apache.lucene.document.Field.Store;
public class Article {
private Long id;
private String title;
private String content;
private String author;
private String url;
public Article(){}
public Article(Long id, String title, String content, String author,
String url) {
super();
this.id = id;
this.title = title;
this.content = content;
this.author = author;
this.url = url;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Document toDocument(){
Document doc = new Document();
doc.add(new LongPoint("id", id));
doc.add(new StoredField("id", id));
doc.add(new TextField("title", title, Store.YES));
doc.add(new TextField("content", content, Store.YES));
doc.add(new StringField("author", author, Store.YES));
doc.add(new StoredField("url", url));
return doc;
}
public static Article parseArticle(Document doc){
Long id = Long.parseLong(doc.get("id"));
String title = doc.get("title");
String content = doc.get("content");
String author = doc.get("author");
String url = doc.get("url");
Article article = new Article(id, title, content, author, url);
return article;
}
@Override
public String toString() {
return "id : " + id + " , title : " + title + " , content : " + content + " , author : " + author + " , url : " + url;
}
}
package cn.edu360.lucene;
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.LongPoint;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;
import java.io.IOException;
import java.nio.file.Paths;
/**
* Created by zx on 2017/9/12.
* <p>
* Lucene Index ToolBox : https://github.com/DmitryKey/luke/releases
* <p>
* https://segmentfault.com/a/1190000010367206
*/
public class HelloWorld {
/**
* 往用lucene写入数据
* @throws IOException
*/
@Test
public void testCreate() throws IOException {
Article article = new Article();
article.setId(108L);
article.setAuthor("老王");
article.setTitle("学习大数据");
article.setContent("学数据,迎娶丁老师!");
article.setUrl("http://www.edu360.cn/a10011");
String indexPath = "/Users/zx/Documents/dev/lucene/index";
FSDirectory fsDirectory = FSDirectory.open(Paths.get(indexPath));
Analyzer analyzer = new IKAnalyzer(true);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(fsDirectory, indexWriterConfig);
Document document = article.toDocument();
indexWriter.addDocument(document);
indexWriter.close();
}
@Test
public void testSearch() throws IOException, ParseException {
String indexPath = "/Users/zx/Documents/dev/lucene/index";
Analyzer analyzer = new IKAnalyzer(true);
DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
String queryStr = "数据";
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.parse(queryStr);
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int doc = scoreDoc.doc;
Document document = indexSearcher.doc(doc);
Article article = Article.parseArticle(document);
System.out.println(article);
}
directoryReader.close();
}
@Test
public void testDelete() throws IOException, ParseException {
String indexPath = "/Users/zx/Documents/dev/lucene/index";
Analyzer analyzer = new IKAnalyzer(true);
FSDirectory fsDirectory = FSDirectory.open(Paths.get(indexPath));
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(fsDirectory, indexWriterConfig);
Query query = LongPoint.newExactQuery("id", 105L);
indexWriter.deleteDocuments(query);
indexWriter.commit();
indexWriter.close();
}
/**
* lucene的update比较特殊,update的代价太高,先删除,然后在插入
* @throws IOException
* @throws ParseException
*/
@Test
public void testUpdate() throws IOException, ParseException {
String indexPath = "/Users/zx/Documents/dev/lucene/index";
StandardAnalyzer analyzer = new StandardAnalyzer();
FSDirectory fsDirectory = FSDirectory.open(Paths.get(indexPath));
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(fsDirectory, indexWriterConfig);
Article article = new Article();
article.setId(106L);
article.setAuthor("老王");
article.setTitle("学好大数据,要找赵老师");
article.setContent("迎娶白富美,走上人生巅峰!!!");
article.setUrl("http://www.edu360.cn/a111");
Document document = article.toDocument();
indexWriter.updateDocument(new Term("author", "老王"), document);
indexWriter.commit();
indexWriter.close();
}
/**
* 可以从多个字段中查找
* @throws IOException
* @throws ParseException
*/
@Test
public void testMultiField() throws IOException, ParseException {
String indexPath = "/Users/zx/Documents/dev/lucene/index";
Analyzer analyzer = new IKAnalyzer(true);
DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
String[] fields = {"title", "content"};
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
Query query = queryParser.parse("老师");
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int doc = scoreDoc.doc;
Document document = indexSearcher.doc(doc);
Article article = Article.parseArticle(document);
System.out.println(article);
}
directoryReader.close();
}
/**
* 查找全部的数据
* @throws IOException
* @throws ParseException
*/
@Test
public void testMatchAll() throws IOException, ParseException {
String indexPath = "/Users/zx/Documents/dev/lucene/index";
DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
Query query = new MatchAllDocsQuery();
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int doc = scoreDoc.doc;
Document document = indexSearcher.doc(doc);
Article article = Article.parseArticle(document);
System.out.println(article);
}
directoryReader.close();
}
/**
* 布尔查询,可以组合多个查询条件
* @throws Exception
*/
@Test
public void testBooleanQuery() throws Exception {
String indexPath = "/Users/zx/Documents/dev/lucene/index";
DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
Query query1 = new TermQuery(new Term("title", "老师"));
Query query2 = new TermQuery(new Term("content", "丁"));
BooleanClause bc1 = new BooleanClause(query1, BooleanClause.Occur.MUST);
BooleanClause bc2 = new BooleanClause(query2, BooleanClause.Occur.MUST_NOT);
BooleanQuery boolQuery = new BooleanQuery.Builder().add(bc1).add(bc2).build();
System.out.println(boolQuery);
TopDocs topDocs = indexSearcher.search(boolQuery, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int doc = scoreDoc.doc;
Document document = indexSearcher.doc(doc);
Article article = Article.parseArticle(document);
System.out.println(article);
}
directoryReader.close();
}
@Test
public void testQueryParser() throws Exception {
String indexPath = "/Users/zx/Documents/dev/lucene/index";
DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
QueryParser queryParser = new QueryParser("title", new IKAnalyzer(true));
Query query = queryParser.parse("title:学好 OR title:学习");
System.out.println(query);
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int doc = scoreDoc.doc;
Document document = indexSearcher.doc(doc);
Article article = Article.parseArticle(document);
System.out.println(article);
}
directoryReader.close();
}
@Test
public void testRangeQuery() throws Exception {
String indexPath = "/Users/zx/Documents/dev/lucene/index";
DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
Query query = LongPoint.newRangeQuery("id", 107L, 108L);
System.out.println(query);
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int doc = scoreDoc.doc;
Document document = indexSearcher.doc(doc);
Article article = Article.parseArticle(document);
System.out.println(article);
}
directoryReader.close();
}
}