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.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

private MultiReader openMultiReader(SimpleFSDirectory[] indexes) throws IOException {
    IndexReader[] readers = new IndexReader[indexes.length];
    for (int i = 0; i < indexes.length; i++) {
        readers[i] = DirectoryReader.open(indexes[i]);
    }/*  w ww .  j  av  a  2s. co  m*/

    return new MultiReader(readers, true);
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

/**
 * Returns a {@code List} of chromosome IDs for a project, specified by ID, where variations exist and satisfy a
 * specified query//from w w w .jav  a  2 s.c o m
 *
 * @param projectId an ID of a project, which index to query
 * @param query     a query to filter variations
 * @return a {@code List} of chromosome IDs
 * @throws IOException
 */
public List<Long> getChromosomeIdsWhereVariationsPresentFacet(long projectId, Query query) throws IOException {
    List<Long> chromosomeIds = new ArrayList<>();

    try (Directory index = fileManager.getIndexForProject(projectId);
            IndexReader reader = DirectoryReader.open(index)) {
        if (reader.numDocs() == 0) {
            return Collections.emptyList();
        }

        FacetsCollector facetsCollector = new FacetsCollector();
        IndexSearcher searcher = new IndexSearcher(reader);
        searcher.search(query, facetsCollector);

        Facets facets = new SortedSetDocValuesFacetCounts(new DefaultSortedSetDocValuesReaderState(reader,
                FeatureIndexFields.FACET_CHR_ID.getFieldName()), facetsCollector);
        FacetResult res = facets.getTopChildren(FACET_LIMIT, FeatureIndexFields.CHR_ID.getFieldName());
        if (res == null) {
            return Collections.emptyList();
        }

        for (LabelAndValue labelAndValue : res.labelValues) {
            chromosomeIds.add(Long.parseLong(labelAndValue.label));
        }
    }

    return chromosomeIds;
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

/**
 * Searches gene IDs, affected by variations in specified VCF files in a specified project
 *
 * @param projectId an ID of a project to search genes
 * @param gene a prefix of a gene ID to search
 * @param vcfFileIds a {@code List} of IDs of VCF files in project to search for gene IDs
 * @return a {@code Set} of gene IDs, that are affected by some variations in specified VCf files
 * @throws IOException//from www . j  av  a2s .com
 */
public Set<String> searchGenesInVcfFilesInProject(long projectId, String gene, List<Long> vcfFileIds)
        throws IOException {
    if (vcfFileIds == null || vcfFileIds.isEmpty()) {
        return Collections.emptySet();
    }

    BooleanQuery.Builder builder = new BooleanQuery.Builder();

    PrefixQuery geneIdPrefixQuery = new PrefixQuery(
            new Term(FeatureIndexFields.GENE_ID.getFieldName(), gene.toLowerCase()));
    PrefixQuery geneNamePrefixQuery = new PrefixQuery(
            new Term(FeatureIndexFields.GENE_NAME.getFieldName(), gene.toLowerCase()));
    BooleanQuery.Builder geneIdOrNameQuery = new BooleanQuery.Builder();
    geneIdOrNameQuery.add(geneIdPrefixQuery, BooleanClause.Occur.SHOULD);
    geneIdOrNameQuery.add(geneNamePrefixQuery, BooleanClause.Occur.SHOULD);

    builder.add(geneIdOrNameQuery.build(), BooleanClause.Occur.MUST);

    List<Term> terms = vcfFileIds.stream()
            .map(vcfFileId -> new Term(FeatureIndexFields.FILE_ID.getFieldName(), vcfFileId.toString()))
            .collect(Collectors.toList());
    TermsQuery termsQuery = new TermsQuery(terms);
    builder.add(termsQuery, BooleanClause.Occur.MUST);
    BooleanQuery query = builder.build();

    Set<String> geneIds;

    try (Directory index = fileManager.getIndexForProject(projectId);
            IndexReader reader = DirectoryReader.open(index)) {
        if (reader.numDocs() == 0) {
            return Collections.emptySet();
        }

        IndexSearcher searcher = new IndexSearcher(reader);
        final TopDocs docs = searcher.search(query, reader.numDocs());
        final ScoreDoc[] hits = docs.scoreDocs;

        geneIds = fetchGeneIds(hits, searcher);
    } catch (IOException e) {
        LOGGER.error(MessageHelper.getMessage(MessagesConstants.ERROR_FEATURE_INDEX_SEARCH_FAILED), e);
        return Collections.emptySet();
    }

    return geneIds;
}

From source file:com.evoapps.lucene.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public ArrayList<Publication> search(String queryTerm) throws Exception {

    list.clear();/* w  w w  .  j a va  2s .  com*/
    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.apache.org/core/4_1_0/demo/ for details.";
    /* if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
       System.out.println(usage);
       System.exit(0);
     }*/

    String index = "index";
    String field = "contents";
    //   String field = "Abstract";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = queryTerm;
    int hitsPerPage = 20;

    /*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++;
      }
    }
    */
    // index = "/home/subhash/LuceneFolder/Indexer";
    index = "/home/subhash/Dropbox/LuceneFolder/IndexNewData";

    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");
        }

        ArrayList<Publication> list = doPagingSearch(in, searcher, query, hitsPerPage, raw,
                queries == null && queryString == null);

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

    return list;
}

From source file:com.facebook.presto.example.LuceneRecordCursor.java

License:Apache License

public LuceneRecordCursor(List<LuceneColumnHandle> columnHandles) throws ParseException {

    this.columnHandles = columnHandles;

    IndexReader reader = null;/*from w  w w . jav  a  2 s. c o  m*/
    try {
        reader = DirectoryReader
                .open(FSDirectory.open(Paths.get("/home/liyong/workspace-neno/lucenetest/index")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    searcher = new IndexSearcher(reader);
    this.NumDoc = reader.maxDoc();

    fieldToColumnIndex = new int[columnHandles.size()];
    for (int i = 0; i < columnHandles.size(); i++) {
        LuceneColumnHandle columnHandle = columnHandles.get(i);
        fieldToColumnIndex[i] = columnHandle.getOrdinalPosition();
    }
}

From source file:com.facebook.presto.operator.HashAggregationOperator.java

License:Apache License

private Map<String, Long> GetGroupByResult() throws IOException {

    IndexReader reader = null;//from   ww  w  . j  a  v a 2  s  .  c  o  m
    Map<String, Long> returnMap = new HashMap<String, Long>();
    try {
        reader = DirectoryReader
                .open(FSDirectory.open(Paths.get("/home/liyong/workspace-neno/lucenetest/index")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    IndexSearcher searcher = new IndexSearcher(reader);

    Terms terms = MultiFields.getTerms(searcher.getIndexReader(), "orderpriority");
    TermsEnum te = terms.iterator();
    while (te.next() != null) {

        String name = te.term().utf8ToString();
        int count = te.docFreq();
        returnMap.put(name, Long.valueOf(count));
    }

    return returnMap;
}

From source file:com.facebook.presto.operator.ScanFilterAndProjectOperator.java

License:Apache License

private Map<String, Long> getCountResult() throws IOException {

    IndexReader reader = null;/*from   w w w .  j ava2 s .  c o m*/
    Map<String, Long> returnMap = new HashMap<String, Long>();
    try {
        reader = DirectoryReader
                .open(FSDirectory.open(Paths.get("/home/liyong/workspace-neno/lucenetest/index")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    IndexSearcher searcher = new IndexSearcher(reader);

    Terms terms = MultiFields.getTerms(searcher.getIndexReader(), "orderpriority");
    TermsEnum te = terms.iterator();
    while (te.next() != null) {

        String name = te.term().utf8ToString();
        int count = te.docFreq();
        returnMap.put(name, Long.valueOf(count));
    }

    return returnMap;
}

From source file:com.flycode.CRIBSearch.SearchEngine.Demo.SearchFiles.java

License:Apache License

/**
 * Simple command-line based search demo.
 *//*w ww.j  a  v a  2s .c o  m*/
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.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);
    }

    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(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();

    BufferedReader in = null;
    if (queries != null) {
        in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8);
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
    }
    QueryParser parser = new QueryParser(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, 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.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(Version.LUCENE_40, analyzer);
    IndexWriter writer = new IndexWriter(rd, iwConfig);
    InputStream dataIn = getServletContext().getResourceAsStream("/WEB-INF/data.tsv");
    BufferedReader br = new BufferedReader(new InputStreamReader(dataIn, IOUtils.CHARSET_UTF_8));
    String line = br.readLine();/*w  w  w.  ja  v a2s  .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.foundationdb.lucene.SimpleTest.java

License:Open Source License

private void assertDocumentsAreThere(Directory dir, int amount) throws IOException {
    IndexReader reader = DirectoryReader.open(dir);
    try {// w  w  w  .j  a  v  a  2  s  .c o  m
        assertEquals(amount, reader.numDocs());
    } finally {
        reader.close();
    }
}