1. 程式人生 > >lucene 搜尋功能介紹(2)

lucene 搜尋功能介紹(2)

準備工作:資料,生成索引

        private Integer[] ids = {1,2,3};
private String[] citys = {"beijing","shanghai","guangzhou"};
private String[] descs = {
"beijing is a beautiful city",
"shanghai is a city of culture",
"guangzhou is a bulsting city"
};

private Directory dir;


/**
* 獲取到IndexWriter的例項
* @return
*/
private IndexWriter getWriter() throws Exception{
// TODO Auto-generated method stub
Analyzer analyzer = new StandardAnalyzer();  //標準分詞器
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
IndexWriter writer = new IndexWriter(dir, conf);
return writer;
}

/**
* 生成索引
* @throws Exception
*/
public void index(String indexDir) throws Exception {
//獲取到索引輸出的目錄
dir = FSDirectory.open(Paths.get(indexDir));
IndexWriter writer = getWriter();
//新建document物件  將資料加入
for(int i = 0; i < ids.length; i++){
Document document = new Document();
document.add(new IntField("id", ids[i], Field.Store.YES));
document.add(new StringField("city", citys[i], Field.Store.YES));
document.add(new TextField("desc",descs[i], Field.Store.NO));
writer.addDocument(document);
}
writer.close();
}

public static void main(String[] args) throws Exception {
new Indexer().index("D:\\lucene2");

}

查詢方法介紹:指定範圍查詢 TermRangeQuery

            指定數字範圍查詢  NumericaRangeQuery

            指定字串開頭查詢  PrefixQuery

            組合多條件查詢  BooleanQuery

準備工作:

     private Directory dir;
private IndexReader reader;
private IndexSearcher is;

@Before
public void setUp() throws Exception {
dir = FSDirectory.open(Paths.get("D:\\lucene2"));  //索引所在的目錄
reader = DirectoryReader.open(dir);  //讀取索引
is = new IndexSearcher(reader);
}

@After
public void tearDown() throws Exception {
reader.close();

}


        /**
* 指定項範圍查詢  TermRangeQuery
* @throws Exception
*/
@Test
public void testTermRangeQuery() throws Exception{
//TermRangeQuery query = new TermRangeQuery(field, lowerTerm, upperTerm, includeLower, includeUpper)
//field  為查詢的欄位   lowerTerm 為查詢的範圍最低為    upperTerm 為查詢的範圍最大為   includeLower  是否包含小寫  includeUpper 是否包含大寫
TermRangeQuery query = new TermRangeQuery("desc",new BytesRef("a".getBytes()),new BytesRef("c".getBytes()), true, true);

TopDocs hits = is.search(query, 10);
for(ScoreDoc scoreDoc : hits.scoreDocs){
Document document = is.doc(scoreDoc.doc);
System.out.println(document.get("id"));
System.out.println(document.get("city"));
System.out.println(document.get("desc"));
}
}

/**
* 指定數字範圍的查詢   泛型不匹配時查不到
* @throws Exception
*/
@Test
public void testNumericRangeQuery() throws Exception{
NumericRangeQuery<Integer> query = NumericRangeQuery.newIntRange("id", 1, 2, true, true);
TopDocs hits = is.search(query, 10);
for(ScoreDoc scoreDoc : hits.scoreDocs){
Document document = is.doc(scoreDoc.doc);
System.out.println(document.get("id"));
System.out.println(document.get("city"));
System.out.println(document.get("desc"));
}
}

/**
* 指定欄位的以某一個字母開頭的查詢
* @throws Exception
*/
@Test
public void testPrefixQuery() throws Exception{
PrefixQuery query = new PrefixQuery(new Term("city","s"));
TopDocs hits = is.search(query, 10);
for(ScoreDoc scoreDoc : hits.scoreDocs){
Document document = is.doc(scoreDoc.doc);
System.out.println(document.get("id"));
System.out.println(document.get("city"));
System.out.println(document.get("desc"));
}
}

/**
* 組合多條件查詢   MUST為並且   SHOULD為或   MUST_NOT為非
* @throws Exception
*/
@Test
public void testBooleanQuery() throws Exception{
NumericRangeQuery<Integer> query1 = NumericRangeQuery.newIntRange("id", 1, 2, true, true);
PrefixQuery query2 = new PrefixQuery(new Term("city","s"));
BooleanQuery.Builder query = new BooleanQuery.Builder();
query.add(query1, BooleanClause.Occur.MUST);  //兩個條件都滿足   可以一直加條件
query.add(query2, BooleanClause.Occur.MUST);

TopDocs hits = is.search(query.build(), 10);
for(ScoreDoc scoreDoc : hits.scoreDocs){
Document document = is.doc(scoreDoc.doc);
System.out.println(document.get("id"));
System.out.println(document.get("city"));
System.out.println(document.get("desc"));
}
}