Example usage for org.apache.lucene.search IndexSearcher search

List of usage examples for org.apache.lucene.search IndexSearcher search

Introduction

In this page you can find the example usage for org.apache.lucene.search IndexSearcher search.

Prototype

protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException 

Source Link

Document

Lower-level search API.

Usage

From source file:com.netcrest.pado.index.provider.lucene.LuceneSearch.java

License:Open Source License

protected Set<Object> getIdentityKeySet(String queryString, Directory dir) {
    Set<Object> identityKeySet = new HashSet<Object>();
    DirectoryReader reader;/* w ww  .j  a  va 2 s.  c om*/
    try {
        reader = DirectoryReader.open(dir);
    } catch (CorruptIndexException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }

    Query query;
    try {
        StandardQueryParser parser = new StandardQueryParser(new StandardAnalyzer(LUCENE_VERSION));
        query = parser.parse(queryString.replaceAll("\\-", "\\\\-"), "__doc");
    } catch (Exception ex) {
        // Lucene bug. Unable to serialize exception. Log the message and
        // throw a new exception with the string message.
        ex.printStackTrace();
        throw new PadoException(ex.getMessage());
    }
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs results;
    try {
        results = searcher.search(query, null, Integer.MAX_VALUE);

        for (ScoreDoc hit : results.scoreDocs) {
            Document doc;
            try {
                doc = searcher.doc(hit.doc);
            } catch (CorruptIndexException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            //            IndexableField field = doc.getField("IdentityKey");
            //            if (field == null) {
            //               continue;
            //            }
            //            Object identityKey = field.stringValue();
            //            if (identityKey == null) {
            //               identityKey = field.numericValue();
            //            }
            //            if (identityKey == null) {
            //               BytesRef br = field.binaryValue();
            //               if (br != null) {
            //                  byte[] blob = br.bytes;
            //                  try {
            //                     identityKey = BlobHelper.deserializeBlob(blob);
            //                     identityKeySet.add(identityKey);
            //                  } catch (Exception ex) {
            //                     Logger.warning("Identity key deserialization error", ex);
            //                  }
            //               } else {
            //                  identityKey = field.toString();
            //               }
            //            }
            LuceneField luceneField = new LuceneField();
            ITemporalKey temporalKey = luceneField.getTemporalKey(doc);
            if (temporalKey != null) {
                identityKeySet.add(temporalKey.getIdentityKey());
            }
        }
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }
    return identityKeySet;
}

From source file:com.netcrest.pado.index.provider.lucene.LuceneSearch.java

License:Open Source License

protected Set<ITemporalKey> getTemporalKeySet(String queryString, Directory dir) {
    Set<ITemporalKey> temporalKeySet = new HashSet<ITemporalKey>();
    DirectoryReader reader;/*from   w  w  w  . j av  a2s.  c o  m*/
    try {
        reader = DirectoryReader.open(dir);
    } catch (CorruptIndexException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }
    Query query;
    try {
        StandardQueryParser parser = new StandardQueryParser(new StandardAnalyzer(LUCENE_VERSION));
        query = parser.parse(queryString.replaceAll("\\-", "\\\\-"), "__doc");
    } catch (Exception ex) {
        // Lucene 4.7 bug, internal message not serializable
        // Send message instead of nesting the cause.
        throw new RuntimeException(ex.getMessage());
    }

    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs results;
    try {
        results = searcher.search(query, null, Integer.MAX_VALUE);

        for (ScoreDoc hit : results.scoreDocs) {
            Document doc;
            try {
                doc = searcher.doc(hit.doc);
            } catch (CorruptIndexException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            LuceneField luceneField = new LuceneField();
            ITemporalKey temporalKey = luceneField.getTemporalKey(doc);
            if (temporalKey != null) {
                temporalKeySet.add(temporalKey);
            }
        }
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }
    return temporalKeySet;
}

From source file:com.netcrest.pado.index.provider.lucene.LuceneSearchRAMDirectory.java

License:Open Source License

private List<Object> getIdentityKeyList(String queryString, RAMDirectory dir) {
    List<Object> list = new ArrayList<Object>();
    IndexReader reader;//from www .  j a  va 2s .c  o  m
    try {
        reader = IndexReader.open(dir);
    } catch (CorruptIndexException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }
    IndexSearcher searcher = new IndexSearcher(reader);

    Query query;
    try {
        query = parser.parse(queryString, "IdentityKey");
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
    TopDocs results;
    try {
        results = searcher.search(query, null, Integer.MAX_VALUE);
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }
    for (ScoreDoc hit : results.scoreDocs) {
        Document doc;
        try {
            doc = searcher.doc(hit.doc);
        } catch (CorruptIndexException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        IndexableField field = doc.getField("IdentityKey");
        if (field == null) {
            continue;
        }
        Object identityKey = null;
        BytesRef br = field.binaryValue();
        if (br != null) {
            byte[] blob = br.bytes;
            try {
                identityKey = BlobHelper.deserializeBlob(blob);
                list.add(identityKey);
            } catch (Exception ex) {
                Logger.warning("Identity key deserialization error", ex);
            }
        } else {
            identityKey = field.stringValue();
            list.add(identityKey);
        }
    }
    return list;
}

From source file:com.netcrest.pado.index.provider.lucene.TopNLuceneSearch.java

License:Open Source License

protected Set<Object> getIdentityKeySet(String queryString, Directory dir) {
    Set<Object> identityKeySet = new HashSet<Object>();
    DirectoryReader reader;/*w w w .  ja  va2s . c o m*/
    try {
        reader = DirectoryReader.open(dir);
    } catch (CorruptIndexException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }

    Query query;
    try {
        StandardQueryParser parser = new StandardQueryParser(new StandardAnalyzer(LUCENE_VERSION));
        query = parser.parse(queryString.replaceAll("\\-", "\\\\-"), "__doc");
    } catch (Exception ex) {
        // Lucene bug. Unable to serialize exception. Log the message and
        // throw a new exception with the string message.
        ex.printStackTrace();
        throw new PadoException(ex.getMessage());
    }
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs results;
    try {
        results = searcher.search(query, null, Integer.MAX_VALUE);
        for (ScoreDoc hit : results.scoreDocs) {
            Document doc;
            try {
                doc = searcher.doc(hit.doc);
            } catch (CorruptIndexException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            // IndexableField field = doc.getField("IdentityKey");
            // if (field == null) {
            // continue;
            // }
            // Object identityKey = field.stringValue();
            // if (identityKey == null) {
            // identityKey = field.numericValue();
            // }
            // if (identityKey == null) {
            // BytesRef br = field.binaryValue();
            // if (br != null) {
            // byte[] blob = br.bytes;
            // try {
            // identityKey = BlobHelper.deserializeBlob(blob);
            // identityKeySet.add(identityKey);
            // } catch (Exception ex) {
            // Logger.warning("Identity key deserialization error", ex);
            // }
            // } else {
            // identityKey = field.toString();
            // }
            // }
            LuceneField luceneField = new LuceneField();
            ITemporalKey temporalKey = luceneField.getTemporalKey(doc);

            if (temporalKey != null) {
                float docScore = hit.score;
                identityKeySet.add(temporalKey.getIdentityKey());
            }
        }
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }
    return identityKeySet;
}

From source file:com.orientechnologies.lucene.test.LuceneNativeFacet.java

License:Apache License

/** User runs a query and counts facets only without collecting the matching documents.*/
private List<FacetResult> facetsOnly() throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

    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:
    searcher.search(new MatchAllDocsQuery(), null /*Filter */, fc);

    // Retrieve results
    List<FacetResult> results = new ArrayList<FacetResult>();

    // Count both "Publish Date" and "Author" dimensions
    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);

    results.add(facets.getTopChildren(10, "Author"));
    results.add(facets.getTopChildren(10, "Publish Date"));

    indexReader.close();/*from   w w w  .j a  v a2s.  com*/
    taxoReader.close();

    return results;
}

From source file:com.orientechnologies.lucene.tx.OLuceneTxChangesAbstract.java

License:Apache License

@Override
public long deletedDocs(Query query, Filter filter) {

    try {//from   www  . j  a  va  2 s .  c o  m
        IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(deletedIdx, true));

        if (filter != null) {
            TopDocs search = indexSearcher.search(query, filter, Integer.MAX_VALUE);
            return search.totalHits;
        } else {
            TopDocs search = indexSearcher.search(query, Integer.MAX_VALUE);
            return search.totalHits;
        }
    } catch (IOException e) {
        OLogManager.instance().error(this, "Error during searcher instantiation", e);
    }

    return 0;
}

From source file:com.orientechnologies.spatial.sandbox.LuceneGeoTest.java

License:Apache License

@Test
public void geoIntersectTest() throws IOException, ParseException {

    RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(
            new GeohashPrefixTree(JtsSpatialContext.GEO, 11), "location");

    strategy.setDistErrPct(0);// w w  w.  ja v a2  s  .com

    IndexWriterConfig conf = new IndexWriterConfig(new StandardAnalyzer());
    final RAMDirectory directory = new RAMDirectory();
    final IndexWriter writer = new IndexWriter(directory, conf);

    Shape point = JtsSpatialContext.GEO.getWktShapeParser().parse("POINT (9.4714708 47.6819432)");

    Shape polygon = JtsSpatialContext.GEO.getWktShapeParser().parse(
            "POLYGON((9.481201171875 47.64885294675266,9.471416473388672 47.65128140482982,9.462661743164062 47.64781214443791,9.449443817138672 47.656947367880335,9.445838928222656 47.66110972448931,9.455795288085938 47.667352637215,9.469013214111328 47.67255449415724,9.477081298828125 47.679142768657066,9.490299224853516 47.678680460743834,9.506263732910156 47.679258344995326,9.51364517211914 47.68191653011071,9.518795013427734 47.677177931734406,9.526691436767578 47.679489496903706,9.53390121459961 47.67139857075435,9.50918197631836 47.66180341832901,9.50815200805664 47.6529003141482,9.51192855834961 47.64654002455372,9.504375457763672 47.64237650648966,9.49270248413086 47.649662445325035,9.48617935180664 47.65151268066222,9.481201171875 47.64885294675266))");

    Document doc = new Document();

    Assert.assertNotEquals(point.relate(polygon), SpatialRelation.INTERSECTS);
    for (IndexableField f : strategy.createIndexableFields(point)) {
        doc.add(f);
    }

    writer.addDocument(doc);
    writer.commit();

    SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, polygon.getBoundingBox());
    Filter filter = strategy.makeFilter(args);
    IndexReader reader = DirectoryReader.open(directory);

    IndexSearcher searcher = new IndexSearcher(reader);

    TopDocs search = searcher.search(new MatchAllDocsQuery(), filter, 1000);
    Assert.assertEquals(search.totalHits, 0);

    reader.close();
    writer.close();
}

From source file:com.paladin.sys.lucene.SearchFiles.java

License:Apache License

/**
 * Simple command-line based search demo.
 *
 * @throws Exception//ww w .  j  a  va  2s.c  om
 */
public static void main(String[] args) throws Exception {
    final String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] "
            + "[-repeat n] [-queries file] [-query string] [-raw] [-paging numPerPage]\n\n"
            + "See http://lucene.apache.org/java/4_0/demo.html for details.";

    args = new String[] { "-index", "D:\\myData\\luceneIdx" };
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        out.println(usage);
        System.exit(0);
    }

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

    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 ("-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])) {
            numPerPage = Short.parseShort(args[i + 1]);
            if (numPerPage <= 0) {
                err.println("?1?:(");
                System.exit(1);
            }
            i++;
        }
    }

    IndexSearcher searcher = new IndexSearcher(FSDirectory.open(new File(index)));
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
    BufferedReader buffered_reader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    QueryParser parser = new QueryParser(Version.LUCENE_31, field, analyzer);

    while (true) {
        out.print(": ");

        String key = queryString != null ? queryString : buffered_reader.readLine();
        if (key == null || key.trim().length() == 0)
            break;

        Query query = parser.parse(key);
        out.println(": " + query.toString(field));

        if (repeat > 0) {// ??N 
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                searcher.search(query, null, 100);
            }
            Date end = new Date();
            out.println(": " + (end.getTime() - start.getTime()) + "ms");
        }
        //  ?
        doPagingSearch(buffered_reader, searcher, query, raw, queryString == null);

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

From source file:com.radialpoint.word2vec.lucene.SearchFiles.java

License:Open Source License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava com.radialpoint.word2vec.lucene.SearchFiles [-index dir] [-vectors v] [-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);//from   ww  w .  jav  a  2  s.c  o m
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    String vectors = "vectors";
    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 ("-vectors".equals(args[i])) {
            vectors = 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);
    final File vectorsFile = new File(vectors);
    Analyzer analyzer = new Analyzer() {

        @SuppressWarnings("deprecation")
        @Override
        protected TokenStreamComponents createComponents(final String fieldName, final java.io.Reader reader) {
            final StandardTokenizer src = new StandardTokenizer(Version.LUCENE_40, reader);
            src.setMaxTokenLength(15);
            TokenStream tok = new StandardFilter(Version.LUCENE_40, src);
            tok = new LowerCaseFilter(Version.LUCENE_40, tok);
            tok = new StopFilter(Version.LUCENE_40, tok, StandardAnalyzer.STOP_WORDS_SET);
            TokenStream baseTok = tok;
            if (vectorsFile.exists()) {
                try {
                    tok = new Word2VecFilter(tok,
                            new QueryExpander(new Vectors(new FileInputStream(vectorsFile)), true,
                                    TermSelection.CUT_75_ABS),
                            3, false);
                } catch (IOException e) {
                    e.printStackTrace();
                    tok = baseTok;
                }
            }
            return new TokenStreamComponents(src, tok) {
                @Override
                protected void setReader(final java.io.Reader reader) throws IOException {
                    src.setMaxTokenLength(15);
                    super.setReader(reader);
                }
            };
        }
    };

    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"));
    }
    @SuppressWarnings("deprecation")
    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.sg.business.vault.index.demo.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.apache.org/core/4_1_0/demo/ for details."; //$NON-NLS-1$
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) { //$NON-NLS-1$ //$NON-NLS-2$
        System.out.println(usage);
        System.exit(0);//from  w ww .  j ava2s . c o m
    }

    String index = "index"; //$NON-NLS-1$
    String field = "contents"; //$NON-NLS-1$
    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])) { //$NON-NLS-1$
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) { //$NON-NLS-1$
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) { //$NON-NLS-1$
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) { //$NON-NLS-1$
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) { //$NON-NLS-1$
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) { //$NON-NLS-1$
            raw = true;
        } else if ("-paging".equals(args[i])) { //$NON-NLS-1$
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page."); //$NON-NLS-1$
                System.exit(1);
            }
            i++;
        }
    }

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

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

        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)); //$NON-NLS-1$

        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()) //$NON-NLS-1$
                    + "ms"); //$NON-NLS-1$
        }

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

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