lucene 3.4 contrib/facet 切面搜索

  categories:搜索资料  author:
来源:互联网
solr  有facet  search  ,BOBO也有;现在lucene3.4之后也有了,这个是贡献版本,在apache 官方的包里面有提供,这种功能对于分组统计和类别统计是一个很好的帮手;

有了这个就不用羡慕solr了,不是我抗拒solr,只是像我们公司有时间让我们开发的情况下,我更偏向于底层点的api开发,lucene更得心应手。

再说现在的solr没有近实时搜索,听说要4.0后有。

废话不说,直接上代码

 

  1. public class Indexer {
  2. //需要索引的信息
  3. public static String[] docTitles = {
  4. “white car”,
  5. “white dog”,
  6. };
  7. public static String[] docTexts = {
  8. “the white car is the one I want.”,
  9. “the white dog does not belong to anyone.”,
  10. };
  11. //分的类别
  12. public static CategoryPath[] categories = {
  13. new CategoryPath(“root”,”a”,”f1″), new CategoryPath(“root”,”a”,”f2″)
  14. };
  15. public static void index (Directory indexDir, Directory taxoDir) throws Exception {
  16. // 创建一个普通的indexwriter
  17. IndexWriter iw = new IndexWriter(indexDir, new IndexWriterConfig(ExampleUtils.EXAMPLE_VER, SimpleUtils.analyzer));
  18. //创建一个 taxonomy writer
  19. TaxonomyWriter taxo = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
  20. int nDocsAdded = 0;
  21. int nFacetsAdded = 0;
  22. for (int docNum=0; docNum<docTexts.length; docNum++)
  23. {
  24. // 准备当前的切面
  25. List<CategoryPath> facetList = SimpleUtils.categoryPathArrayToList(categories[0]);
  26. //
  27. CategoryDocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxo).setCategoryPaths(facetList);
  28. // 创建document
  29. Document doc = new Document();
  30. doc.add(new Field(SimpleUtils.TITLE, docTitles[docNum], Store.YES, Index.ANALYZED));
  31. doc.add(new Field(SimpleUtils.TEXT, docTexts[docNum], Store.NO, Index.ANALYZED));
  32. // 把切面的索引信息添加到document
  33. categoryDocBuilder.build(doc);
  34. // 最终写入索引
  35. iw.addDocument(doc);
  36. nDocsAdded ++;
  37. nFacetsAdded += facetList.size();
  38. }
  39. // commit changes.
  40. // we commit changes to the taxonomy index prior to committing them to the search index.
  41. // this is important, so that all facets referred to by documents in the search index
  42. // will indeed exist in the taxonomy index.
  43. taxo.commit();
  44. iw.commit();
  45. // close the taxonomy index and the index – all modifications are
  46. // now safely in the provided directories: indexDir and taxoDir.
  47. taxo.close();
  48. iw.close();
  49. System.out.println(“Indexed “+nDocsAdded+” documents with overall “+nFacetsAdded+” facets.”);
  50. }
  51. public static void main(String[] args){
  52. String indexp = “D:/work/data/index/facet_index/n_index”;
  53. String indexp_t = “D:/work/data/index/facet_index/t_index”;
  54. try {
  55. Directory  directory =  FSDirectory.open(new File(indexp));
  56. Directory  directory_t =  FSDirectory.open(new File(indexp_t));
  57. new Indexer().index(directory, directory_t);
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  1. public class Searcher {
  2. public void query(Directory indexDir, Directory taxoDir) throws CorruptIndexException, IOException
  3. {
  4. Query q = new TermQuery(new Term(SimpleUtils.TEXT, “white”));
  5. IndexReader indexReader = IndexReader.open(indexDir, true);
  6. IndexSearcher searcher = new IndexSearcher(indexReader);
  7. TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
  8. TopScoreDocCollector topDocsCollector = TopScoreDocCollector.create(10, true);
  9. FacetIndexingParams     indexingParams = new DefaultFacetIndexingParams();
  10. FacetSearchParams facetSearchParams = new FacetSearchParams(indexingParams);
  11. facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath(“root”,”a”), 10));
  12. FacetsCollector facetsCollector = new FacetsCollector(facetSearchParams, indexReader, taxoReader);
  13. // perform documents search and facets accumulation
  14. searcher.search(q, MultiCollector.wrap(topDocsCollector, facetsCollector));
  15. // Obtain facets results and print them
  16. List<FacetResult> res = facetsCollector.getFacetResults();
  17. System.out.println(res.size());
  18. int i = 0;
  19. for (FacetResult facetResult : res) {
  20. System.out.println(“Res ” + (i++) + “: ” + facetResult);
  21. System.out.println(“————————————-“);
  22. System.out.println( facetResult.getFacetResultNode().getNumSubResults());
  23. System.out.println( facetResult.getFacetResultNode().getOrdinal());
  24. System.out.println( facetResult.getFacetResultNode().getValue());
  25. System.out.println( facetResult.getFacetResultNode().getResidue());
  26. }
  27. }
  28. public static void main(String[] args) {
  29. String indexp = “D:/work/data/index/facet_index/n_index”;
  30. String indexp_t = “D:/work/data/index/facet_index/t_index”;
  31. try {
  32. Directory  directory =  FSDirectory.open(new File(indexp));
  33. Directory  directory_t =  FSDirectory.open(new File(indexp_t));
  34. new Searcher().query(directory, directory_t);
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }


快乐成长 每天进步一点点