zoukankan      html  css  js  c++  java
  • lucene 4.0

    package com.fox.facet;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    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.Store;
    import org.apache.lucene.document.StringField;
    import org.apache.lucene.facet.index.CategoryDocumentBuilder;
    import org.apache.lucene.facet.search.FacetsCollector;
    import org.apache.lucene.facet.search.params.CountFacetRequest;
    import org.apache.lucene.facet.search.params.FacetSearchParams;
    import org.apache.lucene.facet.search.results.FacetResult;
    import org.apache.lucene.facet.search.results.FacetResultNode;
    import org.apache.lucene.facet.taxonomy.CategoryPath;
    import org.apache.lucene.facet.taxonomy.TaxonomyReader;
    import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
    import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
    import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
    import org.apache.lucene.index.DirectoryReader;
    import org.apache.lucene.index.IndexReader;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.index.IndexWriterConfig.OpenMode;
    import org.apache.lucene.index.Term;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.MultiCollector;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.TermQuery;
    import org.apache.lucene.search.TopScoreDocCollector;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    
    public class FacetingExample {
        private static final String INDEX = "d:/facet/index";
        private static final String INDEX_TAXO = "d:/facet/taxo";
    
        public static void main(final String[] args) throws IOException {
            Directory dir = FSDirectory.open(new File(INDEX));
            Directory taxoDir = FSDirectory.open(new File(INDEX_TAXO));
            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
            IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40, analyzer);
            iwc.setOpenMode(OpenMode.CREATE);
            IndexWriter writer = new IndexWriter(dir, iwc);
            TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE_OR_APPEND);
    
            List<Book> books = Arrays.asList(new Book("Tom Sawyer", "Mark Twain", "1840", "Novel"), new Book("Collected Tales",
                    "Mark Twain", "1850", "Novel"), new Book("The Trial", "Franz Kafka", "1901", "Novel"), new Book("Some book",
                    "Some author", "1901", "Novel"));
    
            createDocuments(writer, taxoWriter, books);
            taxoWriter.commit();
            writer.commit();
            writer.close();
            taxoWriter.close();
    
            IndexReader indexReader = DirectoryReader.open(dir);
            IndexSearcher searcher = new IndexSearcher(indexReader);
            TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
            Query q = new TermQuery(new Term("category", "Novel"));
            TopScoreDocCollector tdc = TopScoreDocCollector.create(10, true);
            FacetSearchParams facetSearchParams = new FacetSearchParams();
            facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("author"), 10));
            facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("category"), 10));
            facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("published"), 10));
            FacetsCollector facetsCollector = new FacetsCollector(facetSearchParams, indexReader, taxoReader);
            searcher.search(q, MultiCollector.wrap(tdc, facetsCollector));
            List<FacetResult> res = facetsCollector.getFacetResults();
            System.out.println("Search for books with the category:Novel returned : " + res.size()
                    + " results
    ---------------------------------");
            for (final FacetResult r : res) {
                System.out.println("
    Matching " + r.getFacetResultNode().getLabel() + ":
    ------------------------------------");
                for (FacetResultNode n : r.getFacetResultNode().getSubResults()) {
                    System.out.println(String.format("	%s: %.0f", n.getLabel().lastComponent(), n.getValue()));
                }
            }
        }
    
        private static void createDocuments(final IndexWriter writer, final TaxonomyWriter taxoWriter, final List<Book> books)
                throws IOException {
            for (final Book b : books) {
                Document doc = new Document();
                doc.add(new StringField("title", b.getTitle(), Store.YES));
                doc.add(new StringField("category", b.getCategory(), Store.YES));
                List<CategoryPath> categories = new ArrayList<CategoryPath>();
                categories.add(new CategoryPath("author", b.getAuthor()));
                categories.add(new CategoryPath("category", b.getCategory()));
                categories.add(new CategoryPath("published", b.getPublished()));
                CategoryDocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxoWriter);
                categoryDocBuilder.setCategoryPaths(categories);
                categoryDocBuilder.build(doc);
                writer.addDocument(doc);
            }
        }
    }
    
    class Book {
        private final String title;
        private final String author;
        private final String published;
        private final String category;
    
        public Book(final String title, final String author, final String published, final String category) {
            this.title = title;
            this.author = author;
            this.published = published;
            this.category = category;
        }
    
        public String getTitle() {
            return title;
        }
    
        public String getAuthor() {
            return author;
        }
    
        public String getPublished() {
            return published;
        }
    
        public String getCategory() {
            return category;
        }
    }

    Result

    Search for books with the category:Novel returned : 3 results
    ---------------------------------
    
    Matching author:
    ------------------------------------
        Mark Twain: 2
        Some author: 1
        Franz Kafka: 1
    
    Matching category:
    ------------------------------------
        Novel: 4
    
    Matching published:
    ------------------------------------
        1901: 2
        1850: 1
        1840: 1
  • 相关阅读:
    diy_markdown 的 html 显示
    根据 vuex 的 this.$store.dispatch() 返回值 处理逻辑
    vue 项目配置: 局域网 ip 发布
    vue-markdown 之 markdown-it, 以及 table of content 的实现:markdown-it-toc-and-anchor
    程序员面试金典-面试题 08.05. 递归乘法
    程序员面试金典-面试题 08.04. 幂集
    程序员面试金典-面试题 08.03. 魔术索引
    程序员面试金典-面试题 08.02. 迷路的机器人
    程序员面试金典-面试题 08.01. 三步问题
    程序员面试金典-面试题 05.08. 绘制直线
  • 原文地址:https://www.cnblogs.com/huangfox/p/4177750.html
Copyright © 2011-2022 走看看