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

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

Introduction

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

Prototype

public Document doc(int docID) throws IOException 

Source Link

Document

Sugar for .getIndexReader().document(docID)

Usage

From source file:jena.spatialindexdump.java

License:Apache License

private static void dump(SpatialIndexLucene spatialIndex) {
    try {//w  w w . ja  v a  2  s .  c o  m
        Directory directory = spatialIndex.getDirectory();
        Analyzer analyzer = spatialIndex.getAnalyzer();
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        QueryParser queryParser = new QueryParser(spatialIndex.getDocDef().getEntityField(), analyzer);
        Query query = queryParser.parse("*:*");
        ScoreDoc[] sDocs = indexSearcher.search(query, 1000).scoreDocs;
        for (ScoreDoc sd : sDocs) {
            System.out.println("Doc: " + sd.doc);
            Document doc = indexSearcher.doc(sd.doc);
            //System.out.println(doc) ;
            for (IndexableField f : doc) {
                //System.out.println("  "+f) ;
                System.out.println("  " + f.name() + " = " + f.stringValue());
            }

        }

    } catch (Exception ex) {
        throw new SpatialIndexException(ex);
    }

}

From source file:jena.textindexdump.java

License:Apache License

private static void dump(TextIndexLucene textIndex) {
    try {//from  ww w .  j  a va 2s  .  c om
        Directory directory = textIndex.getDirectory();
        Analyzer analyzer = textIndex.getQueryAnalyzer();
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        QueryParser queryParser = new QueryParser(textIndex.getDocDef().getPrimaryField(), analyzer);
        Query query = queryParser.parse("*:*");
        ScoreDoc[] sDocs = indexSearcher.search(query, 1000).scoreDocs;
        for (ScoreDoc sd : sDocs) {
            System.out.println("Doc: " + sd.doc);
            Document doc = indexSearcher.doc(sd.doc);
            // Don't forget that many fields aren't stored, just indexed.
            for (IndexableField f : doc) {
                //System.out.println("  "+f) ;
                System.out.println("  " + f.name() + " = " + f.stringValue());
            }

        }

    } catch (Exception ex) {
        throw new TextIndexException(ex);
    }

}

From source file:jlu.search.Searcher.java

License:Apache License

public static void search(String indexDir, String q) throws IOException, ParseException {

    Directory dir = FSDirectory.open(new File(indexDir));
    IndexSearcher is = new IndexSearcher(dir);

    QueryParser parser = new QueryParser(Version.LUCENE_30, "contents",
            new StandardAnalyzer(Version.LUCENE_30));
    Query query = parser.parse(q);
    long start = System.currentTimeMillis();
    TopDocs hits = is.search(query, 10);
    long end = System.currentTimeMillis();

    System.err.println("Found " + hits.totalHits + " document(s) (in " + (end - start)
            + " milliseconds) that matched query '" + q + "':");

    for (ScoreDoc scoreDoc : hits.scoreDocs) {
        Document doc = is.doc(scoreDoc.doc);
        // System.out.println(doc.get("title"));
        System.out.println(doc.get("fullpath"));
    }/*www.  ja  v  a 2s.  c o  m*/

    is.close();
}

From source file:jlu.search.Searcher.java

License:Apache License

/**
 *   qtopNum autoCompleted/* w  w w .j ava  2s  .  c  om*/
 * @param indexDir
 * @param q
 * @param topNum
 * @return
 * @throws IOException
 * @throws ParseException
 */
public static List<SearchResultBean> searchRetList(String indexDir, String q, int topNum)
        throws IOException, ParseException {
    List<SearchResultBean> ret = new ArrayList<SearchResultBean>();
    Directory dir = FSDirectory.open(new File(indexDir));
    IndexSearcher is = new IndexSearcher(dir);

    QueryParser parser = new QueryParser(Version.LUCENE_30, "contents",
            new StandardAnalyzer(Version.LUCENE_30));
    Query query = parser.parse(q);
    TopDocs hits = is.search(query, topNum);
    for (ScoreDoc scoreDoc : hits.scoreDocs) {
        Document doc = is.doc(scoreDoc.doc);
        SearchResultBean srb = new SearchResultBean();
        srb.setTitle(doc.get("title"));
        srb.setFullpath(doc.get("fullpath"));
        ret.add(srb);
        Tolls tool = new Tolls();
        srb.setHighlightedAbstract(tool.autoCompl(doc, query, ConstantFactory.AUTO_COMPL_FRAGE_LENGTH));
        ret.add(srb);
    }
    is.close();
    return ret;
}

From source file:jobs.EvaluateRetrieval.java

@Override
public void doJob() throws Exception {

    Path filePath = VirtualFile.fromRelativePath("/data/mesh_disease_terms.txt").getRealFile().toPath();
    Charset charset = Charset.defaultCharset();
    List<String> lines = Files.readAllLines(filePath, charset);

    Stopwatch stopwatch = Stopwatch.createUnstarted();
    stopwatch.start();//from ww w.  java  2s .co m

    int total = lines.size();
    int counter = 0;

    //TODO just to store the resutls and printing file
    StringBuilder sb = new StringBuilder();
    sb.append("PMID\tMESH_ID\tMESH_TERM\n");

    for (String line : lines) {
        String[] splits = line.split("\t");
        String id = splits[0];
        String term = splits[1];
        String originalTerm = splits[1];

        counter++;
        Logger.info("Term: " + term + "(" + counter + "/" + total + ")");

        if (term.contains(",")) {
            Pattern p = Pattern.compile("(.*), (.*)");
            Matcher m = p.matcher(term);
            if (m.find()) {
                String post = m.group(1);
                String pre = m.group(2);
                term = pre + " " + post;
                Logger.info("Term modified: " + term);
            }
        }

        Directory directory = FSDirectory.open(VirtualFile.fromRelativePath("/index").getRealFile());
        DirectoryReader ireader = DirectoryReader.open(directory);

        //TODO Query analyzer - can be changed and switched
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
        IndexSearcher isearcher = new IndexSearcher(ireader);

        //Maybe different type of parser?
        QueryParser parser = new QueryParser(Version.LUCENE_47, "contents", analyzer);

        // Logger.info("Query: " + term);
        if (!term.contains("(")) {

            //TODO different syntax and operators
            Query query = parser.parse("\"" + term.replace("/", "\\/") + "\"");
            //Logger.info("query: " + query.toString());

            ScoreDoc[] hits = isearcher.search(query, null, 10000).scoreDocs;
            //Logger.info("results: " + hits.length);
            int freq = hits.length;

            if (freq > 0) {
                for (int i = 0; i < hits.length; i++) {
                    Document hitDoc = isearcher.doc(hits[i].doc);
                    //Logger.info(hitDoc.get("pmid") + " - " + hits[i].score);
                    sb.append(hitDoc.get("pmid")).append("\t").append(id).append("\t").append(originalTerm)
                            .append("\n");
                }
            }

        }
        ireader.close();
        directory.close();

    }
    stopwatch.stop();
    Logger.info("Time to index the documents: " + stopwatch.elapsed(TimeUnit.SECONDS));
    File file = VirtualFile.fromRelativePath("/data/annotatedArticles.txt").getRealFile();
    FileUtils.writeStringToFile(file, sb.toString());
    Logger.info("File saved: " + file.getAbsolutePath());
}

From source file:junit.org.rapidpm.microservice.optionals.index.v004.server.LoggerEventIndex.java

License:Apache License

@Override
public List<LoggerEvent> query(final String query) {
    try {//w  w w.ja  va  2s. com
        final Query q = new QueryParser(MESSAGE, analyzer).parse(query);
        final IndexSearcher searcher = new IndexSearcher(directoryReader);
        final TopScoreDocCollector collector = TopScoreDocCollector.create(1_00);
        searcher.search(q, collector);
        final ScoreDoc[] hits = collector.topDocs().scoreDocs;
        final List<LoggerEvent> result = new ArrayList<>();
        for (final ScoreDoc hit : hits) {
            final int doc = hit.doc;
            final Document document = searcher.doc(doc);
            document.get(LEVEL);
            result.add(new LoggerEvent().level(document.get(LEVEL)).message(document.get(MESSAGE)).timestamp(
                    LocalDateTime.parse(document.get(TIMESTAMP), IndexManagement.DATE_TIME_FORMATTER)));
        }
        return result;
    } catch (ParseException | IOException e) {
        e.printStackTrace();
    }
    return Collections.emptyList();
}

From source file:Lab6.LuceneTest.java

public static void main(String[] args) {

    try {/*  ww  w.ja va2 s  . co m*/
        //   Specify the analyzer for tokenizing text.
        //   The same analyzer should be used for indexing and searching
        StandardAnalyzer analyzer = new StandardAnalyzer();

        //   Code to create the index
        Directory index = new RAMDirectory();

        IndexWriterConfig config = new IndexWriterConfig(analyzer);

        IndexWriter w = new IndexWriter(index, config);
        addDoc(w, " Software Engineering 2", "133");
        addDoc(w, " Software Engineering 1", "CMPE 131:");
        addDoc(w, " Object Oriented Design", "CS 151:");
        addDoc(w, " Advance Data Structures with Java ", "CS 146:");
        addDoc(w, " System Security with Java", "CS 166:");
        addDoc(w, "Lucene demo by teja", "23k43413");
        w.close();

        //   Text to search
        String querystr = args.length > 0 ? args[0] : "Object";

        //   The \"title\" arg specifies the default field to use when no field is explicitly specified in the query
        Query q = new QueryParser("Classes", analyzer).parse(querystr);

        // Searching code
        int hitsPerPage = 10;
        IndexReader reader = DirectoryReader.open(index);
        IndexSearcher searcher = new IndexSearcher(reader);
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);
        searcher.search(q, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;

        //   Code to display the results of search
        System.out.println("Found " + hits.length + " Classes Matching your Requirement");
        for (int i = 0; i < hits.length; ++i) {
            int docId = hits[i].doc;
            Document d = searcher.doc(docId);
            System.out.println((i + 1) + ". " + d.get("Number") + d.get("Classes"));
        }

        // reader can only be closed when there is no need to access the documents any more
        reader.close();
    }

    catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:lab_mri.SearchEngine.java

public List<SearchResult> search(String query, String[] fields, int n)
        throws IOException, ParseException, Exception {

    String eq = QueryParser.escape(query);
    QueryParser parser = new MultiFieldQueryParser(fields, new CustomAnalyzer());
    Query q = parser.parse(eq);/*from  w  w  w. j  a  v a2 s  .co  m*/
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(FSDirectory.open(this.dir)));
    List<SearchResult> results = new ArrayList<>();
    TopDocs top = searcher.search(q, n);

    for (ScoreDoc doc : top.scoreDocs) {
        results.add(new SearchResult(searcher.doc(doc.doc).get("id"), doc.score));
    }
    return results;
}

From source file:lia.analysis.codec.MetaphoneAnalyzerTest.java

License:Apache License

public void testKoolKat() throws Exception {
    RAMDirectory directory = new RAMDirectory();
    Analyzer analyzer = new MetaphoneReplacementAnalyzer();

    IndexWriter writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);

    Document doc = new Document();
    doc.add(new Field("contents", //#A
            "cool cat", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);/*  w w  w.j  a  v  a 2s.  c  om*/
    writer.close();

    IndexSearcher searcher = new IndexSearcher(directory);

    Query query = new QueryParser(Version.LUCENE_30, //#B
            "contents", analyzer) //#B
                    .parse("kool kat"); //#B

    TopDocs hits = searcher.search(query, 1);
    assertEquals(1, hits.totalHits); //#C
    int docID = hits.scoreDocs[0].doc;
    doc = searcher.doc(docID);
    assertEquals("cool cat", doc.get("contents")); //#D

    searcher.close();
}

From source file:lia.chapter1.Searcher.java

License:Apache License

/**
 * creates index reader pointing to indexes and index searcher to search for docs matching given query(set to queryparser)
 *
 * @param indexDir/* w ww  . j a v  a2  s  .co  m*/
 * @param fieldName
 * @param searchString
 * @throws ParseException
 * @throws IOException
 */
public static void search(String indexDir, final String fieldName, final String searchString)
        throws ParseException, IOException {
    IndexSearcher searcher = Utils.getIndexSearcher(indexDir);

    QueryParser parser = new QueryParser(fieldName, new StandardAnalyzer());
    Query query = parser.parse(searchString);

    TopDocs docs = searcher.search(query, 100);
    ScoreDoc[] hits = docs.scoreDocs;
    int numTotalHits = docs.totalHits;
    System.out.println("Total match:" + numTotalHits);
    Document[] documents = new Document[numTotalHits];
    System.out.println("result docs: ");
    for (int i = 0; i < numTotalHits; i++) {
        documents[i] = searcher.doc(hits[i].doc);
        System.out.println(documents[i].get("filepath"));
    }

}