基于Lucene多索引进行索引和搜索

Lucene支持创建多个索引目录,同时存储多个索引。我们可能担心的问题是,在索引的过程中,分散地存储到多个索引目录中,是否在搜索时能够得到全局的相关度计算得分,其实Lucene的ParallelMultiSearcher和MultiSearcher支持全局得分的计算,也就是说,虽然索引分布在多个索引目录中,在搜索的时候还会将全部的索引数据聚合在一起进行查询匹配和得分计算。


索引目录处理


下面我们通过将索引随机地分布到以a~z的26个目录中,并实现一个索引和搜索的程序,来验证一下Lucene得分的计算。

首先,实现一个用来构建索引目录以及处理搜索的工具类,代码如下所示:

  1. package org.shirdrn.lucene;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.HashMap;  
  7. import java.util.Iterator;  
  8. import java.util.List;  
  9. import java.util.Map;  
  10. import java.util.Random;  
  11. import java.util.concurrent.locks.Lock;  
  12. import java.util.concurrent.locks.ReentrantLock;  
  13.   
  14. import org.apache.lucene.index.CorruptIndexException;  
  15. import org.apache.lucene.index.IndexWriter;  
  16. import org.apache.lucene.index.IndexWriterConfig;  
  17. import org.apache.lucene.index.IndexWriter.MaxFieldLength;  
  18. import org.apache.lucene.search.DefaultSimilarity;  
  19. import org.apache.lucene.search.IndexSearcher;  
  20. import org.apache.lucene.search.Searchable;  
  21. import org.apache.lucene.search.Similarity;  
  22. import org.apache.lucene.store.FSDirectory;  
  23. import org.apache.lucene.store.LockObtainFailedException;  
  24. import org.shirdrn.lucene.MultipleIndexing.IndexWriterObj;  
  25.   
  26. /** 
  27.  * Indexing accross multiple Lucene indexes. 
  28.  *  
  29.  * @author shirdrn 
  30.  * @date   2011-12-12 
  31.  */  
  32. public class IndexHelper {  
  33.       
  34.     private static WriterHelper writerHelper = null;  
  35.     private static SearcherHelper searcherHelper = null;  
  36.       
  37.     public static WriterHelper newWriterHelper(String root, IndexWriterConfig indexConfig) {  
  38.         return WriterHelper.newInstance(root, indexConfig);  
  39.     }  
  40.       
  41.     public static SearcherHelper newSearcherHelper(String root, IndexWriterConfig indexConfig) {  
  42.         return SearcherHelper.newInstance(root, indexConfig);  
  43.     }  
  44.   
  45.     protected static class WriterHelper {  
  46.         private String alphabet = "abcdefghijklmnopqrstuvwxyz";  
  47.         private Lock locker = new ReentrantLock();  
  48.         private String indexRootDir = null;  
  49.         private IndexWriterConfig indexConfig;  
  50.         private Map<Character, IndexWriterObj> indexWriters = new HashMap<Character, IndexWriterObj>();  
  51.         private static Random random = new Random();  
  52.         private WriterHelper() {  
  53.               
  54.         }  
  55.         private synchronized static WriterHelper newInstance(String root, IndexWriterConfig indexConfig) {  
  56.             if(writerHelper==null) {  
  57.                 writerHelper = new WriterHelper();  
  58.                 writerHelper.indexRootDir = root;  
  59.                 writerHelper.indexConfig = indexConfig;  
  60.             }  
  61.             return writerHelper;  
  62.         }  
  63.         public IndexWriterObj selectIndexWriter() {  
  64.             int pos = random.nextInt(alphabet.length());  
  65.             char ch = alphabet.charAt(pos);  
  66.             String dir = new String(new char[] {ch});  
  67.             locker.lock();  
  68.             try {  
  69.                 File path = new File(indexRootDir, dir);  
  70.                 if(!path.exists()) {  
  71.                     path.mkdir();  
  72.                 }  
  73.                 if(!indexWriters.containsKey(ch)) {  
  74.                     IndexWriter indexWriter = new IndexWriter(FSDirectory.open(path), indexConfig.getAnalyzer(), MaxFieldLength.UNLIMITED);  
  75.                     indexWriters.put(ch, new IndexWriterObj(indexWriter, dir));  
  76.                 }  
  77.             } catch (CorruptIndexException e) {  
  78.                 e.printStackTrace();  
  79.             } catch (LockObtainFailedException e) {  
  80.                 e.printStackTrace();  
  81.             } catch (IOException e) {  
  82.                 e.printStackTrace();  
  83.             } finally {  
  84.                 locker.unlock();  
  85.             }  
  86.             return indexWriters.get(ch);  
  87.         }  
  88.         @SuppressWarnings("deprecation")  
  89.         public void closeAll(boolean autoOptimize) {  
  90.             Iterator<Map.Entry<Character, IndexWriterObj>> iter = indexWriters.entrySet().iterator();  
  91.             while(iter.hasNext()) {  
  92.                 Map.Entry<Character, IndexWriterObj> entry = iter.next();  
  93.                 try {  
  94.                     if(autoOptimize) {  
  95.                         entry.getValue().indexWriter.optimize();  
  96.                     }  
  97.                     entry.getValue().indexWriter.close();  
  98.                 } catch (CorruptIndexException e) {  
  99.                     e.printStackTrace();  
  100.                 } catch (IOException e) {  
  101.                     e.printStackTrace();  
  102.                 }  
  103.             }  
  104.         }  
  105.     }  
  106.       
  107.     protected static class SearcherHelper {  
  108.         private List<IndexSearcher> searchers = new ArrayList<IndexSearcher>();  
  109.         private Similarity similarity = new DefaultSimilarity();  
  110.         private SearcherHelper() {  
  111.               
  112.         }  
  113.         private synchronized static SearcherHelper newInstance(String root, IndexWriterConfig indexConfig) {  
  114.             if(searcherHelper==null) {  
  115.                 searcherHelper = new SearcherHelper();  
  116.                 if(indexConfig.getSimilarity()!=null) {  
  117.                     searcherHelper.similarity = indexConfig.getSimilarity();  
  118.                 }  
  119.                 File indexRoot = new File(root);  
  120.                 File[] files = indexRoot.listFiles();  
  121.                 for(File f : files) {  
  122.                     IndexSearcher searcher = null;  
  123.                     try {  
  124.                         searcher = new IndexSearcher(FSDirectory.open(f));  
  125.                     } catch (CorruptIndexException e) {  
  126.                         e.printStackTrace();  
  127.                     } catch (IOException e) {  
  128.                         e.printStackTrace();  
  129.                     }  
  130.                     if(searcher!=null) {  
  131.                         searcher.setSimilarity(searcherHelper.similarity);  
  132.                         searcherHelper.searchers.add(searcher);  
  133.                     }  
  134.                 }  
  135.             }  
  136.             return searcherHelper;  
  137.         }  
  138.         public void closeAll() {  
  139.             Iterator<IndexSearcher> iter = searchers.iterator();  
  140.             while(iter.hasNext()) {  
  141.                 try {  
  142.                     iter.next().close();  
  143.                 } catch (IOException e) {  
  144.                     e.printStackTrace();  
  145.                 }  
  146.             }  
  147.         }  
  148.         public Searchable[] getSearchers() {  
  149.             Searchable[] a = new Searchable[searchers.size()];  
  150.             return searchers.toArray(a);  
  151.         }  
  152.     }  
  153. }  
由于在索引的时候,同时打开了多个Directory实例,而每个Directory对应一个IndexWriter,我们通过记录a~z这26个字母为每个IndexWriter的名字,将IndexWriter和目录名称包裹在IndexWriterObj类的对象中,便于通过日志看到实际数据的分布。在进行Lucene Document构建的时候,将这个索引目录的名字(a~z字符中之一)做成一个Field。在索引的时候,值需要调用IndexHelper.WriterHelper的selectIndexWriter()方法,即可以自动选择对应的IndexWriter实例去进行索引。

在搜索的时候,通过IndexHelper.SearcherHelper工具来获取多个Searchable实例的数组,调用getSearchers()即可以获取到,提供给MultiSearcher构建搜索。


索引实现


我们的数据源,选择根据指定的查询条件直接从MongoDB中读取,所以有关处理与MongoDB进行交互的代码都封装到了处理索引的代码中,通过内部类实现。我们看一下,执行索引数据的实现,代码如下所示:

  1. package org.shirdrn.lucene;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.Serializable;  
  5. import java.util.HashMap;  
  6. import java.util.Iterator;  
  7. import java.util.Map;  
  8. import java.util.concurrent.atomic.AtomicInteger;  
  9.   
  10. import org.apache.lucene.document.Document;  
  11. import org.apache.lucene.document.Field;  
  12. import org.apache.lucene.document.Field.Index;  
  13. import org.apache.lucene.document.Field.Store;  
  14. import org.apache.lucene.index.CorruptIndexException;  
  15. import org.apache.lucene.index.IndexWriter;  
  16. import org.apache.lucene.index.IndexWriterConfig;  
  17. import org.shirdrn.lucene.IndexHelper.WriterHelper;  
  18. import org.slf4j.Logger;  
  19. import org.slf4j.LoggerFactory;  
  20.   
  21. import com.mongodb.BasicDBObject;  
  22. import com.mongodb.DBCollection;  
  23. import com.mongodb.DBCursor;  
  24. import com.mongodb.Mongo;  
  25. import com.mongodb.MongoException;  
  26.   
  27. /** 
  28.  * Indexing accross multiple Lucene indexes. 
  29.  *  
  30.  * @author shirdrn 
  31.  * @date   2011-12-12 
  32.  */  
  33. public class MultipleIndexing {  
  34.   
  35.     private static Logger LOG = LoggerFactory.getLogger(MultipleIndexing.class);  
  36.     private DBCollection pageColletion;  
  37.     private WriterHelper writerHelper;  
  38.     private Map<IndexWriter, IntCounter> docCountPerIndexWriter = new HashMap<IndexWriter, IntCounter>();  
  39.     private int maxIndexCommitCount = 100;  
  40.     private AtomicInteger docCounter = new AtomicInteger();  
  41.   
  42.     public MultipleIndexing(String indexRoot, int maxIndexCommitCount, MongoConfig mongoConfig, IndexWriterConfig indexConfig) {  
  43.         super();  
  44.         if (maxIndexCommitCount != 0) {  
  45.             this.maxIndexCommitCount = maxIndexCommitCount;  
  46.         }  
  47.         pageColletion = MongoHelper.newHelper(mongoConfig).getCollection(mongoConfig.collectionName);  
  48.         writerHelper = IndexHelper.newWriterHelper(indexRoot, indexConfig);  
  49.     }  
  50.   
  51.     /** 
  52.      * Indexing 
  53.      * @param conditions 
  54.      */  
  55.     public void index(Map<String, Object> conditions) {  
  56.         DBCursor cursor = pageColletion.find(new BasicDBObject(conditions));  
  57.         try {  
  58.             while (cursor.hasNext()) {  
  59.                 try {  
  60.                     IndexWriterObj obj = writerHelper.selectIndexWriter();  
  61.                     Document document = encapsulate(cursor.next().toMap(), obj.name);  
  62.                     obj.indexWriter.addDocument(document);  
  63.                     docCounter.addAndGet(1);  
  64.                     LOG.info("Global docCounter: " + docCounter.get());  
  65.                     increment(obj.indexWriter);  
  66.                     checkCommit(obj.indexWriter);  
  67.                 } catch (MongoException e) {  
  68.                     e.printStackTrace();  
  69.                 } catch (CorruptIndexException e) {  
  70.                     e.printStackTrace();  
  71.                 } catch (IOException e) {  
  72.                     e.printStackTrace();  
  73.                 }  
  74.             }  
  75.             finallyCommitAll();  
  76.         } catch (Exception e) {  
  77.             e.printStackTrace();  
  78.         } finally {  
  79.             cursor.close();  
  80.             writerHelper.closeAll(true);  
  81.             LOG.info("Close all indexWriters.");  
  82.         }  
  83.     }  
  84.   
  85.     private void finallyCommitAll() throws Exception {  
  86.         Iterator<IndexWriter> iter = docCountPerIndexWriter.keySet().iterator();  
  87.         while(iter.hasNext()) {  
  88.             iter.next().commit();  
  89.         }  
  90.     }  
  91.   
  92.     private void checkCommit(IndexWriter indexWriter) throws Exception {  
  93.         if(docCountPerIndexWriter.get(indexWriter).value%maxIndexCommitCount==0) {  
  94.             indexWriter.commit();  
  95.             LOG.info("Commit: " + indexWriter + ", " + docCountPerIndexWriter.get(indexWriter).value);  
  96.         }  
  97.     }  
  98.   
  99.     private void increment(IndexWriter indexWriter) {  
  100.         IntCounter counter = docCountPerIndexWriter.get(indexWriter);  
  101.         if (counter == null) {  
  102.             counter = new IntCounter(1);  
  103.             docCountPerIndexWriter.put(indexWriter, counter);  
  104.         } else {  
  105.             ++counter.value;  
  106.         }  
  107.     }  
  108.   
  109.     @SuppressWarnings("unchecked")  
  110.     private Document encapsulate(Map map, String path) {  
  111.         String title = (String) map.get("title");  
  112.         String content = (String) map.get("content");  
  113.         String url = (String) map.get("url");  
  114.         Document doc = new Document();  
  115.         doc.add(new Field(FieldName.TITLE, title, Store.YES, Index.ANALYZED_NO_NORMS));  
  116.         doc.add(new Field(FieldName.CONTENT, content, Store.NO, Index.ANALYZED));  
  117.         doc.add(new Field(FieldName.URL, url, Store.YES, Index.NOT_ANALYZED_NO_NORMS));  
  118.         doc.add(new Field(FieldName.PATH, path, Store.YES, Index.NOT_ANALYZED_NO_NORMS));  
  119.         return doc;  
  120.     }  
  121.   
  122.     protected interface FieldName {  
  123.         public static final String TITLE = "title";  
  124.         public static final String CONTENT = "content";  
  125.         public static final String URL = "url";  
  126.         public static final String PATH = "path";  
  127.     }  
  128.   
  129.     protected class IntCounter {  
  130.         public IntCounter(int value) {  
  131.             super();  
  132.             this.value = value;  
  133.         }  
  134.         private int value;  
  135.     }  
  136.       
  137.     protected static class IndexWriterObj {  
  138.         IndexWriter indexWriter;  
  139.         String name;  
  140.         public IndexWriterObj(IndexWriter indexWriter, String name) {  
  141.             super();  
  142.             this.indexWriter = indexWriter;  
  143.             this.name = name;  
  144.         }  
  145.         @Override  
  146.         public String toString() {  
  147.             return "[" + name + "]";  
  148.         }  
  149.     }  
  150.       
  151.     public static class MongoConfig implements Serializable {  
  152.         private static final long serialVersionUID = -3028092758346115702L;  
  153.         private String host;  
  154.         private int port;  
  155.         private String dbname;  
  156.         private String collectionName;  
  157.   
  158.         public MongoConfig(String host, int port, String dbname, String collectionName) {  
  159.             super();  
  160.             this.host = host;  
  161.             this.port = port;  
  162.             this.dbname = dbname;  
  163.             this.collectionName = collectionName;  
  164.         }  
  165.   
  166.         @Override  
  167.         public boolean equals(Object obj) {  
  168.             MongoConfig other = (MongoConfig) obj;  
  169.             return host.equals(other.host) && port == other.port && dbname.equals(other.dbname) && collectionName.equals(other.collectionName);  
  170.         }  
  171.     }  
  172.   
  173.     protected static class MongoHelper {  
  174.         private static Mongo mongo;  
  175.         private static MongoHelper helper;  
  176.         private MongoConfig mongoConfig;  
  177.   
  178.         private MongoHelper(MongoConfig mongoConfig) {  
  179.             super();  
  180.             this.mongoConfig = mongoConfig;  
  181.         }  
  182.   
  183.         public synchronized static MongoHelper newHelper(MongoConfig mongoConfig) {  
  184.             try {  
  185.                 if (helper == null) {  
  186.                     helper = new MongoHelper(mongoConfig);  
  187.                     mongo = new Mongo(mongoConfig.host, mongoConfig.port);  
  188.                     Runtime.getRuntime().addShutdownHook(new Thread() {  
  189.                         @Override  
  190.                         public void run() {  
  191.                             if (mongo != null) {  
  192.                                 mongo.close();  
  193.                             }  
  194.                         }  
  195.                     });  
  196.                 }  
  197.             } catch (Exception e) {  
  198.                 e.printStackTrace();  
  199.             }  
  200.             return helper;  
  201.         }  
  202.   
  203.         public DBCollection getCollection(String collectionName) {  
  204.             DBCollection c = null;  
  205.             try {  
  206.                 c = mongo.getDB(mongoConfig.dbname).getCollection(collectionName);  
  207.             } catch (Exception e) {  
  208.                 e.printStackTrace();  
  209.             }  
  210.             return c;  
  211.         }  
  212.     }  
  213. }  
上面代码是基于单线程的,如果你的应用具有海量的数据,这种方式势必会影响索引的吞吐量。不过基于上述代码很容易将其改造成多线程的,基本思路就是:将数据源在内存中适当缓存,然后基于生产者-消费者模型,启动多个消费线程去获取数据同时并发地向IndexWriter推送数据(尤其需要注意的是,在同一个IndexWriter实例上不要进行同步,否则容易造成死锁,因为IndexWriter是线程安全的)。

另外需要说明一点,有关索引数据分布和更新的问题。基于上述随机选择索引目录,在一定程度上能够均匀地将数据分布到不同的目录中,但是在更新的时候,如果处理不当会造成数据的重复(因为随机),解决重复的方法就是在外部增加重复检测工作,限制将重复(非常相似)的文档再次进行索引。

下面我们看一下索引的测试用例,代码如下所示:

  1. package org.shirdrn.lucene;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import junit.framework.TestCase;  
  7.   
  8. import org.apache.lucene.analysis.Analyzer;  
  9. import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;  
  10. import org.apache.lucene.index.IndexWriterConfig;  
  11. import org.apache.lucene.index.IndexWriterConfig.OpenMode;  
  12. import org.apache.lucene.util.Version;  
  13. import org.shirdrn.lucene.MultipleIndexing.MongoConfig;  
  14.   
  15. public class TestMultipleIndexing extends TestCase {  
  16.   
  17.     MultipleIndexing indexer;  
  18.       
  19.     @Override  
  20.     protected void setUp() throws Exception {  
  21.         MongoConfig mongoConfig = new MongoConfig("192.168.0.184"27017"page""Article");  
  22.         String indexRoot = "E:\\Store\\indexes";  
  23.         int maxIndexCommitCount = 200;  
  24.         Analyzer a = new SmartChineseAnalyzer(Version.LUCENE_35, true);  
  25.         IndexWriterConfig indexConfig = new IndexWriterConfig(Version.LUCENE_35, a);  
  26.         indexConfig.setOpenMode(OpenMode.CREATE);  
  27.         indexer = new MultipleIndexing(indexRoot, maxIndexCommitCount, mongoConfig, indexConfig);  
  28.     }  
  29.       
  30.     @Override  
  31.     protected void tearDown() throws Exception {  
  32.         super.tearDown();  
  33.     }  
  34.       
  35.     public void testIndexing() {  
  36.         Map<String, Object> conditions = new HashMap<String, Object>();  
  37.         conditions.put("spiderName""sinaSpider");  
  38.         indexer.index(conditions);  
  39.     }  
  40. }  
我这里,索引了9w多篇文档,生成的索引还算均匀地分布在名称为a~z的26个目录中。

搜索实现


在搜索的时候,你可以选择ParallelMultiSearcher或MultiSearcher的任意一个,MultiSearcher是在搜索时候,通过一个循环来遍历多个索引获取到检索结果,而ParallelMultiSearcher则是启动多个线程并行执行搜索,使用它们的效率在不同配置的机器上效果是不同的,在实际使用的时候根据你的需要来决定。我简单地使用了MultiSearcher来构建搜索,实现代码如下所示:

  1. package org.shirdrn.lucene;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.lucene.analysis.Analyzer;  
  6. import org.apache.lucene.index.CorruptIndexException;  
  7. import org.apache.lucene.index.IndexWriterConfig;  
  8. import org.apache.lucene.queryParser.ParseException;  
  9. import org.apache.lucene.queryParser.QueryParser;  
  10. import org.apache.lucene.search.MultiSearcher;  
  11. import org.apache.lucene.search.Query;  
  12. import org.apache.lucene.search.ScoreDoc;  
  13. import org.apache.lucene.search.Searcher;  
  14. import org.apache.lucene.search.TopScoreDocCollector;  
  15. import org.apache.lucene.util.Version;  
  16. import org.shirdrn.lucene.IndexHelper.SearcherHelper;  
  17. import org.slf4j.Logger;  
  18. import org.slf4j.LoggerFactory;  
  19.   
  20. /** 
  21.  * Searching accross multiple Lucene indexes. 
  22.  *  
  23.  * @author shirdrn 
  24.  * @date   2011-12-12 
  25.  */  
  26. public class MultipleSearching {  
  27.   
  28.     private static Logger LOG = LoggerFactory.getLogger(MultipleSearching.class);  
  29.     private SearcherHelper searcherHelper;  
  30.     private Searcher searcher;  
  31.     private QueryParser queryParser;  
  32.     private IndexWriterConfig indexConfig;  
  33.       
  34.     private Query query;  
  35.     private ScoreDoc[] scoreDocs;  
  36.       
  37.     public MultipleSearching(String indexRoot, IndexWriterConfig indexConfig) {  
  38.         searcherHelper = IndexHelper.newSearcherHelper(indexRoot, indexConfig);  
  39.         this.indexConfig = indexConfig;  
  40.         try {  
  41.             searcher = new MultiSearcher(searcherHelper.getSearchers());  
  42.             searcher.setSimilarity(indexConfig.getSimilarity());  
  43.             queryParser = new QueryParser(Version.LUCENE_35, "content", indexConfig.getAnalyzer());  
  44.         } catch (IOException e) {  
  45.             e.printStackTrace();  
  46.         }  
  47.     }  
  48.       
  49.     public void search(String queries) {  
  50.         try {  
  51.             query = queryParser.parse(queries);  
  52.             TopScoreDocCollector collector = TopScoreDocCollector.create(100000true);  
  53.             searcher.search(query, collector);  
  54.             scoreDocs = collector.topDocs().scoreDocs;  
  55.         } catch (ParseException e) {  
  56.             e.printStackTrace();  
  57.         } catch (IOException e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.     }  
  61.       
  62.     public void iterateDocs(int start, int end) {  
  63.         for (int i = start; i < Math.min(scoreDocs.length, end); i++) {  
  64.             try {  
  65.                 LOG.info(searcher.doc(scoreDocs[i].doc).toString());  
  66.             } catch (CorruptIndexException e) {  
  67.                 e.printStackTrace();  
  68.             } catch (IOException e) {  
  69.                 e.printStackTrace();  
  70.             }  
  71.         }  
  72.     }  
  73.       
  74.     public void explain(int start, int end) {  
  75.         for (int i = start; i < Math.min(scoreDocs.length, end); i++) {  
  76.             try {  
  77.                 System.out.println(searcher.explain(query, scoreDocs[i].doc));  
  78.             } catch (CorruptIndexException e) {  
  79.                 e.printStackTrace();  
  80.             } catch (IOException e) {  
  81.                 e.printStackTrace();  
  82.             }  
  83.         }  
  84.     }  
  85.       
  86.     public void close() {  
  87.         searcherHelper.closeAll();  
  88.         try {  
  89.             searcher.close();  
  90.         } catch (IOException e) {  
  91.             e.printStackTrace();  
  92.         }  
  93.     }  
  94. }  
我们的一个目的是查看搜索是否是在多个索引目录上进行检索,并且最终相关度排序是基于多个索引计算的。上面给出了一个更接近测试用例的实现,iterateDocs()迭代出文档并输出,explain()方法查看得分计算明细。
下面给出搜索的测试用例,代码如下所示:
  1. package org.shirdrn.lucene;  
  2.   
  3. import org.apache.lucene.analysis.Analyzer;  
  4. import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;  
  5. import org.apache.lucene.index.IndexWriterConfig;  
  6. import org.apache.lucene.util.Version;  
  7.   
  8. import junit.framework.TestCase;  
  9.   
  10. public class TestMultipleSearching extends TestCase {  
  11.   
  12.     MultipleSearching searcher;  
  13.       
  14.     @Override  
  15.     protected void setUp() throws Exception {  
  16.         String indexRoot = "E:\\Store\\indexes";  
  17.         Analyzer a = new SmartChineseAnalyzer(Version.LUCENE_35, true);  
  18.         IndexWriterConfig indexConfig = new IndexWriterConfig(Version.LUCENE_35, a);  
  19.         searcher = new MultipleSearching(indexRoot, indexConfig);  
  20.     }  
  21.       
  22.     @Override  
  23.     protected void tearDown() throws Exception {  
  24.         searcher.close();  
  25.     }  
  26.       
  27.     public void testSearching() {  
  28.         searcher.search("+title:拉斯维加斯^1.25 (+content:美国^1.50 +content:拉斯维加斯)");  
  29.         searcher.iterateDocs(010);  
  30.         searcher.explain(05);  
  31.     }  
  32. }  
搜索结果,迭代出来的文档数据信息,如下所示:
[plain] view plain copy
  1. 2011-12-13 12:12:55 org.shirdrn.lucene.MultipleSearching iterateDocs  
  2. 信息: Document<stored,indexed,tokenized,omitNorms<title:全新体验 拉斯维加斯的完美24小时(组图)(4)_新浪旅游_新浪网> stored,indexed,omitNorms<url:http://travel.sina.com.cn/world/2010-08-16/1400141443_4.shtml> stored,indexed,omitNorms<path:x>>  
  3. 2011-12-13 12:12:55 org.shirdrn.lucene.MultipleSearching iterateDocs  
  4. 信息: Document<stored,indexed,tokenized,omitNorms<title:拉斯维加斯 触摸你的奢侈底线(组图)_新浪旅游_新浪网> stored,indexed,omitNorms<url:http://travel.sina.com.cn/world/2009-05-21/095684952.shtml> stored,indexed,omitNorms<path:v>>  
  5. 2011-12-13 12:12:55 org.shirdrn.lucene.MultipleSearching iterateDocs  
  6. 信息: Document<stored,indexed,tokenized,omitNorms<title:美国拉斯维加斯地图_新浪旅游_新浪网> stored,indexed,omitNorms<url:http://travel.sina.com.cn/world/2008-08-20/113317460.shtml> stored,indexed,omitNorms<path:a>>  
  7. 2011-12-13 12:12:55 org.shirdrn.lucene.MultipleSearching iterateDocs  
  8. 信息: Document<stored,indexed,tokenized,omitNorms<title:美国拉斯维加斯:潮野水上乐园_新浪旅游_新浪网> stored,indexed,omitNorms<url:http://travel.sina.com.cn/world/2008-08-20/093217358.shtml> stored,indexed,omitNorms<path:e>>  
  9. 2011-12-13 12:12:55 org.shirdrn.lucene.MultipleSearching iterateDocs  
  10. 信息: Document<stored,indexed,tokenized,omitNorms<title:美国拉斯维加斯:米高梅历险游乐园_新浪旅游_新浪网> stored,indexed,omitNorms<url:http://travel.sina.com.cn/world/2008-08-20/095617381.shtml> stored,indexed,omitNorms<path:k>>  
  11. 2011-12-13 12:12:55 org.shirdrn.lucene.MultipleSearching iterateDocs  
  12. 信息: Document<stored,indexed,tokenized,omitNorms<title:美国拉斯维加斯主要景点_新浪旅游_新浪网> stored,indexed,omitNorms<url:http://travel.sina.com.cn/world/2008-08-20/114817479.shtml> stored,indexed,omitNorms<path:m>>  
  13. 2011-12-13 12:12:55 org.shirdrn.lucene.MultipleSearching iterateDocs  
  14. 信息: Document<stored,indexed,tokenized,omitNorms<title:娱乐之都拉斯维加斯宣布在中国推旅游市场新战略_新浪旅游_新浪网> stored,indexed,omitNorms<url:http://travel.sina.com.cn/news/2008-11-19/094337435.shtml> stored,indexed,omitNorms<path:j>>  
  15. 2011-12-13 12:12:55 org.shirdrn.lucene.MultipleSearching iterateDocs  
  16. 信息: Document<stored,indexed,tokenized,omitNorms<title:美国拉斯维加斯简介_新浪旅游_新浪网> stored,indexed,omitNorms<url:http://travel.sina.com.cn/world/2008-08-19/160017116.shtml> stored,indexed,omitNorms<path:v>>  
  17. 2011-12-13 12:12:55 org.shirdrn.lucene.MultipleSearching iterateDocs  
  18. 信息: Document<stored,indexed,tokenized,omitNorms<title:拉斯维加斯“猫王模仿秀”亮相国际旅交会_新浪旅游_新浪网> stored,indexed,omitNorms<url:http://travel.sina.com.cn/news/2009-11-23/1004116788.shtml> stored,indexed,omitNorms<path:j>>  
  19. 2011-12-13 12:12:55 org.shirdrn.lucene.MultipleSearching iterateDocs  
  20. 信息: Document<stored,indexed,tokenized,omitNorms<title:10大美食家的饕餮名城:拉斯维加斯(图)(4)_新浪旅游_新浪网> stored,indexed,omitNorms<url:http://travel.sina.com.cn/food/2009-01-16/090855088.shtml> stored,indexed,omitNorms<path:s>>  

通过path可以看到,搜索结果是将多个索引目录下的结果进行了聚合。

下面是搜索结果相关度得分情况,如下所示:

[plain] view plain copy
  1. 7.3240967 = (MATCH) sum of:  
  2.   6.216492 = (MATCH) weight(title:拉斯维加斯^1.25 in 616), product of:  
  3.     0.7747233 = queryWeight(title:拉斯维加斯^1.25), product of:  
  4.       1.25 = boost  
  5.       8.024145 = idf(docFreq=82, maxDocs=93245)  
  6.       0.07723921 = queryNorm  
  7.     8.024145 = (MATCH) fieldWeight(title:拉斯维加斯 in 616), product of:  
  8.       1.0 = tf(termFreq(title:拉斯维加斯)=1)  
  9.       8.024145 = idf(docFreq=82, maxDocs=93245)  
  10.       1.0 = fieldNorm(field=title, doc=616)  
  11.   1.1076047 = (MATCH) sum of:  
  12.     0.24895692 = (MATCH) weight(content:美国^1.5 in 616), product of:  
  13.       0.39020002 = queryWeight(content:美国^1.5), product of:  
  14.         1.5 = boost  
  15.         3.3678925 = idf(docFreq=8734, maxDocs=93245)  
  16.         0.07723921 = queryNorm  
  17.       0.63802385 = (MATCH) fieldWeight(content:美国 in 616), product of:  
  18.         1.7320508 = tf(termFreq(content:美国)=3)  
  19.         3.3678925 = idf(docFreq=8734, maxDocs=93245)  
  20.         0.109375 = fieldNorm(field=content, doc=616)  
  21.     0.8586478 = (MATCH) weight(content:拉斯维加斯 in 616), product of:  
  22.       0.49754182 = queryWeight(content:拉斯维加斯), product of:  
  23.         6.4415708 = idf(docFreq=403, maxDocs=93245)  
  24.         0.07723921 = queryNorm  
  25.       1.7257802 = (MATCH) fieldWeight(content:拉斯维加斯 in 616), product of:  
  26.         2.4494898 = tf(termFreq(content:拉斯维加斯)=6)  
  27.         6.4415708 = idf(docFreq=403, maxDocs=93245)  
  28.         0.109375 = fieldNorm(field=content, doc=616)  
  29.   
  30. 7.2405667 = (MATCH) sum of:  
  31.   6.216492 = (MATCH) weight(title:拉斯维加斯^1.25 in 2850), product of:  
  32.     0.7747233 = queryWeight(title:拉斯维加斯^1.25), product of:  
  33.       1.25 = boost  
  34.       8.024145 = idf(docFreq=82, maxDocs=93245)  
  35.       0.07723921 = queryNorm  
  36.     8.024145 = (MATCH) fieldWeight(title:拉斯维加斯 in 2850), product of:  
  37.       1.0 = tf(termFreq(title:拉斯维加斯)=1)  
  38.       8.024145 = idf(docFreq=82, maxDocs=93245)  
  39.       1.0 = fieldNorm(field=title, doc=2850)  
  40.   1.0240744 = (MATCH) sum of:  
  41.     0.17423354 = (MATCH) weight(content:美国^1.5 in 2850), product of:  
  42.       0.39020002 = queryWeight(content:美国^1.5), product of:  
  43.         1.5 = boost  
  44.         3.3678925 = idf(docFreq=8734, maxDocs=93245)  
  45.         0.07723921 = queryNorm  
  46.       0.44652367 = (MATCH) fieldWeight(content:美国 in 2850), product of:  
  47.         1.4142135 = tf(termFreq(content:美国)=2)  
  48.         3.3678925 = idf(docFreq=8734, maxDocs=93245)  
  49.         0.09375 = fieldNorm(field=content, doc=2850)  
  50.     0.8498409 = (MATCH) weight(content:拉斯维加斯 in 2850), product of:  
  51.       0.49754182 = queryWeight(content:拉斯维加斯), product of:  
  52.         6.4415708 = idf(docFreq=403, maxDocs=93245)  
  53.         0.07723921 = queryNorm  
  54.       1.7080793 = (MATCH) fieldWeight(content:拉斯维加斯 in 2850), product of:  
  55.         2.828427 = tf(termFreq(content:拉斯维加斯)=8)  
  56.         6.4415708 = idf(docFreq=403, maxDocs=93245)  
  57.         0.09375 = fieldNorm(field=content, doc=2850)  
  58.   
  59. 7.1128473 = (MATCH) sum of:  
  60.   6.216492 = (MATCH) weight(title:拉斯维加斯^1.25 in 63), product of:  
  61.     0.7747233 = queryWeight(title:拉斯维加斯^1.25), product of:  
  62.       1.25 = boost  
  63.       8.024145 = idf(docFreq=82, maxDocs=93245)  
  64.       0.07723921 = queryNorm  
  65.     8.024145 = (MATCH) fieldWeight(title:拉斯维加斯 in 63), product of:  
  66.       1.0 = tf(termFreq(title:拉斯维加斯)=1)  
  67.       8.024145 = idf(docFreq=82, maxDocs=93245)  
  68.       1.0 = fieldNorm(field=title, doc=63)  
  69.   0.896355 = (MATCH) sum of:  
  70.     0.1451946 = (MATCH) weight(content:美国^1.5 in 63), product of:  
  71.       0.39020002 = queryWeight(content:美国^1.5), product of:  
  72.         1.5 = boost  
  73.         3.3678925 = idf(docFreq=8734, maxDocs=93245)  
  74.         0.07723921 = queryNorm  
  75.       0.37210304 = (MATCH) fieldWeight(content:美国 in 63), product of:  
  76.         1.4142135 = tf(termFreq(content:美国)=2)  
  77.         3.3678925 = idf(docFreq=8734, maxDocs=93245)  
  78.         0.078125 = fieldNorm(field=content, doc=63)  
  79.     0.7511604 = (MATCH) weight(content:拉斯维加斯 in 63), product of:  
  80.       0.49754182 = queryWeight(content:拉斯维加斯), product of:  
  81.         6.4415708 = idf(docFreq=403, maxDocs=93245)  
  82.         0.07723921 = queryNorm  
  83.       1.5097432 = (MATCH) fieldWeight(content:拉斯维加斯 in 63), product of:  
  84.         3.0 = tf(termFreq(content:拉斯维加斯)=9)  
  85.         6.4415708 = idf(docFreq=403, maxDocs=93245)  
  86.         0.078125 = fieldNorm(field=content, doc=63)  
  87.   
  88. 7.1128473 = (MATCH) sum of:  
  89.   6.216492 = (MATCH) weight(title:拉斯维加斯^1.25 in 2910), product of:  
  90.     0.7747233 = queryWeight(title:拉斯维加斯^1.25), product of:  
  91.       1.25 = boost  
  92.       8.024145 = idf(docFreq=82, maxDocs=93245)  
  93.       0.07723921 = queryNorm  
  94.     8.024145 = (MATCH) fieldWeight(title:拉斯维加斯 in 2910), product of:  
  95.       1.0 = tf(termFreq(title:拉斯维加斯)=1)  
  96.       8.024145 = idf(docFreq=82, maxDocs=93245)  
  97.       1.0 = fieldNorm(field=title, doc=2910)  
  98.   0.896355 = (MATCH) sum of:  
  99.     0.1451946 = (MATCH) weight(content:美国^1.5 in 2910), product of:  
  100.       0.39020002 = queryWeight(content:美国^1.5), product of:  
  101.         1.5 = boost  
  102.         3.3678925 = idf(docFreq=8734, maxDocs=93245)  
  103.         0.07723921 = queryNorm  
  104.       0.37210304 = (MATCH) fieldWeight(content:美国 in 2910), product of:  
  105.         1.4142135 = tf(termFreq(content:美国)=2)  
  106.         3.3678925 = idf(docFreq=8734, maxDocs=93245)  
  107.         0.078125 = fieldNorm(field=content, doc=2910)  
  108.     0.7511604 = (MATCH) weight(content:拉斯维加斯 in 2910), product of:  
  109.       0.49754182 = queryWeight(content:拉斯维加斯), product of:  
  110.         6.4415708 = idf(docFreq=403, maxDocs=93245)  
  111.         0.07723921 = queryNorm  
  112.       1.5097432 = (MATCH) fieldWeight(content:拉斯维加斯 in 2910), product of:  
  113.         3.0 = tf(termFreq(content:拉斯维加斯)=9)  
  114.         6.4415708 = idf(docFreq=403, maxDocs=93245)  
  115.         0.078125 = fieldNorm(field=content, doc=2910)  
  116.   
  117. 7.1128473 = (MATCH) sum of:  
  118.   6.216492 = (MATCH) weight(title:拉斯维加斯^1.25 in 2920), product of:  
  119.     0.7747233 = queryWeight(title:拉斯维加斯^1.25), product of:  
  120.       1.25 = boost  
  121.       8.024145 = idf(docFreq=82, maxDocs=93245)  
  122.       0.07723921 = queryNorm  
  123.     8.024145 = (MATCH) fieldWeight(title:拉斯维加斯 in 2920), product of:  
  124.       1.0 = tf(termFreq(title:拉斯维加斯)=1)  
  125.       8.024145 = idf(docFreq=82, maxDocs=93245)  
  126.       1.0 = fieldNorm(field=title, doc=2920)  
  127.   0.896355 = (MATCH) sum of:  
  128.     0.1451946 = (MATCH) weight(content:美国^1.5 in 2920), product of:  
  129.       0.39020002 = queryWeight(content:美国^1.5), product of:  
  130.         1.5 = boost  
  131.         3.3678925 = idf(docFreq=8734, maxDocs=93245)  
  132.         0.07723921 = queryNorm  
  133.       0.37210304 = (MATCH) fieldWeight(content:美国 in 2920), product of:  
  134.         1.4142135 = tf(termFreq(content:美国)=2)  
  135.         3.3678925 = idf(docFreq=8734, maxDocs=93245)  
  136.         0.078125 = fieldNorm(field=content, doc=2920)  
  137.     0.7511604 = (MATCH) weight(content:拉斯维加斯 in 2920), product of:  
  138.       0.49754182 = queryWeight(content:拉斯维加斯), product of:  
  139.         6.4415708 = idf(docFreq=403, maxDocs=93245)  
  140.         0.07723921 = queryNorm  
  141.       1.5097432 = (MATCH) fieldWeight(content:拉斯维加斯 in 2920), product of:  
  142.         3.0 = tf(termFreq(content:拉斯维加斯)=9)  
  143.         6.4415708 = idf(docFreq=403, maxDocs=93245)  
  144.         0.078125 = fieldNorm(field=content, doc=2920)  

可见,搜索结果相关度得分,是基于全部的多个索引来计算的(maxDocs=93245)。


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值