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.bitranger.parknshop.common.fulltext.SearchShop.java

License:Open Source License

public List<PsShop> search(String value) throws IOException {
    // get the index
    IndexReader reader = DirectoryReader.open(FSDirectory.open(BuildIndexForItem.getIndexFile()));
    // use this to search
    IndexSearcher indexSearcher = new IndexSearcher(reader);
    // use the queryParser to wrap your request
    QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_45, queryString, analyzer);

    List<PsShop> shop = new ArrayList<PsShop>();

    Query query = null;/*  ww  w.ja v a2s  .  c o m*/
    try {
        query = queryParser.parse("title:" + value + "~");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    // we get what we want in the topdocs
    TopDocs topDocs = indexSearcher.search(query, 25);
    System.out.println(":" + topDocs.totalHits + "");
    // ScoreDoc[] scoreDoc = topDocs.scoreDocs;
    for (int i = 0; i < topDocs.scoreDocs.length; i++) {
        Document resultDocument = indexSearcher.doc(topDocs.scoreDocs[i].doc);
        PsShop myshop = new PsShop();
        myshop.setId(Integer.valueOf(resultDocument.get((indexField[0]))));
        myshop.setName(resultDocument.get((indexField[2])));
        myshop.setStatus(Short.valueOf(resultDocument.get((indexField[3]))));
        myshop.setIntroduction(resultDocument.get((indexField[4])));
        myshop.setVote(Double.valueOf(resultDocument.get((indexField[6]))));
        shop.add(myshop);

    }

    return shop;
}

From source file:com.bluedragon.search.collection.CollectionListCategoryFunction.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    String name = getNamedStringParam(argStruct, "collection", null);
    if (name == null)
        throwException(_session, "please specifiy the 'collection' attribute");

    if (!CollectionFactory.isCollection(name))
        throwException(_session, "collection, " + name + ", does not exist");

    Collection col = CollectionFactory.getCollection(name);

    // Collection up the stats
    Map<String, CountWrapper> catList = new FastMap<String, CountWrapper>();
    Map<String, CountWrapper> catTreeList = new FastMap<String, CountWrapper>();

    try {//from w w w .j a va 2 s .c  o m

        // Run around all the documents counting up the categories
        IndexSearcher indexsearcher = col.getIndexSearcher();
        for (int d = 0; d < col.getTotalDocs(); d++) {
            DocumentWrap docWrap = new DocumentWrap(indexsearcher.doc(d));

            String[] categories = docWrap.getCategories();
            if (categories != null) {
                for (int c = 0; c < categories.length; c++) {
                    incrementCount(catList, categories[c]);
                }
            }

            String catTree = docWrap.getCategoryTree();
            if (catTree != null)
                incrementCount(catTreeList, catTree);
        }

        // divide the categories trees into subcategory tree counts
        Object[] keys = catTreeList.keySet().toArray();
        for (int k = 0; k < keys.length; k++) {
            String nextCatTree = keys[k].toString();
            int indx = nextCatTree.indexOf('/');
            while (indx != -1 && indx != (nextCatTree.length() + 1)) {
                String subTree = nextCatTree.substring(0, indx + 1);
                incrementCount(catTreeList, subTree);
                indx = nextCatTree.indexOf('/', indx + 1);
            }
        }

        // create cfStructDatas from the results
        cfStructData cats = new cfStructData();
        Iterator<String> catsIterator = catList.keySet().iterator();
        while (catsIterator.hasNext()) {
            String nextCat = catsIterator.next();
            cats.setData(nextCat, new cfNumberData(catList.get(nextCat).value));
        }

        // Collect up the categorytree counts
        cfStructData catTrees = new cfStructData();
        Iterator<String> catTreesIterator = catTreeList.keySet().iterator();
        while (catTreesIterator.hasNext()) {
            String nextCatTree = catTreesIterator.next();
            catTrees.setData(nextCatTree, new cfNumberData(catTreeList.get(nextCatTree).value));
        }

        cfStructData catListResult = new cfStructData();
        catListResult.setData("CATEGORIES", cats);
        catListResult.setData("CATEGORYTREES", catTrees);

        return catListResult;

    } catch (Exception ioe) {
        throwException(_session, ioe.getMessage());
    }

    return cfBooleanData.TRUE;
}

From source file:com.bluedragon.search.search.QueryRun.java

License:Open Source License

private void addRow(IndexSearcher searcher, int docid, float score, int rank, int searchCount,
        int recordsSearched) throws CorruptIndexException, Exception {
    DocumentWrap document = new DocumentWrap(searcher.doc(docid));

    queryResultData.addRow(1);/* w ww .jav  a  2s .co m*/
    queryResultData.setCurrentRow(queryResultData.getSize());

    // Add in the standard columns that we know we have for every search
    queryResultData.setCell(1, new cfStringData(document.getId()));
    queryResultData.setCell(2, new cfStringData(document.getName()));
    queryResultData.setCell(3, new cfNumberData(score));
    queryResultData.setCell(4, new cfNumberData(searchCount));
    queryResultData.setCell(5, new cfNumberData(recordsSearched));
    queryResultData.setCell(6, new cfNumberData(rank + 1));

    String uC = queryAttributes.getUniqueColumn();

    // Now we do the custom ones
    List<IndexableField> fields = document.getDocument().getFields();
    Iterator<IndexableField> it = fields.iterator();
    while (it.hasNext()) {
        IndexableField fieldable = it.next();

        String fieldName = fieldable.name().toLowerCase();

        // Check for the unique
        if (uniqueSet != null && fieldName.equals(uC)) {
            if (uniqueSet.contains(fieldable.stringValue())) {
                queryResultData.deleteRow(queryResultData.getSize());
                return;
            } else
                uniqueSet.add(fieldable.stringValue());
        }

        // Check to see if we have this column
        if (fieldName.equals("contents") && !queryAttributes.getContentFlag())
            continue;

        if (!activeColumns.containsKey(fieldName)) {
            int newcolumn = queryResultData.addColumnData(fieldable.name().toUpperCase(),
                    cfArrayData.createArray(1), null);
            activeColumns.put(fieldName, newcolumn);
        }

        int column = activeColumns.get(fieldName);
        if (column <= 6)
            continue;

        queryResultData.setCell(column, new cfStringData(fieldable.stringValue()));
    }

    // Do the context stuff if enable
    if (queryAttributes.getContextPassages() > 0) {

        Scorer scorer = new QueryScorer(queryAttributes.getQuery());
        SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(queryAttributes.getContextHighlightStart(),
                queryAttributes.getContextHighlightEnd());
        Highlighter highlighter = new Highlighter(formatter, scorer);
        Fragmenter fragmenter = new SimpleFragmenter(queryAttributes.getContextBytes());
        highlighter.setTextFragmenter(fragmenter);

        String nextContext = "";
        String contents = document.getAttribute(DocumentWrap.CONTENTS);

        if (contents != null) {
            TokenStream tokenStream = AnalyzerFactory.get("simple").tokenStream(DocumentWrap.CONTENTS,
                    new StringReader(contents));
            String[] fragments = null;
            try {
                fragments = highlighter.getBestFragments(tokenStream, contents,
                        queryAttributes.getContextPassages());
                if (fragments.length == 1) {
                    nextContext = fragments[0] + "...";
                } else {
                    StringBuilder context = new StringBuilder();
                    for (int f = 0; f < fragments.length; f++) {
                        context.append("...");
                        context.append(fragments[f]);
                    }
                    context.append("...");
                    nextContext = context.toString();
                }
            } catch (Exception e) {
            }

            // Add in the context
            if (!activeColumns.containsKey("context")) {
                int newcolumn = queryResultData.addColumnData("CONTEXT", cfArrayData.createArray(1), null);
                activeColumns.put("context", newcolumn);
            }

            queryResultData.setCell(activeColumns.get("context"), new cfStringData(nextContext));
        }
    }
}

From source file:com.bull.aurocontrol.csst.poc.index.interval.BaseIntervalQueryTest.java

License:Apache License

protected void assertSearch(IndexSearcher searcher, Query query, Integer... expectedResults)
        throws IOException {
    HashSet<Integer> expected = new HashSet<Integer>(Arrays.asList(expectedResults));

    TopDocs docs = searcher.search(query, 100);
    HashSet<Integer> actual = new HashSet<Integer>();
    for (ScoreDoc scoreDoc : docs.scoreDocs) {
        Document doc = searcher.doc(scoreDoc.doc);
        actual.add(Integer.valueOf(doc.get("id")));
    }/*from w w w .  jav  a  2  s.  co m*/

    Assert.assertEquals(query + " should match " + expected.toString(), expected, actual);
}

From source file:com.burkeware.search.api.internal.lucene.DefaultIndexer.java

License:Open Source License

/**
 * Search the local lucene repository for documents with similar information with information inside the
 * <code>query</code>. Search can return multiple documents with similar information or empty list when no
 * document have similar information with the <code>query</code>.
 *
 * @param query the lucene query./*from   ww w.  j av  a2  s . c o m*/
 * @return objects with similar information with the query.
 * @throws IOException when the search encounter error.
 */
private List<Document> findDocuments(final Query query) throws IOException {
    List<Document> documents = new ArrayList<Document>();
    IndexSearcher searcher = getIndexSearcher();
    if (searcher != null) {
        TopDocs docs = searcher.search(query, DEFAULT_MAX_DOCUMENTS);
        ScoreDoc[] hits = docs.scoreDocs;
        for (ScoreDoc hit : hits)
            documents.add(searcher.doc(hit.doc));
    }
    return documents;
}

From source file:com.chenyi.langeasy.lucene.SearchTest.java

License:Apache License

public static boolean search(IndexSearcher searcher, QueryParser parser, String word)
        throws ParseException, IOException {
    Query query = parser.parse(word);
    System.out.println("Searching for: " + query.toString("contents"));

    TopDocs results = searcher.search(query, 3000);
    ScoreDoc[] hits = results.scoreDocs;

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

    int start = 0;
    int end = numTotalHits;// Math.min(numTotalHits, hitsPerPage);
    boolean result = false;
    if (numTotalHits > 0) {
        findWTotal++;/*from   w  w w.j a v a  2 s . com*/
        result = true;
    }
    for (int i = start; i < end; i++) {
        findSTotal++;
        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");
        }
    }
    return result;
}

From source file:com.codenvy.test.lucene.DeleteFilesWithSameName.java

License:Open Source License

public static void searchAndPrintResult(Path indexPath) throws Exception {
    IndexReader reader = DirectoryReader.open(FSDirectory.open(indexPath));
    IndexSearcher searcher = new IndexSearcher(reader);

    Query query = new PrefixQuery(new Term(PATH, filesDirPath + "/File1"));
    TopDocs topDocs = searcher.search(query, 100);

    System.out.println();//  w ww  .  j a  va2s .  com
    System.out.println("=========================== search =================================");
    final String[] result = new String[topDocs.scoreDocs.length];
    for (int i = 0, length = result.length; i < length; i++) {
        result[i] = searcher.doc(topDocs.scoreDocs[i].doc).getField(PATH).stringValue();
        System.out.println(result[i]);
    }

    reader.close();
}

From source file:com.codeReading.core.opengrok.search.Results.java

License:Open Source License

/**
 * Create a has map keyed by the directory of the document found.
 *
 * @param searcher searcher to use.//from  w  ww  .j  av  a2 s  .  com
 * @param hits hits produced by the given searcher's search
 * @param startIdx the index of the first hit to check
 * @param stopIdx the index of the last hit to check
 * @return a (directory, hitDocument) hashmap
 * @throws CorruptIndexException
 * @throws IOException
 */
private static Map<String, ArrayList<Document>> createMap(IndexSearcher searcher, ScoreDoc[] hits, int startIdx,
        int stopIdx) throws CorruptIndexException, IOException {
    LinkedHashMap<String, ArrayList<Document>> dirHash = new LinkedHashMap<String, ArrayList<Document>>();
    for (int i = startIdx; i < stopIdx; i++) {
        int docId = hits[i].doc;
        Document doc = searcher.doc(docId);
        String rpath = doc.get("path");
        String parent = rpath.substring(0, rpath.lastIndexOf('/'));
        ArrayList<Document> dirDocs = dirHash.get(parent);
        if (dirDocs == null) {
            dirDocs = new ArrayList<Document>();
            dirHash.put(parent, dirDocs);
        }
        dirDocs.add(doc);
    }
    return dirHash;
}

From source file:com.cohesionforce.search.EMFIndex.java

License:Open Source License

/**
 * Searches the index for an attribute with a matching value.
 * //from  w  w  w  .ja v a 2 s  .  c om
 * @param feature
 *            - the attribute to match when searching
 * @param value
 *            - the value to match when searching
 * @return a list of search results
 * @throws IllegalArgumentException
 *             if any parameters are null, or if the attribute is not marked
 *             with the search annotation
 * @throws IOException
 *             if there are issues reading the index
 */
public List<SearchResult> search(EStructuralFeature feature, String value)
        throws IllegalArgumentException, IOException {

    if (feature == null) {
        throw new IllegalArgumentException("Attribute cannot be null");
    }
    if (value == null) {
        throw new IllegalArgumentException("Value cannot be null");
    }

    List<SearchResult> rvalue = new ArrayList<SearchResult>();
    boolean tokenize = false;
    EAnnotation annotation = feature.getEAnnotation(EMFIndexUtil.SOURCE);
    if (annotation != null) {
        if (annotation.getDetails().containsKey(EMFIndexUtil.TOKENIZE_KEY)) {
            tokenize = true;
        }
    } else {
        // Bail out early if this feature should not be indexed
        throw new IllegalArgumentException("Attribute is not annotated to be indexed");
    }

    String key = EMFIndexUtil.getKey(feature);
    DirectoryReader reader = DirectoryReader.open(fsDir);
    IndexSearcher searcher = new IndexSearcher(reader);
    try {

        Query query = null;

        if (tokenize) {
            QueryParser parser = new QueryParser(version, key, analyzer);
            query = parser.parse(value);
        } else {
            Term term = new Term(key, value);
            query = new TermQuery(term);
        }

        ScoreDoc[] hits = searcher.search(query, null, MAX_SEARCH_RESULT).scoreDocs;

        // Iterate through the results:
        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = searcher.doc(hits[i].doc);
            SearchResult result = new SearchResult(hitDoc);
            rvalue.add(result);
            logger.debug(hitDoc.toString());
        }
    } catch (ParseException e) {
        logger.error(e.getMessage());
    } finally {
        reader.close();
    }
    return rvalue;
}

From source file:com.cohesionforce.search.EMFIndex.java

License:Open Source License

/**
 * Searches the index for an attribute with a matching value and a matching
 * eclass//  ww  w  .j  a  v  a2s .  co m
 * 
 * @param eclass
 *            - the EClass to match when searching
 * @param attr
 *            - the EAttribute to match when searching
 * @param value
 *            - the value to match when searching
 * @return a list of search results
 * @throws IllegalArgumentException
 *             if any parameters are null, or if the attribute is not marked
 *             with the search annotation
 * @throws IOException
 *             if there are issues reading the index
 */
public List<SearchResult> search(EClass eclass, EAttribute attr, String value)
        throws IllegalArgumentException, IOException {

    if (eclass == null) {
        throw new IllegalArgumentException("EClass cannot be null");
    }
    if (attr == null) {
        throw new IllegalArgumentException("Attribute cannot be null");
    }
    if (value == null) {
        throw new IllegalArgumentException("Value cannot be null");
    }

    EAnnotation annotation = eclass.getEAnnotation(EMFIndexUtil.SOURCE);
    if (annotation == null) {
        // Bail out early if this feature should not be indexed
        throw new IllegalArgumentException("EClass is not annotated to be indexed");
    }
    List<SearchResult> rvalue = new ArrayList<SearchResult>();
    boolean tokenize = false;
    annotation = attr.getEAnnotation(EMFIndexUtil.SOURCE);
    if (annotation != null) {
        if (annotation.getDetails().containsKey(EMFIndexUtil.TOKENIZE_KEY)) {
            tokenize = true;
        }
    } else {
        // Bail out early if this feature should not be indexed
        throw new IllegalArgumentException("Attribute is not annotated to be indexed");
    }

    String key = EMFIndexUtil.getKey(attr);

    DirectoryReader reader = DirectoryReader.open(fsDir);
    IndexSearcher searcher = new IndexSearcher(reader);
    try {

        BooleanQuery bquery = new BooleanQuery();

        TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, eclass.getName()));
        bquery.add(classQuery, Occur.MUST);

        Query query = null;
        if (tokenize) {
            QueryParser parser = new QueryParser(version, key, analyzer);
            query = parser.parse(value);
        } else {
            Term term = new Term(key, value);
            query = new TermQuery(term);
        }
        bquery.add(query, Occur.MUST);

        ScoreDoc[] hits = searcher.search(bquery, null, MAX_SEARCH_RESULT).scoreDocs;
        // Iterate through the results:
        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = searcher.doc(hits[i].doc);
            SearchResult result = new SearchResult(hitDoc);
            rvalue.add(result);
            logger.debug(hitDoc.toString());
        }
    } catch (ParseException e) {
        logger.error(e.getMessage());
    } finally {
        reader.close();
    }
    return rvalue;
}