Practice Every Day_14(多字段搜索和多索引搜索)

             今天实现了多字段搜索和多索引搜索,看了看分词器原理部分的讲解。下面是实现多字段搜索和多索引搜索的代码:

//创建索引并实现多字段搜索和多索引搜索
package MySearcher;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

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.NumericField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searchable;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

public class IndexAndSearcher
{     
        private final static Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_35);
        private static Directory directory=null;
      public static  void index_01()
      {
      IndexWriter writer=null;
      try
      {  
        directory=FSDirectory.open(new File("f:Index_01"));
             writer=new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)));
             File file=new File("e:/Lucene实例/example/");
             Document doc=null;
            for(File f:file.listFiles())
           {
             doc=new Document();
             doc.add(new Field("content",new FileReader(f)));
             doc.add(new Field("filename",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
             doc.add(new Field("path",f.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
             doc.add(new NumericField("date",Field.Store.YES,true).setLongValue(f.lastModified()));
             doc.add(new NumericField("size",Field.Store.YES,true).setIntValue((int)(f.length()/1024)));
               writer.addDocument(doc);
            }
         writer.close();
      }
      catch(CorruptIndexException e)
      {
       e.printStackTrace();
      }
      catch(LockObtainFailedException e)
      {
       e.printStackTrace();
      }
      catch(IOException e)
      {
          e.printStackTrace();
      }
    } 
    
      public static  void index_02()
      {
      IndexWriter writer=null;
      try
      {  
        directory=FSDirectory.open(new File("f:Index_02"));
             writer=new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)));
             File file=new File("e:/Lucene实例/example/");
             Document doc=null;
            for(File f:file.listFiles())
           {
             doc=new Document();
             doc.add(new Field("content",new FileReader(f)));
             doc.add(new Field("filename",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
             doc.add(new Field("path",f.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
             doc.add(new NumericField("date",Field.Store.YES,true).setLongValue(f.lastModified()));
             doc.add(new NumericField("size",Field.Store.YES,true).setIntValue((int)(f.length()/1024)));
               writer.addDocument(doc);
            }
         writer.close();
      }
      catch(CorruptIndexException e)
      {
       e.printStackTrace();
      }
      catch(LockObtainFailedException e)
      {
       e.printStackTrace();
      }
      catch(IOException e)
      {
          e.printStackTrace();
      }
    } 
     
      public static  void index_03()
      {
      IndexWriter writer=null;
      try
      {  
        directory=FSDirectory.open(new File("f:Index_03"));
             writer=new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)));
             File file=new File("e:/Lucene实例/example/");
             Document doc=null;
            for(File f:file.listFiles())
           {
             doc=new Document();
             doc.add(new Field("content",new FileReader(f)));
             doc.add(new Field("filename",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
             doc.add(new Field("path",f.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
             doc.add(new NumericField("date",Field.Store.YES,true).setLongValue(f.lastModified()));
             doc.add(new NumericField("size",Field.Store.YES,true).setIntValue((int)(f.length()/1024)));
               writer.addDocument(doc);
            }
         writer.close();
      }
      catch(CorruptIndexException e)
      {
       e.printStackTrace();
      }
      catch(LockObtainFailedException e)
      {
       e.printStackTrace();
      }
      catch(IOException e)
      {
          e.printStackTrace();
      }
    } 
      public static void check() throws IOException
      {      //检查索引是否被正确建立(打印索引)
    FSDirectory directory = FSDirectory.open(new File("f:Index_04"));
    IndexReader reader = IndexReader.open(directory);
    for(int i = 0;i<reader.numDocs();i++)
    {
     System.out.println(reader.document(i));
    }
      }
     

  public static void searcher3(String field1,String field2,String s,Sort sort) throws CorruptIndexException, IOException, ParseException
      {
       Directory directory=FSDirectory.open(new File("f:Index_01"));
       IndexReader reader=IndexReader.open(directory);
       IndexSearcher searcher=new IndexSearcher(reader);
       String[] fields={field1,field2};
       MultiFieldQueryParser mp=new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);
       Query query=mp.parse(s);
       TopDocs tds=searcher.search(query,50,sort);
       int total=tds.totalHits;
       System.out.println("共搜索到"+total+"条结果");
       ScoreDoc[] sds1=tds.scoreDocs;
         for(ScoreDoc sd:sds1)
         {
          Document doc=searcher.doc(sd.doc);
          System.out.println(sd.doc+"   path:"+doc.get("path")+"   filename:"+doc.get("filename"));
         }     
      }
      public static void searcher4(String f,String s) throws IOException
      {
       Directory directory1=FSDirectory.open(new File("f:Index_01"));
       Directory directory2=FSDirectory.open(new File("f:Index_02"));
       Directory directory3=FSDirectory.open(new File("f:Index_03"));
       IndexReader readers[]={IndexReader.open(directory1),IndexReader.open(directory2),IndexReader.open(directory3)};
        MultiReader reader=new MultiReader(readers);
        IndexSearcher searcher=new IndexSearcher(reader);
  Term t=new Term(f,s);
     FuzzyQuery query=new FuzzyQuery(t,0.1f);
         TopDocs tds=searcher.search(query,50);
     int total=tds.totalHits;
        System.out.println("共搜索到"+total+"条结果");
     ScoreDoc[] sds1=tds.scoreDocs;
       for(ScoreDoc sd:sds1)
       {
        Document doc=searcher.doc(sd.doc);
        System.out.println(sd.doc+"   path:"+doc.get("path")+"   filename:"+doc.get("filename"));
       }     
      }
}   

 //测试类

package MySearcher;

import java.io.IOException;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Sort;

public class Test {

  public static void main(String[] args) throws IOException ,CorruptIndexException, ParseException
   {
    String f="content";
    String s="java";
    int num=10;
    IndexAndSearcher.index_01();
    IndexAndSearcher.index_02();
    IndexAndSearcher.index_03(); 
    System.out.println("多字段搜索结果:");
    //当content和filename中都含有TimeRecorder时,一个文件只显示一个结果
    IndexAndSearcher.searcher3("content","filename","TimeRecorder",Sort.INDEXORDER);
    System.out.println("多索引搜索结果为");
    IndexAndSearcher.searcher4("content", "java");
   }

}

今天小小luxury了一下,看了点“HAPPY    CAMP”还有《晚秋》。明天学习同义词查询。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值