LuceneSearchSpanQuery.java
上传用户:cctqzzy
上传日期:2022-03-14
资源大小:12198k
文件大小:8k
- package chapter6;
- import java.io.IOException;
- import org.apache.lucene.index.Term;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.SimpleAnalyzer;
- import org.apache.lucene.search.Query;
- import org.apache.lucene.search.Hits;
- import org.apache.lucene.search.spans.*;
- import org.apache.lucene.search.IndexSearcher;
- import org.apache.lucene.queryParser.*;
- public class LuceneSearchSpanQuery {
-
- private static String Dest_Index_Path = "D:\workshop\TextIndex";
-
- static protected String[] keywords = {"001","002","003"};
- static protected String[] textdetail = {"记录一 记录二 记录三 记录四 记录五","记录一 记录六 记录七 记录八 记录九 记录十 记录三","记录十一 记录十二 记录十三 记录十四 记录十五"};
-
- /*================================================================
- * 名 称:SpanFirstQueryTest
- * 功 能:构造SpanQuery检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void SpanFirstQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- String searchWords = "记录四";
- Term t = new Term("content",searchWords);
- SpanTermQuery query = new SpanTermQuery(t);
- SpanFirstQuery firstquery = new SpanFirstQuery(query,4);
-
- System.out.println(query.toString());
- Hits hits = searcher.search(firstquery);
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++)
- {
- System.out.println(hits.doc(i));
- System.out.println(hits.doc(i).getField("id"));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:SpanNearQueryTest
- * 功 能:构造SpanNearQuery检索查询器,对指定的目录进行查询,找到指定的值,
- * 并输出相应结果。
- ===============================================================*/
- public static void SpanNearQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- Term t1 = new Term("content","记录一");
- Term t2 = new Term("content","记录三");
- Term t3 = new Term("content","记录四");
-
- SpanTermQuery query1 = new SpanTermQuery(t1);
- SpanTermQuery query2 = new SpanTermQuery(t2);
- SpanTermQuery query3 = new SpanTermQuery(t3);
-
- SpanQuery[] queryarray = new SpanQuery[]{query1,query3};
-
- SpanNearQuery nearquery = new SpanNearQuery(queryarray,2,true);
-
- System.out.println(nearquery.toString());
- Hits hits = searcher.search( nearquery );
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++)
- {
- System.out.println(hits.doc(i));
- System.out.println(hits.doc(i).getField("id"));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:SpanNotQueryTest
- * 功 能:构造SpanNotQuery检索查询器,对指定的目录进行查询,找到指定的值,
- * 并输出相应结果。
- ===============================================================*/
- public static void SpanNotQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- Term t1 = new Term("content","记录一");
- Term t2 = new Term("content","记录三");
- Term t3 = new Term("content","记录四");
-
- SpanTermQuery query1 = new SpanTermQuery(t1);
- SpanTermQuery query2 = new SpanTermQuery(t2);
- SpanTermQuery query3 = new SpanTermQuery(t3);
-
- SpanQuery[] queryarray = new SpanQuery[]{query1,query3};
-
- SpanNearQuery nearquery = new SpanNearQuery(queryarray,1,true);
- SpanNotQuery notquery = new SpanNotQuery(nearquery,query2);
-
- System.out.println(notquery.toString());
- Hits hits = searcher.search( notquery );
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++)
- {
- System.out.println(hits.doc(i));
- System.out.println(hits.doc(i).getField("id"));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:SpanOrQueryTest
- * 功 能:构造SpanOrQuery检索查询器,对指定的目录进行查询,找到指定的值,
- * 并输出相应结果。
- ===============================================================*/
- public static void SpanOrQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- Term t1 = new Term("content","记录一");
- Term t2 = new Term("content","记录三");
- Term t3 = new Term("content","记录四");
-
- SpanTermQuery query1 = new SpanTermQuery(t1);
- SpanTermQuery query2 = new SpanTermQuery(t2);
- SpanTermQuery query3 = new SpanTermQuery(t3);
-
- SpanQuery[] queryarray1 = new SpanQuery[]{query1,query2};
- SpanQuery[] queryarray2 = new SpanQuery[]{query2,query3};
-
- SpanNearQuery nearquery1 = new SpanNearQuery(queryarray1,1,true);
- SpanNearQuery nearquery2 = new SpanNearQuery(queryarray2,1,true);
-
- SpanOrQuery orquery = new SpanOrQuery(new SpanNearQuery[]{nearquery1,nearquery2});
-
- System.out.println(orquery.toString());
- Hits hits = searcher.search( orquery );
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++)
- {
- System.out.println(hits.doc(i));
- System.out.println(hits.doc(i).getField("id"));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
-
- /*================================================================
- * 名 称:TermQueryParserTest
- * 功 能:测试QueryParser生成TermQuery对象,并利用检索查询器,对指定的目录进行查询,
- * 找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void TermQueryParserTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- String searchWords = "记录";
- Analyzer analyzer = new SimpleAnalyzer();
- QueryParser parser = new QueryParser("content",analyzer);
- try{
- Query query = parser.parse(searchWords);
- System.out.println(query.toString());
- System.out.println(query.getClass());
-
- Hits hits = searcher.search(query);
-
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++)
- {
- System.out.println(hits.doc(i));
- System.out.println(hits.doc(i).getField("id"));
- }
-
- } catch(ParseException e1){
- e1.printStackTrace();
- }
-
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
-
- }
-
- /*================================================================
- * 名 称:SpanIndexBuilder
- * 功 能:构造磁盘索引,添加内容到指定目录,为后续检索查询做好准备。
- ===============================================================*/
- public static void SpanIndexBuilder(){
- try {
- Analyzer TextAnalyzer = new SimpleAnalyzer();
- IndexWriter TextIndex = new IndexWriter(Dest_Index_Path,TextAnalyzer,true);
- TextIndex.setUseCompoundFile(true);
- for(int i = 0; i < 3 ; i++){
- Document document = new Document();
-
- Field field_id = new Field("id", keywords[i],
- Field.Store.YES,Field.Index.UN_TOKENIZED);
-
- document.add(field_id);
-
- Field field_content = new Field("content", textdetail[i],
- Field.Store.YES,Field.Index.TOKENIZED);
-
- document.add(field_content);
-
- TextIndex.addDocument(document);
- }
- TextIndex.optimize();
- TextIndex.close();
-
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Index success");
- }
- /*================================================================
- * 名 称:main
- * 功 能:测试Lucene索引建立和检索查询功能。
- ===============================================================*/
- public static void main(String[] args) {
-
- SpanIndexBuilder();
- //SpanFirstQueryTest();
- //SpanNearQueryTest();
- //SpanNotQueryTest();
- SpanOrQueryTest();
-
- System.out.println("Test success");
- }
- }