依賴
<!-- Lucene核心 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>4.7.2</version>
</dependency>
<!-- Lucene搜索查詢相關 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>4.7.2</version>
</dependency>
<!-- Lucene分詞器相關 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>4.7.2</version>
</dependency>
<!--高亮-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>4.7.2</version>
</dependency>
建立索引
本次增加了Float雄妥、Int類型的域
package top.yuyufeng.learn.lucene.demo2;
/**
* @author yuyufeng
* @date 2017/11/21
*/
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import java.io.File;
import java.io.IOException;
/**
* @author yuyufeng
* @date 2017/11/21
*/
public class LuceneIndexDemo {
public static void main(String[] args) {
// 建立5條索引
String content = "10月11日杭州云棲大會上最蕾,馬云表達了對新建成的阿里巴巴全球研究院—阿里巴巴達摩院的愿景,希望達摩院二十年內成為世界第一大經濟體老厌,服務世界二十億人瘟则,創(chuàng)造一億個工作崗位。";
Long createTime = System.currentTimeMillis();
String id = createTime + "";
int readCount =10;
float score =9.5f;
index(content, createTime, id, readCount, score);
content = "中國互聯(lián)網界梅桩,阿里巴巴被認為是技術實力最弱的公司壹粟。我確實不懂技術拜隧,承認不懂技術不丟人宿百,不懂裝懂才丟人。";
createTime = System.currentTimeMillis();
id = createTime + "";
readCount =3;
score =9.7f;
index(content, createTime, id, readCount, score);
content = "阿里巴巴未來二十年的目標是打造世界第五大經濟體洪添,不是我們狂妄垦页,而是世界需要這么一個經濟體,也一定會有這么一個經濟體干奢。";
createTime = System.currentTimeMillis();
id = createTime + "";
readCount =69;
score =5.6f;
index(content, createTime, id, readCount, score);
content = "達摩院一定也必須要超越英特爾痊焊,必須超越微軟,必須超越IBM忿峻,因為我們生于二十一世紀薄啥,我們是有機會后發(fā)優(yōu)勢的。";
createTime = System.currentTimeMillis();
id = createTime + "";
readCount =38;
score =4.7f;
index(content, createTime, id, readCount, score);
content = "阿里巴巴有很多爭議逛尚,似乎無處不在垄惧,我還真想不出有什么東西是我們不做的〈履互聯(lián)網是一種思想到逊,是一種技術革命铣口,不應該有界限【鹾跨界樂趣無窮脑题。我覺得阿里巴巴的跨界還不錯";
createTime = System.currentTimeMillis();
id = createTime + "";
readCount =73;
score =1.7f;
index(content, createTime, id, readCount, score);
}
private static void index(String content, Long createTime, String id, int readCount, float score) {
// 實例化IKAnalyzer分詞器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
Directory directory = null;
IndexWriter iwriter;
try {
// 索引目錄
directory = new SimpleFSDirectory(new File("D://test/lucene_index_blog"));
// 配置IndexWriterConfig
IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_47, analyzer);
iwConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
iwriter = new IndexWriter(directory, iwConfig);
// 寫入索引
Document doc = new Document();
doc.add(new StringField("ID", id, Field.Store.YES));
doc.add(new TextField("content", content, Field.Store.YES));
doc.add(new LongField("createTime", createTime, Field.Store.YES));
doc.add(new IntField("readCount", readCount, Field.Store.YES));
doc.add(new FloatField("score", score, Field.Store.YES));
iwriter.addDocument(doc);
iwriter.close();
System.out.println("建立索引成功:" + id);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
搜索排序
package top.yuyufeng.learn.lucene.demo2;
/**
* @author yuyufeng
* @date 2017/11/21
*/
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import java.io.File;
import java.io.IOException;
/**
* created by yuyufeng on 2017/11/13.
*/
public class LuceneSearchDemo {
public static void main(String[] args) {
String content = "content";
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
Directory directory = null;
IndexReader ireader = null;
IndexSearcher isearcher;
try {
//索引目錄
directory = new SimpleFSDirectory(new File("D://test/lucene_index_blog"));
// 配置IndexWriterConfig
IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_47, analyzer);
iwConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
// 實例化搜索器
ireader = DirectoryReader.open(directory);
isearcher = new IndexSearcher(ireader);
//查詢所有
Query query = new MatchAllDocsQuery();
System.out.println("Query = " + query);
// 排序的關鍵地方
SortField sortField = new SortField("score",SortField.Type.FLOAT,true);
Sort sort = new Sort(sortField);
TopDocs topDocs = isearcher.search(query, 5,sort);
System.out.println("命中:" + topDocs.totalHits);
// 遍歷輸出結果
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (int i = 0; i < topDocs.totalHits; i++) {
Document targetDoc = isearcher.doc(scoreDocs[i].doc);
System.out.println("內容:" + targetDoc.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ireader != null) {
try {
ireader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
范圍搜索
查詢score范圍在1~5之間的文檔,對于上面的代碼中的Query進行改造
Query query = NumericRangeQuery.newFloatRange("score",1f,5f,true,true);
對搜索結果的分頁
package top.yuyufeng.learn.lucene.demo2;
/**
* @author yuyufeng
* @date 2017/11/21
*/
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.Version;
import java.io.File;
import java.io.IOException;
/**
* created by yuyufeng on 2017/11/13.
*/
public class LuceneSearchDemo {
public static void main(String[] args) {
page(2, 3);
}
private static void page(int page, int size) {
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
Directory directory = null;
IndexReader ireader = null;
IndexSearcher isearcher;
try {
//索引目錄
directory = new SimpleFSDirectory(new File("D://test/lucene_index_blog"));
// 配置IndexWriterConfig
IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_47, analyzer);
iwConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
// 實例化搜索器
ireader = DirectoryReader.open(directory);
isearcher = new IndexSearcher(ireader);
//查詢所有
Query query = new MatchAllDocsQuery();
TopDocs topDocs = isearcher.search(query, 100);
int total = topDocs.totalHits;
System.out.println("命中:" + topDocs.totalHits);
// 遍歷輸出結果
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (int i = (page - 1) * size; i < ((page - 1) * size + size > total ? total : (page - 1) * size + size); i++) {
Document targetDoc = isearcher.doc(scoreDocs[i].doc);
System.out.println("內容:" + targetDoc.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ireader != null) {
try {
ireader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
多條件查詢
package top.yuyufeng.learn.lucene.demo2;
/**
* @author yuyufeng
* @date 2017/11/21
*/
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import java.io.File;
import java.io.IOException;
/**
* created by yuyufeng on 2017/11/13.
*/
public class LuceneSearchDemo {
public static void main(String[] args) {
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
Directory directory = null;
IndexReader ireader = null;
IndexSearcher isearcher;
try {
//索引目錄
directory = new SimpleFSDirectory(new File("D://test/lucene_index_blog"));
// 配置IndexWriterConfig
IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_47, analyzer);
iwConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
// 實例化搜索器
ireader = DirectoryReader.open(directory);
isearcher = new IndexSearcher(ireader);
BooleanQuery booleanQuery = new BooleanQuery();
String keyword = "達摩院";
// 條件一
QueryParser qp = new QueryParser(Version.LUCENE_47, "content", analyzer);
Query query = qp.parse(keyword);
booleanQuery.add(query,BooleanClause.Occur.MUST);
//條件二
query = NumericRangeQuery.newFloatRange("score",1f,5f,true,true);
booleanQuery.add(query,BooleanClause.Occur.MUST);
TopDocs topDocs = isearcher.search(booleanQuery,100);
System.out.println("命中:" + topDocs.totalHits);
// 遍歷輸出結果
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (int i = 0; i < topDocs.totalHits; i++) {
Document targetDoc = isearcher.doc(scoreDocs[i].doc);
System.out.println("內容:" + targetDoc.toString());
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} finally {
if (ireader != null) {
try {
ireader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (directory != null) {
try {
directory.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
BooleanClause.Occur
| 組合方式| 結果 |
| ------------- |-------------|
|MUST和MUST | 取得連個查詢子句的交集铜靶。|
|MUST和MUST_NOT| 表示查詢結果中不能包含MUST_NOT所對應得查詢子句的檢索結果叔遂。|
|SHOULD與MUST_NOT|連用時,功能同MUST和MUST_NOT旷坦。|
|SHOULD與MUST|結果為MUST子句的檢索結果,但是SHOULD可影響排序|
|SHOULD與SHOULD|表示“或”關系掏熬,最終檢索結果為所有檢索子句的并集|
|MUST_NOT和MUST_NOT|無意義,檢索無結果|