LuceneSearchQueryType.java
上传用户:cctqzzy
上传日期:2022-03-14
资源大小:12198k
文件大小:24k
- package chapter6;
- import java.io.IOException;
- import java.util.Date;
- 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.TermQuery;
- import org.apache.lucene.search.RangeQuery;
- import org.apache.lucene.search.BooleanQuery;
- import org.apache.lucene.search.PrefixQuery;
- import org.apache.lucene.search.PhraseQuery;
- import org.apache.lucene.search.FuzzyQuery;
- import org.apache.lucene.search.WildcardQuery;
- import org.apache.lucene.search.BooleanClause;
- import org.apache.lucene.search.IndexSearcher;
- import org.apache.lucene.queryParser.*;
- import org.apache.lucene.search.MultiSearcher;
- public class LuceneSearchQueryType {
-
- private static String Dest_Index_Path = "D:\workshop\TextIndex";
-
- static protected String[] keywords = {"001","002","003","004","005"};
- static protected String[] textdetail = {"记录 一","记录 二", "记录 三", "一 2345 记录", "记录 新 一"} ;
- private static String Dest_Index_Path2 = "D:\workshop\TextIndex2";
-
- static protected String[] keywords2 = {"001","002","003","004","005"};
- static protected String[] textdetail2 = {"Record 一","Record 二", "Record 三", "一 2345 Record", "Record 新 一"} ;
-
- /*================================================================
- * 名 称:TermQueryTest
- * 功 能:构造检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void TermQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- String searchWords = "记录一";
- Term t = new Term("id","002");
- Query query = new TermQuery(t);
- System.out.println(query.toString());
- 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 (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");
-
- }
- /*================================================================
- * 名 称:RangeQueryTest
- * 功 能:构造范围检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void RangeQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- Term termStart = new Term("numval","150");
- Term termEnd = new Term("numval","155");
-
- Query query = new RangeQuery(termStart,termEnd,true);
- System.out.println(query.toString());
- Hits hits = searcher.search(query);
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++)
- {
- System.out.println(hits.doc(i));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:RangeQueryParserTest
- * 功 能:测试QueryParser生成RangeQuery对象,并利用检索查询器,对指定的目录进行查询,
- * 找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void RangeQueryParserTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- String searchWords = "{150 TO 155}";
- Analyzer analyzer = new SimpleAnalyzer();
- QueryParser parser = new QueryParser("numval",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));
- }
-
- } catch(ParseException e1){
- e1.printStackTrace();
- }
-
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
-
- /*================================================================
- * 名 称:BooleanQueryTest
- * 功 能:构造布尔检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void BooleanQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- Term term1 = new Term("content","记录");
- Term term2 = new Term("content","二");
-
- TermQuery query1 = new TermQuery(term1);
- TermQuery query2 = new TermQuery(term2);
- BooleanQuery query = new BooleanQuery();
- query.add(query1,BooleanClause.Occur.MUST);
- query.add(query2,BooleanClause.Occur.MUST);
-
- System.out.println(query.toString());
- Hits hits = searcher.search(query);
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++)
- {
- System.out.println(hits.doc(i));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:BooleanQueryParserTest
- * 功 能:测试QueryParser生成BooleanQuery对象,并利用检索查询器,对指定的目录进行查询,
- * 找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void BooleanQueryParserTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- String searchWords = "(记录 AND 一)";
- // String searchWords = "((一 OR 二)AND 记录)";
- 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));
- }
-
- } catch(ParseException e1){
- e1.printStackTrace();
- }
-
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:PhraseQueryTest
- * 功 能:构造短语检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void PhraseQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- Term term1 = new Term("content","记录");
- Term term2 = new Term("content","一");
-
- PhraseQuery query = new PhraseQuery();
- query.add(term1);
- query.add(term2);
- query.setSlop(2);
- System.out.println(query.toString());
- Hits hits = searcher.search(query);
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++)
- {
- System.out.println(hits.doc(i));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:PhraseQueryParserTest
- * 功 能:测试QueryParser生成PhraseQuery对象,并利用检索查询器,对指定的目录进行查询,
- * 找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void PhraseQueryParserTest(){
-
- 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");
-
- }
- /*================================================================
- * 名 称:PrefixQueryTest
- * 功 能:构造前缀检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void PrefixQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- Term term1 = new Term("content","记录");
-
- PrefixQuery query = new PrefixQuery(term1);
- System.out.println(query.toString());
- Hits hits = searcher.search(query);
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++)
- {
- System.out.println(hits.doc(i));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:PrefixQueryParserTest
- * 功 能:测试QueryParser生成PrefixQuery对象,并利用检索查询器,对指定的目录进行查询,
- * 找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void PrefixQueryParserTest(){
-
- 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));
- }
-
- } catch(ParseException e1){
- e1.printStackTrace();
- }
-
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
-
- /*================================================================
- * 名 称:IndexBuilder
- * 功 能:构造磁盘索引,添加内容到指定目录,为后续检索查询做好准备。
- ===============================================================*/
- public static void IndexBuilder(){
- try {
- Analyzer TextAnalyzer = new SimpleAnalyzer();
- IndexWriter TextIndex = new IndexWriter(Dest_Index_Path,TextAnalyzer,true);
- TextIndex.setUseCompoundFile(true);
- for(int i = 0; i < 5 ; 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");
- }
- /*================================================================
- * 名 称:IndexBuilder2
- * 功 能:构造磁盘索引,添加内容到指定目录,为后续检索查询做好准备。
- ===============================================================*/
- public static void IndexBuilder2(){
- try {
- Analyzer TextAnalyzer = new SimpleAnalyzer();
- IndexWriter TextIndex = new IndexWriter(Dest_Index_Path2,TextAnalyzer,true);
- TextIndex.setUseCompoundFile(true);
- for(int i = 0; i < 5 ; i++){
- Document document = new Document();
-
- Field field_id = new Field("id", keywords2[i],
- Field.Store.YES,Field.Index.UN_TOKENIZED);
-
- document.add(field_id);
-
- Field field_content = new Field("content", textdetail2[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");
- }
- /*================================================================
- * 名 称:DataIndexBuilder
- * 功 能:构造数据类型值的磁盘索引,添加内容到指定目录,为后续检索查询做好准备。
- ===============================================================*/
- public static void DataIndexBuilder(){
-
- try {
- Integer nNum;
-
- Analyzer TextAnalyzer = new SimpleAnalyzer();
- IndexWriter TextIndex = new IndexWriter(Dest_Index_Path,TextAnalyzer,true);
- TextIndex.setUseCompoundFile(true);
- System.out.println("Index Value:");
- for(int i = 100; i < 160 ; i++){
- Document document = new Document();
- nNum = i;
- String sortvalue = nNum.toString();
- System.out.print(sortvalue);
- System.out.print(" ");
- if( i%20 == 19){
- System.out.println("");
- }
- Field field_data = new Field("numval", sortvalue,
- Field.Store.YES,Field.Index.UN_TOKENIZED);
- document.add(field_data);
-
- TextIndex.addDocument(document);
- }
-
- TextIndex.optimize();
- TextIndex.close();
-
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Index success");
- }
-
- /*================================================================
- * 名 称:FuzzyQueryTest
- * 功 能:构造模糊检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void FuzzyQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- // Term term = new Term("content","登录");
- // Term term = new Term("content","记者");
- Term term = new Term("content","记录");
-
- FuzzyQuery query = new FuzzyQuery(term,0.1f,1);
- System.out.println(query.toString());
- Hits hits = searcher.search(query);
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++)
- {
- System.out.println(hits.doc(i));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:FuzzyQueryParserTest
- * 功 能:测试QueryParser生成FuzzyQuery对象,并利用检索查询器,对指定的目录进行查询,
- * 找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void FuzzyQueryParserTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- String searchWords = "记录一~0.1";
- 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");
-
- }
-
- /*================================================================
- * 名 称:WildcardQueryTest
- * 功 能:构造通配符检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void WildcardQueryTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- Term term = new Term("content","记*");
- WildcardQuery query = new WildcardQuery(term);
- System.out.println(query.toString());
- Hits hits = searcher.search(query);
- System.out.println("Search result:");
- for(int i=0; i < hits.length(); i++){
- System.out.println(hits.doc(i));
- }
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:WildcardQueryParserTest
- * 功 能:测试QueryParser生成WildcardQuery对象,并利用检索查询器,对指定的目录进行查询,
- * 找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void WildcardQueryParserTest(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- String searchWords = "0*1";
- Analyzer analyzer = new SimpleAnalyzer();
- QueryParser parser = new QueryParser("id",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");
-
- }
-
- /*================================================================
- * 名 称:MultiFeildQueryParserTest
- * 功 能:测试QueryParser生成MultiFeildQuery对象,并利用检索查询器,对指定的目录进行查询,
- * 找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void MultiFeildQueryParserTest(){
-
- try {
- String word_list[] ={"002","记录"};
- String feild_list[] ={"id","content"};
- try{
-
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- BooleanQuery boolquery = new BooleanQuery();
- Analyzer analyzer = new SimpleAnalyzer();
- for(int m=0;m<2;m++)
- {
- QueryParser parser = new QueryParser(feild_list[m],analyzer);
- Query query = parser.parse(word_list[m]);
- boolquery.add(query, BooleanClause.Occur.SHOULD);
- }
- //Query query = parser.parse(searchWords);
- System.out.println(boolquery.toString());
- System.out.println(boolquery.getClass());
-
- Hits hits = searcher.search(boolquery);
-
- 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");
-
- }
- /*================================================================
- * 名 称:MultiFeildQueryParserTest
- * 功 能:测试QueryParser生成MultiFeildQuery对象,并利用检索查询器,对指定的目录进行查询,
- * 找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void MultiFeildTermTest(){
-
- try {
- String word_list[] ={"002","记录"};
- String feild_list[] ={"id","content"};
- try{
-
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
- BooleanQuery boolquery = new BooleanQuery();
- Analyzer analyzer = new SimpleAnalyzer();
- for(int m=0;m<2;m++)
- {
- QueryParser parser = new QueryParser(feild_list[m],analyzer);
- Query query = parser.parse(word_list[m]);
- boolquery.add(query, BooleanClause.Occur.SHOULD);
- }
-
- //Query query = parser.parse(searchWords);
- System.out.println(boolquery.toString());
- System.out.println(boolquery.getClass());
-
- Hits hits = searcher.search(boolquery);
-
- 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");
-
- }
-
-
- /*================================================================
- * 名 称:MultiSearcherQueryTest
- * 功 能:构造检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
- ===============================================================*/
- public static void MultiSearcherQueryTest(){
-
- try {
- IndexSearcher searcher1 = new IndexSearcher(Dest_Index_Path);
- IndexSearcher searcher2 = new IndexSearcher(Dest_Index_Path2);
-
- IndexSearcher[] searchers = {searcher1,searcher2};
- MultiSearcher multisearcher = new MultiSearcher(searchers);
-
- String searchWords = "二";
- Term t = new Term("id","002");
- Query query = new TermQuery(t);
- System.out.println(query.toString());
- Hits hits = multisearcher.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 (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search success");
- }
-
- /*================================================================
- * 名 称:main
- * 功 能:测试Lucene索引建立和检索查询功能。
- ===============================================================*/
- public static void main(String[] args) {
-
- //IndexBuilder();
- //TermQueryTest();
- //TermQueryParserTest();
-
- //DataIndexBuilder();
- //RangeQueryTest();
- //RangeQueryParserTest();
-
- //IndexBuilder();
- //BooleanQueryTest();
- //BooleanQueryParserTest();
-
- //IndexBuilder();
- //PrefixQueryTest();
- //PrefixQueryParserTest();
-
-
- //IndexBuilder();
- //PhraseQueryTest();
- //PhraseQueryParserTest();
-
- //IndexBuilder();
- //FuzzyQueryTest();
- //FuzzyQueryParserTest();
-
- //IndexBuilder();
- //WildcardQueryTest();
- //WildcardQueryParserTest();
-
- //IndexBuilder();
- //MultiFeildQueryParserTest();
-
- IndexBuilder();
- IndexBuilder2();
- MultiSearcherQueryTest();
- System.out.println("Test success");
- }
- }