Example usage for org.apache.lucene.index DirectoryReader open

List of usage examples for org.apache.lucene.index DirectoryReader open

Introduction

In this page you can find the example usage for org.apache.lucene.index DirectoryReader open.

Prototype

public static DirectoryReader open(final IndexCommit commit) throws IOException 

Source Link

Document

Expert: returns an IndexReader reading the index in the given IndexCommit .

Usage

From source file:com.czw.search.lucene.example.facet.SimpleFacetsExample.java

License:Apache License

/**
 * User drills down on 'Publish Date/2012', and we
 * return facets for both 'Publish Date' and 'Author',
 * using DrillSideways.// w ww.  ja  v a2s  . c  o m
 */
private List<FacetResult> drillSideways() throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

    // Passing no baseQuery means we drill down on all
    // documents ("browse only"):
    DrillDownQuery q = new DrillDownQuery(config);

    // Now user drills down on Publish Date/2010:
    q.add("Publish Date", "2010");

    DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
    DrillSidewaysResult result = ds.search(q, 10);

    // Retrieve results
    List<FacetResult> facets = result.facets.getAllDims(10);
    System.out.println("totalHits:" + result.hits.totalHits);
    ScoreDoc[] docs = result.hits.scoreDocs;
    System.out.println(searcher.doc(docs[0].doc));

    indexReader.close();
    taxoReader.close();

    return facets;
}

From source file:com.czw.search.lucene.example.facet.SimpleSortedSetFacetsExample.java

License:Apache License

/**
 * User runs a query and counts facets.//  ww  w.j  av a2s  . co  m
 */
private List<FacetResult> search() throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader);

    // Aggregatses the facet counts
    FacetsCollector fc = new FacetsCollector();

    // MatchAllDocsQuery is for "browsing" (counts facets
    // for all non-deleted docs in the index); normally
    // you'd use a "normal" query:
    FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);

    // Retrieve results
    Facets facets = new SortedSetDocValuesFacetCounts(state, fc);

    List<FacetResult> results = new ArrayList<>();
    results.add(facets.getTopChildren(10, "Author"));
    results.add(facets.getTopChildren(10, "Publish Year"));
    indexReader.close();

    return results;
}

From source file:com.czw.search.lucene.example.facet.SimpleSortedSetFacetsExample.java

License:Apache License

/**
 * User drills down on 'Publish Year/2010'.
 *//*w  w w . ja va  2 s .c  o m*/
private FacetResult drillDown() throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader);

    // Now user drills down on Publish Year/2010:
    DrillDownQuery q = new DrillDownQuery(config);
    q.add("Publish Year", "2010");
    FacetsCollector fc = new FacetsCollector();
    FacetsCollector.search(searcher, q, 10, fc);

    // Retrieve results
    Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
    FacetResult result = facets.getTopChildren(10, "Author");
    indexReader.close();

    return result;
}

From source file:com.czw.search.lucene.example.xmlparser.FormBasedXmlQueryDemo.java

License:Apache License

private void openExampleIndex() throws IOException {
    //Create a RAM-based index from our test data file
    RAMDirectory rd = new RAMDirectory();
    IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
    IndexWriter writer = new IndexWriter(rd, iwConfig);
    InputStream dataIn = getServletContext().getResourceAsStream("/WEB-INF/data.tsv");
    BufferedReader br = new BufferedReader(new InputStreamReader(dataIn, StandardCharsets.UTF_8));
    String line = br.readLine();/*from w  w  w  .  ja  v a  2 s  .c o  m*/
    final FieldType textNoNorms = new FieldType(TextField.TYPE_STORED);
    textNoNorms.setOmitNorms(true);
    while (line != null) {
        line = line.trim();
        if (line.length() > 0) {
            //parse row and create a document
            StringTokenizer st = new StringTokenizer(line, "\t");
            Document doc = new Document();
            doc.add(new Field("location", st.nextToken(), textNoNorms));
            doc.add(new Field("salary", st.nextToken(), textNoNorms));
            doc.add(new Field("type", st.nextToken(), textNoNorms));
            doc.add(new Field("description", st.nextToken(), textNoNorms));
            writer.addDocument(doc);
        }
        line = br.readLine();
    }
    writer.close();

    //open searcher
    // this example never closes it reader!
    IndexReader reader = DirectoryReader.open(rd);
    searcher = new IndexSearcher(reader);
}

From source file:com.daniel.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.che.org/java/4_0/demo.html for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);//from w  w w  . ja  va2 s  . c o m
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) {
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);

    BufferedReader in = null;
    if (queries != null) {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8"));
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    }
    QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer);
    while (true) {
        if (queries == null && queryString == null) { // prompt the user
            System.out.println("Enter query: ");
        }

        String line = queryString != null ? queryString : in.readLine();

        if (line == null || line.length() == -1) {
            break;
        }

        line = line.trim();
        if (line.length() == 0) {
            break;
        }

        Query query = parser.parse(line);
        System.out.println("Searching for: " + query.toString(field));

        if (repeat > 0) { // repeat & time as benchmark
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                searcher.search(query, null, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }

        doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

        if (queryString != null) {
            break;
        }
    }
    reader.close();
}

From source file:com.devb.search.IndicSearcher.java

License:Apache License

private void callSearch(boolean j) {
    System.out.println("Servlet Ctx " + servletContext.getRealPath("/"));
    String indexPath = servletContext.getRealPath("/") + "/hindex/";
    String docsPath = servletContext.getRealPath("/") + "/hdocs/";

    final File docDir = new File(docsPath);
    if (!docDir.exists() || !docDir.canRead()) {
        System.out.println("Document directory '" + docDir.getAbsolutePath()
                + "' does not exist or is not readable, " + "please check the path\n");
        return;//from  w  ww . j  a  v a2 s  .  co m
    }

    IndexReader reader = null;
    IndexSearcher searcher = null;
    Analyzer analyzer = null;
    String field = "contents";

    try {
        reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));
        searcher = new IndexSearcher(reader);
        analyzer = new HindiAnalyzer();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    QueryParser parser = new QueryParser(field, analyzer);
    String /*ByteBuffer*/ line = null;
    Query query = null;

    try {
        // line = Charset.forName("UTF-8").encode(this.id);
        line = this.id;

        // line = line.trim();
        if (line == null) {
            return;
        }
        System.out.println("Hindi StandardSearcher / callSearch Line " + line);
        query = parser.parse(line);
        System.out.println("Hindi StandardSearcher / callSearch Hindi Query " + query);
        final int maxHits = 10;
        ScoreDoc[] hits = searcher.search(query, null, maxHits).scoreDocs;
        try {
            // Iterate through the results:
            for (int i = 0; i < hits.length; i++) {
                Document hitDoc = searcher.doc(hits[i].doc);

                if (j) {
                    JSONObject jo = new JSONObject();
                    jo.put("query", query.toString(field));
                    jo.put("path", hitDoc.get("path"));
                    jo.put("line", hitDoc.get("linenumber"));
                    jo.put("contents", hitDoc.get("contents"));

                    ja.put(jo);
                } else {
                    SearchResult ns = new SearchResult();
                    ns.setQuery(query.toString(field));
                    ns.setDocPath(hitDoc.get("path"));
                    ns.setLineNum(hitDoc.get("linenumber"));
                    ns.setContents(hitDoc.get("contents"));
                    contentProvider.put(String.valueOf(i), ns);
                }
            }
        } catch (Exception ito) {
            ito.printStackTrace();
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    try {
        reader.close();
    } catch (IOException ioe) {

    }
}

From source file:com.devb.search.StandardSearcher.java

License:Apache License

private void callSearch(boolean j) {
    System.out.println("Servlet Ctx " + servletContext.getRealPath("/"));
    String indexPath = servletContext.getRealPath("/") + "/index/";
    String docsPath = servletContext.getRealPath("/") + "/docs/";

    final File docDir = new File(docsPath);
    if (!docDir.exists() || !docDir.canRead()) {
        System.out.println("Document directory '" + docDir.getAbsolutePath()
                + "' does not exist or is not readable, " + "please check the path\n");
        return;/* w  ww  .j  a v a 2  s.c  o m*/
    }

    IndexReader reader = null;
    IndexSearcher searcher = null;
    Analyzer analyzer = null;
    String field = "contents";

    try {
        reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));
        searcher = new IndexSearcher(reader);
        analyzer = new StandardAnalyzer();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    QueryParser parser = new QueryParser(field, analyzer);
    String line = null;
    Query query = null;

    try {
        line = this.id;

        line = line.trim();
        if (line.length() == 0) {
            return;
        }
        query = parser.parse(line);
        final int maxHits = 10;
        ScoreDoc[] hits = searcher.search(query, null, maxHits).scoreDocs;
        try {
            // Iterate through the results:
            for (int i = 0; i < hits.length; i++) {
                Document hitDoc = searcher.doc(hits[i].doc);

                if (j) {
                    JSONObject jo = new JSONObject();
                    jo.put("query", query.toString(field));
                    jo.put("path", hitDoc.get("path"));
                    jo.put("line", hitDoc.get("linenumber"));
                    jo.put("contents", hitDoc.get("contents"));

                    ja.put(jo);
                } else {
                    SearchResult ns = new SearchResult();
                    ns.setQuery(query.toString(field));
                    ns.setDocPath(hitDoc.get("path"));
                    ns.setLineNum(hitDoc.get("linenumber"));
                    ns.setContents(hitDoc.get("contents"));
                    contents.put(String.valueOf(i), ns);
                }
            }
        } catch (Exception ito) {
            ito.printStackTrace();
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    try {
        reader.close();
    } catch (IOException ioe) {

    }
}

From source file:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java

License:Open Source License

@Override
public synchronized boolean isEmpty() {
    boolean empty;
    try {/*  ww w. j a v a 2s .co m*/
        Directory directory = FSDirectory.open(indexDir);
        IndexReader indexReader = DirectoryReader.open(directory);
        empty = indexReader.numDocs() <= 1;
        indexReader.close();
        directory.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return empty;
}

From source file:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java

License:Open Source License

protected synchronized int getDocNum(T object) {
    int docNum;//from   w w  w  .  ja va  2  s.  co m
    String uniqueIndexFieldName = getUniqueIndexFieldName();
    String uniqueIndexFieldValue = getUniqueIndexFieldValue(object);
    if (uniqueIndexFieldValue != null) {
        String query = uniqueIndexFieldName + ":" + uniqueIndexFieldValue;
        try {
            Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH);
            QueryParser multiFielsQP = new QueryParser(Version.LUCENE_44, uniqueIndexFieldName, analyzer);
            Query luceneQuery = multiFielsQP.parse(query);

            Directory directory = FSDirectory.open(indexDir);
            IndexReader reader = DirectoryReader.open(directory);
            IndexSearcher indexSearcher = new IndexSearcher(reader);
            TopDocs topDocs = indexSearcher.search(luceneQuery, reader.maxDoc());
            if (topDocs.totalHits > 0) {
                docNum = topDocs.scoreDocs[0].doc;
            } else {
                docNum = -1;
            }
            //               indexSearcher.close();
            // TODO add finally
            reader.close();
            directory.close();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        } catch (CorruptIndexException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        docNum = -1;
    }
    return docNum;
}

From source file:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java

License:Open Source License

public String highlight(String strToHighlight, String fieldName, Query luceneQuery) {
    String highlightedText;//  ww  w. j  a v a 2 s. c o  m
    Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH);
    try {
        Directory directory = FSDirectory.open(indexDir);
        IndexReader indexReader = DirectoryReader.open(directory);
        Query rewrittenLuceneQuery = luceneQuery.rewrite(indexReader);
        QueryScorer luceneScorer = new QueryScorer(rewrittenLuceneQuery);
        SimpleHTMLFormatter luceneFormatter = new SimpleHTMLFormatter("<span class=\"hit\">", "</span>");
        Highlighter luceneHighlighter = new Highlighter(luceneFormatter, luceneScorer);

        Fragmenter luceneFragmenter;
        // Si la chaine  highlighter est sup  250 carac
        if (strToHighlight.length() > TAILLE_CHAINE_NON_FRAGMENTEE) {
            // Cration de best fragments de 100 carac chaque
            luceneFragmenter = new SimpleFragmenter(TAILLE_FRAGMENT);
        } else {
            // Toute la chaine est highlight
            luceneFragmenter = new SimpleFragmenter(Integer.MAX_VALUE);
        }
        luceneHighlighter.setTextFragmenter(luceneFragmenter);

        TokenStream luceneTokenStream = analyzer.tokenStream(fieldName, new StringReader(strToHighlight));
        String fragment = null;
        if (strToHighlight.length() > TAILLE_CHAINE_NON_FRAGMENTEE) {
            fragment = luceneHighlighter.getBestFragments(luceneTokenStream, strToHighlight, NB_BEST_FRAGMENT,
                    FRAGMENT_SEP);
        } else {
            fragment = luceneHighlighter.getBestFragment(luceneTokenStream, strToHighlight);
        }

        if (StringUtils.isBlank(fragment) && fieldName.equalsIgnoreCase("titre")) {
            fragment = strToHighlight;
        }
        indexReader.close();
        directory.close();

        highlightedText = fragment;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InvalidTokenOffsetsException e) {
        throw new RuntimeException(e);
    }
    return highlightedText;
}