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:com.vnet.demo.service.lucene.LuceneService.java

License:Apache License

public SearchResult<DocumentData> query(String keyword, int start, int number) {
    SearchResult<DocumentData> searchResult = null;
    try {/*from  ww  w .  java 2  s .co m*/
        List<DocumentData> documentDatas = new ArrayList<DocumentData>();
        DirectoryReader ireader = DirectoryReader.open(index);
        IndexSearcher isearcher = new IndexSearcher(ireader);
        Query query = new QueryParser(version, "title", analyzer).parse(keyword + "*");

        TopDocs hits = null;
        if (start > 0) {
            TopDocs result = isearcher.search(query, start);
            ScoreDoc scoreDoc = result.scoreDocs[result.scoreDocs.length - 1];
            hits = isearcher.searchAfter(scoreDoc, query, number);
        } else {
            hits = isearcher.search(query, number);
        }
        for (int i = 0; i < hits.scoreDocs.length; i++) {
            DocumentData data = new DocumentData();
            Document hitDoc = isearcher.doc(hits.scoreDocs[i].doc);
            data.setId(Long.parseLong(hitDoc.get("id")));
            data.setTitle(hitDoc.get("title"));
            data.setSummary(hitDoc.get("summary"));
            data.setCreateDate(Long.parseLong(hitDoc.get("createDate")));
            documentDatas.add(data);
        }
        searchResult = new SearchResult<DocumentData>(new Long(hits.totalHits), documentDatas);
    } catch (ParseException | IOException e) {
        e.printStackTrace();
    }
    return searchResult;
}

From source file:com.weasel.lucene.ik.sample.IKAnalyzerDemo.java

License:Apache License

public static void main(String[] args) {
    //Lucene Document??
    String fieldName = "text";
    ///*  w ww . jav a  2s.c  om*/
    String text = "IK Analyzer???????";

    //IKAnalyzer?
    Analyzer analyzer = new IKAnalyzer();

    Directory directory = null;
    IndexWriter iwriter = null;
    IndexReader ireader = null;
    IndexSearcher isearcher = null;
    try {
        //
        directory = new RAMDirectory();

        //?IndexWriterConfig
        IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_34, analyzer);
        iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
        iwriter = new IndexWriter(directory, iwConfig);
        //
        Document doc = new Document();
        doc.add(new Field("ID", "10000", Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.add(new Field(fieldName, text, Field.Store.YES, Field.Index.ANALYZED));
        iwriter.addDocument(doc);
        iwriter.close();

        //?**********************************
        //?   
        ireader = IndexReader.open(directory);
        isearcher = new IndexSearcher(ireader);

        String keyword = "?";
        //QueryParser?Query
        QueryParser qp = new QueryParser(Version.LUCENE_34, fieldName, analyzer);
        qp.setDefaultOperator(QueryParser.AND_OPERATOR);
        Query query = qp.parse(keyword);

        //?5?
        TopDocs topDocs = isearcher.search(query, 5);
        System.out.println("" + topDocs.totalHits);
        //
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (int i = 0; i < topDocs.totalHits; i++) {
            Document targetDoc = isearcher.doc(scoreDocs[i].doc);
            System.out.println("" + targetDoc.toString());
        }

    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } finally {
        if (ireader != null) {
            try {
                ireader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (directory != null) {
            try {
                directory.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.work.SearchFiles.java

License:Apache License

/**
 * This demonstrates a typical paging search scenario, where the search engine presents 
 * pages of size n to the user. The user can then go to the next page if interested in
 * the next hits.//from  w ww  .j  ava2s .  c om
 * 
 * When the query is executed for the first time, then only enough results are collected
 * to fill 5 result pages. If the user wants to page beyond this limit, then the query
 * is executed another time and all hits are collected.
 * 
 */
public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query, int hitsPerPage,
        boolean raw, boolean interactive) throws IOException {

    // Collect enough docs to show 5 pages
    TopDocs results = searcher.search(query, 5 * hitsPerPage);
    ScoreDoc[] hits = results.scoreDocs;

    int numTotalHits = results.totalHits;
    System.out.println(numTotalHits + " total matching documents");
    int start = 0;

    int end = Math.min(numTotalHits, hitsPerPage);

    while (true) {
        if (end > hits.length) {
            System.out.println("Only results 1 - " + hits.length + " of " + numTotalHits
                    + " total matching documents collected.");
            System.out.println("Collect more (y/n) ?");
            String line = in.readLine();
            if (line.length() == 0 || line.charAt(0) == 'n') {
                break;
            }

            hits = searcher.search(query, numTotalHits).scoreDocs;
        }

        end = Math.min(hits.length, start + hitsPerPage);

        for (int i = start; i < end; i++) {
            if (raw) { // output raw format
                System.out.println("doc=" + hits[i].doc + " score=" + hits[i].score);
                continue;

            }

            Document doc = searcher.doc(hits[i].doc);
            String path = doc.get("path");
            if (path != null) {
                System.out.println((i + 1) + ". " + path);
                String title = doc.get("title");
                if (title != null) {
                    System.out.println("   Title: " + doc.get("title"));
                }
            } else {
                System.out.println((i + 1) + ". " + "No path for this document");
            }

        }

        if (!interactive || end == 0) {
            break;
        }

        if (numTotalHits >= end) {
            boolean quit = false;
            while (true) {
                System.out.print("Press ");
                if (start - hitsPerPage >= 0) {
                    System.out.print("(p)revious page, ");
                }
                if (start + hitsPerPage < numTotalHits) {
                    System.out.print("(n)ext page, ");
                }
                System.out.println("(q)uit or enter number to jump to a page.");

                String line = in.readLine();
                if (line.length() == 0 || line.charAt(0) == 'q') {
                    quit = true;
                    break;
                }
                if (line.charAt(0) == 'p') {
                    start = Math.max(0, start - hitsPerPage);
                    break;
                } else if (line.charAt(0) == 'n') {
                    if (start + hitsPerPage < numTotalHits) {
                        start += hitsPerPage;
                    }
                    break;
                } else {
                    int page = Integer.parseInt(line);
                    if ((page - 1) * hitsPerPage < numTotalHits) {
                        start = (page - 1) * hitsPerPage;
                        break;
                    } else {
                        System.out.println("No such page");
                    }
                }
            }
            if (quit)
                break;
            end = Math.min(numTotalHits, start + hitsPerPage);
        }
    }
}

From source file:com.wrmsr.search.dsl.SearchServiceImpl.java

License:Apache License

@Override
public List<Hit> searchDocs(QueryNode queryNode, int maxHits) throws IOException {
    Query query = new LuceneQueryCompiler(analyzer, new QueryTermRenderer()).compileQuery(queryNode);
    // Supplier<Float> scoreSupplier = () -> 100.0f;

    Lock lock = indexSearcherLock.readLock();
    try {// w  w w.  ja  v a 2  s  .co  m
        lock.lock();

        checkState(this.indexSearcher.isPresent());
        IndexSearcher indexSearcher = this.indexSearcher.get();

        searchScope.enter();
        try {
            searchScope.seed(Key.get(IndexSearcher.class), indexSearcher);
            searchScopeListeners.enter();
            try {
                final Searcher searcher = injector.getInstance(Searcher.class);

                final Supplier<Float> scoreSupplier = injector
                        .getInstance(Key.get(new TypeLiteral<Supplier<Float>>() {
                        }, ScoreVars.scoreVar("static_weird_score")));

                ScoreDoc[] scoreDocs = searcher.search(query, scoreSupplier, maxHits);

                ImmutableList.Builder<Hit> builder = ImmutableList.builder();
                for (int i = 0; i < scoreDocs.length; ++i) {
                    ScoreDoc scoreDoc = scoreDocs[i];
                    Document document = indexSearcher.doc(scoreDoc.doc);
                    Doc doc = new Doc(document.get("title"), document.get("isbn"));
                    Hit hit = new Hit(doc, scoreDoc.score);
                    builder.add(hit);
                }
                return builder.build();
            } finally {
                searchScopeListeners.exit();
            }
        } catch (RuntimeException | IOException e) {
            throw Throwables.propagate(e);
        } finally {
            searchScope.exit();
        }
    } finally {
        lock.unlock();
    }
}

From source file:com.yangxu.searchengine.search.SearchFiles.java

License:Apache License

/**
 * This demonstrates a typical paging search scenario, where the search
 * engine presents pages of size n to the user. The user can then go to the
 * next page if interested in the next hits.
 * /* w ww  .  ja  v a  2 s  . com*/
 * When the query is executed for the first time, then only enough results
 * are collected to fill 5 result pages. If the user wants to page beyond
 * this limit, then the query is executed another time and all hits are
 * collected.
 * 
 */
public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query, int hitsPerPage,
        boolean raw, boolean interactive) throws IOException {

    // Collect enough docs to show 5 pages
    TopDocs results = searcher.search(query, 5 * hitsPerPage);
    ScoreDoc[] hits = results.scoreDocs;

    int numTotalHits = results.totalHits;
    System.out.println(numTotalHits + " total matching documents");

    int start = 0;
    int end = Math.min(numTotalHits, hitsPerPage);

    while (true) {
        if (end > hits.length) {
            System.out.println("Only results 1 - " + hits.length + " of " + numTotalHits
                    + " total matching documents collected.");
            System.out.println("Collect more (y/n) ?");
            String line = in.readLine();
            if (line.length() == 0 || line.charAt(0) == 'n') {
                break;
            }

            hits = searcher.search(query, numTotalHits).scoreDocs;
        }

        end = Math.min(hits.length, start + hitsPerPage);

        for (int i = start; i < end; i++) {
            if (raw) { // output raw format
                System.out.println("doc=" + hits[i].doc + " score=" + hits[i].score);
                continue;
            }

            Document doc = searcher.doc(hits[i].doc);
            String path = doc.get("title");
            if (path != null) {
                System.out.println((i + 1) + ". " + path);
                String title = doc.get("title");
                if (title != null) {
                    System.out.println("   Title: " + doc.get("title"));
                }
            } else {
                System.out.println((i + 1) + ". " + "No path for this document");
                String title = doc.get("title");
                if (title != null) {
                    System.out.println("   Title: " + doc.get("title"));
                }
            }

        }
        if (!interactive || end == 0) {
            break;
        }

        if (numTotalHits >= end) {
            boolean quit = false;
            while (true) {
                System.out.print("Press ");
                if (start - hitsPerPage >= 0) {
                    System.out.print("(p)revious page, ");
                }
                if (start + hitsPerPage < numTotalHits) {
                    System.out.print("(n)ext page, ");
                }
                System.out.println("(q)uit or enter number to jump to a page.");

                String line = in.readLine();
                if (line.length() == 0 || line.charAt(0) == 'q') {
                    quit = true;
                    break;
                }
                if (line.charAt(0) == 'p') {
                    start = Math.max(0, start - hitsPerPage);
                    break;
                } else if (line.charAt(0) == 'n') {
                    if (start + hitsPerPage < numTotalHits) {
                        start += hitsPerPage;
                    }
                    break;
                } else {
                    int page = Integer.parseInt(line);
                    if ((page - 1) * hitsPerPage < numTotalHits) {
                        start = (page - 1) * hitsPerPage;
                        break;
                    } else {
                        System.out.println("No such page");
                    }
                }
            }
            if (quit)
                break;
            end = Math.min(numTotalHits, start + hitsPerPage);
        }
    }
}

From source file:concurrency.SearchFiles.java

License:Apache License

/**
 * This demonstrates a typical paging search scenario, where the search
 * engine presents pages of size n to the user. The user can then go to the
 * next page if interested in the next hits.
 * //from w  ww .j a v a2  s.  c o m
 * When the query is executed for the first time, then only enough results
 * are collected to fill 5 result pages. If the user wants to page beyond
 * this limit, then the query is executed another time and all hits are
 * collected.
 * 
 */
public static void doSearch(BufferedReader in, IndexSearcher searcher, Query query, int hitsPerPage,
        boolean raw, boolean interactive) throws IOException {
    int page = 1;
    // Collect enough docs to show 5 pages
    TopDocs results = searcher.search(query, page * hitsPerPage);
    ScoreDoc[] hits = results.scoreDocs;

    int numTotalHits = results.totalHits;
    System.out.println(numTotalHits + " total matching documents");

    int offset = hitsPerPage * (page - 1);
    int start = 0;
    int end = Math.min(numTotalHits, offset); //hitsPerPage * page);

    for (int i = start + offset; i < end + offset; i++) {
        Document doc = searcher.doc(hits[i].doc);
        String path = doc.get("path");
        if (path != null) {
            System.out.println((i + 1) + ". " + path);
            String title = doc.get("title");
            if (title != null) {
                System.out.println("   Title: " + doc.get("title"));
            }
        } else {
            System.out.println((i + 1) + ". " + "No path for this document");
        }

    }
}

From source file:control.Search.java

/**
 * Search for previously indexed feeds through 'title' and 'description' fields, according to a query
 * /* ww  w .  java2s .c o  m*/
 * @param query terms to be considered in the search
 * @return a JSON representation of the retrieved feeds
 * @throws ParseException query parsing failure
 * @throws IOException  I/O issue when creating index
 */
public String queryIndexedFeeds(String query) throws ParseException, IOException {
    //creates IndexReader with analyzers
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);
    StandardAnalyzer analyzer = new StandardAnalyzer();
    MultiFieldQueryParser queryParser = new MultiFieldQueryParser(new String[] { "title", "description" },
            analyzer);

    //search for documents
    TopDocs docs = searcher.search(queryParser.parse(query), 25);
    ScoreDoc[] hits = docs.scoreDocs;

    //iterate over results and put on JSON format
    JSONArray jsonArray = new JSONArray();
    for (int i = 0; i < hits.length; i++) {
        int docId = hits[i].doc;
        Document d = searcher.doc(docId);

        //create new json object
        JSONObject json = new JSONObject();
        json.put("id", d.get("id"));
        json.put("link", d.get("link"));
        json.put("title", d.get("title"));
        json.put("description", d.get("description"));

        jsonArray.put(json);
    }

    reader.close();
    String ret = jsonArray.toString();

    return ret;
}

From source file:CopulaResources.TermCooccurence.java

public static TermCooccurence generateCooccurencebyClass(IndexReader indexReader, String classFieldName,
        String textFieldName, Analyzer analyzer, int minFreq, int maxFreq) throws IOException {
    System.out.println(":::Generating Term-Pair List:::");
    TermCooccurence CooccurList = new TermCooccurence();
    Terms classes = MultiFields.getTerms(indexReader, classFieldName);
    if (classes != null) {
        TermsEnum classesEnum = classes.iterator();
        BytesRef nextClass;//w w  w  .  j a v a 2s.c  o  m
        while ((nextClass = classesEnum.next()) != null) {
            if (nextClass.length > 0) {

                Term term = new Term(classFieldName, nextClass);
                String tpClass = nextClass.utf8ToString();
                BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
                booleanQuery.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.MUST));
                IndexSearcher indexSearcher = new IndexSearcher(indexReader);
                TopDocs topDocs;
                topDocs = indexSearcher.search(booleanQuery.build(), indexReader.numDocs());

                for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                    IndexableField[] storableFields = indexSearcher.doc(scoreDoc.doc).getFields(textFieldName);
                    for (IndexableField singleStorableField : storableFields) {
                        if (singleStorableField != null) {
                            BytesRef text = new BytesRef(singleStorableField.stringValue());
                            generateCooccurences(text.utf8ToString(), analyzer, CooccurList, tpClass);
                        }
                    }
                }
                CooccurList.trimbyFreq(tpClass, minFreq, maxFreq);

            }
        }
    }
    System.out.println(":::Generation Complete:::");
    return CooccurList;
}

From source file:CopulaResources.TermCooccurence.java

public static void generateCooccurencebyClass(IndexReader indexReader, String classFieldName,
        String textFieldName, Analyzer analyzer, int minFreq, int maxFreq, Path saveDir) throws IOException {
    System.out.println(":::Generating Term-Pair List:::");
    TermCooccurence CooccurList = new TermCooccurence();
    Terms classes = MultiFields.getTerms(indexReader, classFieldName);
    if (classes != null) {
        TermsEnum classesEnum = classes.iterator();
        BytesRef nextClass;//from w w w . j  a  v  a 2  s . c o m
        while ((nextClass = classesEnum.next()) != null) {
            if (nextClass.length > 0) {

                Term term = new Term(classFieldName, nextClass);
                String tpClass = nextClass.utf8ToString();
                BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
                booleanQuery.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.MUST));
                IndexSearcher indexSearcher = new IndexSearcher(indexReader);
                TopDocs topDocs;
                topDocs = indexSearcher.search(booleanQuery.build(), indexReader.numDocs());

                for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                    IndexableField[] storableFields = indexSearcher.doc(scoreDoc.doc).getFields(textFieldName);
                    for (IndexableField singleStorableField : storableFields) {
                        if (singleStorableField != null) {
                            BytesRef text = new BytesRef(singleStorableField.stringValue());
                            generateCooccurences(text.utf8ToString(), analyzer, CooccurList, tpClass);
                        }
                    }
                }
                CooccurList.trimbyFreq(tpClass, minFreq, maxFreq);

            }
        }
    }
    CooccurList.savetoFile(saveDir);
    System.out.println(":::Generation Complete:::");
}

From source file:CopulaResources.TermCooccurence.java

public static void generateNCooccurencebyClass(IndexReader indexReader, String classFieldName,
        String textFieldName, Analyzer analyzer, String direction, double percent, Path saveDir)
        throws IOException {
    System.out.println(":::Generating Term-Pair List:::");
    TermCooccurence CooccurList = new TermCooccurence();
    Terms classes = MultiFields.getTerms(indexReader, classFieldName);
    if (classes != null) {
        TermsEnum classesEnum = classes.iterator();
        BytesRef nextClass;/*from  ww w  .j ava2 s.c om*/
        while ((nextClass = classesEnum.next()) != null) {
            if (nextClass.length > 0) {

                Term term = new Term(classFieldName, nextClass);
                String tpClass = nextClass.utf8ToString();
                BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
                booleanQuery.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.MUST));
                IndexSearcher indexSearcher = new IndexSearcher(indexReader);
                TopDocs topDocs;
                topDocs = indexSearcher.search(booleanQuery.build(), indexReader.numDocs());

                for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                    IndexableField[] storableFields = indexSearcher.doc(scoreDoc.doc).getFields(textFieldName);
                    for (IndexableField singleStorableField : storableFields) {
                        if (singleStorableField != null) {
                            BytesRef text = new BytesRef(singleStorableField.stringValue());
                            generateCooccurences(text.utf8ToString(), analyzer, CooccurList, tpClass);
                        }
                    }
                }
                CooccurList.trimbyPercent(tpClass, direction, percent);

            }
        }
    }
    CooccurList.savetoFile(saveDir);
    System.out.println(":::Generation Complete:::");
}