Example usage for org.apache.lucene.index IndexReader document

List of usage examples for org.apache.lucene.index IndexReader document

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexReader document.

Prototype

public final Document document(int docID, Set<String> fieldsToLoad) throws IOException 

Source Link

Document

Like #document(int) but only loads the specified fields.

Usage

From source file:com.esri.gpt.catalog.arcgis.agportal.publication.ItemInfoLuceneAdapter.java

License:Apache License

/**
 * Returns the field values associated with a document
 * //from   w  ww  . j  a  v a 2 s.  co  m
 * @param context
 *          the operation context
 * @param fields
 *          array of fields to fetch
 * @param uuid
 *          the document uuid
 * @return the field values (null if not found)
 * @throws CorruptIndexException
 *           if the index is corrupt
 * @throws IOException
 *           if an I/O exception occurs
 */
public ESRI_ItemInformation makeItemInfoByUuid(RequestContext context, String[] fields, String uuid)
        throws CorruptIndexException, IOException {
    TermDocs termDocs = null;
    IndexReader reader = null;
    MapFieldSelector selector = null;
    try {
        uuid = Val.chkStr(uuid);
        if (uuid.length() > 0) {
            IndexSearcher srch = this.getSearcher(context);
            reader = srch.getIndexReader();
            if (fields != null) {
                selector = new MapFieldSelector(fields);
            }
            termDocs = reader.termDocs();
            termDocs.seek(new Term(Storeables.FIELD_UUID, uuid));
            if (termDocs.next()) {
                Document document = reader.document(termDocs.doc(), selector);
                List<Fieldable> flds = document.getFields();

                ESRI_ItemInformation itemInfo = new ESRI_ItemInformation();
                itemInfo.setType("Map Service");
                itemInfo.setTags(Arrays.asList(new String[] { "ArcGIS", "Server map", "service" }));

                Envelope extent = new Envelope();
                for (Fieldable fld : flds) {
                    String fieldName = fld.name();
                    String[] vals = document.getValues(fieldName);
                    if (fieldName.contains("uuid")) {
                        itemInfo.setId(vals[0].replaceAll("^\\{|\\}$|\\-", ""));
                    } else if (fieldName.contains("title")) {
                        itemInfo.setTitle(vals[0]);
                        itemInfo.setName(vals[0]);
                    } else if (fieldName.contains("resource.url")) {
                        itemInfo.setUrl(vals[0]);
                        String type = Val.chkStr(guessServiceTypeFromUrl(vals[0]));
                        if (type.length() > 0) {
                            itemInfo.setType(type);
                        }
                    } else if (fieldName.contains("contentType")) {
                    } else if (fieldName.contains("keywords")) {
                        itemInfo.setTypeKeywords(Arrays.asList(vals));
                        itemInfo.setTags(Arrays.asList(vals));
                    } else if (fieldName.contains("dataTheme")) {
                        itemInfo.setTags(Arrays.asList(vals));
                    } else if (fieldName.contains("abstract")) {
                        itemInfo.setDescription(vals[0]);
                        //                  } else if (fieldName.contains("xml")) {
                        //                     itemInfo.setXml(vals[0]);
                    } else if (fieldName.contains("minx")) {
                        extent.setMinX(vals[0]);
                    } else if (fieldName.contains("miny")) {
                        extent.setMinY(vals[0]);
                    } else if (fieldName.contains("maxx")) {
                        extent.setMaxX(vals[0]);
                    } else if (fieldName.contains("maxy")) {
                        extent.setMaxY(vals[0]);
                    } else if (fieldName.contains("thumbnail.url")) {
                        itemInfo.setThumbnailUrl(vals[0]);
                    }
                }
                itemInfo.setExtent(extent);
                return itemInfo;
            }
        }
    } finally {
        try {
            if (termDocs != null)
                termDocs.close();
            if (reader != null)
                reader.close();
        } catch (Exception ef) {
        }
    }
    return null;
}

From source file:com.esri.gpt.catalog.lucene.LuceneIndexAdapter.java

License:Apache License

/**
 * Queries the ACL values indexed for a document.
 * @param uuid the document UUID//from  www.jav  a  2 s  .  c o m
 * @return the ACL values (can be null)
 * @throws CatalogIndexException if an exception occurs
 */
@Override
public String[] queryAcls(String uuid) throws CatalogIndexException {
    ArrayList<String> values = new ArrayList<String>();
    IndexSearcher searcher = null;
    TermDocs termDocs = null;
    try {
        uuid = Val.chkStr(uuid);
        if (uuid.length() > 0) {
            searcher = newSearcher();
            String[] aFields = new String[] { Storeables.FIELD_ACL };
            MapFieldSelector selector = new MapFieldSelector(aFields);
            searcher = newSearcher();
            IndexReader reader = searcher.getIndexReader();
            termDocs = reader.termDocs();
            termDocs.seek(new Term(Storeables.FIELD_UUID, uuid));
            if (termDocs.next()) {
                Document document = reader.document(termDocs.doc(), selector);
                Field[] fields = document.getFields(Storeables.FIELD_ACL);
                if ((fields != null) && (fields.length > 0)) {
                    for (Field field : fields) {
                        values.add(field.stringValue());
                    }
                }
            }
        }
    } catch (IOException e) {
        String sMsg = "Error accessing index:\n " + Val.chkStr(e.getMessage());
        throw new CatalogIndexException(sMsg, e);
    } finally {
        try {
            if (termDocs != null)
                termDocs.close();
        } catch (Exception ef) {
        }
        closeSearcher(searcher);
    }
    return values.toArray(new String[0]);
}

From source file:com.esri.gpt.catalog.lucene.LuceneIndexAdapter.java

License:Apache License

/**
 * Queries the system modified date associated with an indexed document.
 * @param uuid the document UUID/*w  w w.j av  a2 s. c o  m*/
 * @return the update date (null if none was found)
 * @throws CatalogIndexException if an exception occurs
 */
@Override
public Timestamp queryModifiedDate(String uuid) throws CatalogIndexException {
    Timestamp tsUpdate = null;
    IndexSearcher searcher = null;
    TermDocs termDocs = null;
    try {
        uuid = Val.chkStr(uuid);
        if (uuid.length() > 0) {
            String[] aFields = new String[] { Storeables.FIELD_DATEMODIFIED };
            MapFieldSelector selector = new MapFieldSelector(aFields);
            searcher = newSearcher();
            IndexReader reader = searcher.getIndexReader();
            termDocs = reader.termDocs();
            termDocs.seek(new Term(Storeables.FIELD_UUID, uuid));
            if (termDocs.next()) {
                Document document = reader.document(termDocs.doc(), selector);
                String sUpdate = document.get(Storeables.FIELD_DATEMODIFIED);
                tsUpdate = new Timestamp(Long.valueOf(sUpdate));
            }
        }
    } catch (IOException e) {
        String sMsg = "Error accessing index:\n " + Val.chkStr(e.getMessage());
        throw new CatalogIndexException(sMsg, e);
    } finally {
        try {
            if (termDocs != null)
                termDocs.close();
        } catch (Exception ef) {
        }
        closeSearcher(searcher);
    }
    return tsUpdate;
}

From source file:com.esri.gpt.control.search.browse.TocIndexAdapter.java

License:Apache License

/**
 * Returns the field values associated with a document
 * @param context the operation context/*from   www . ja v  a  2s.co  m*/
 * @param fieldName the field name
 * @param uuid the document uuid
 * @return the field values (null if not found)
 * @throws CorruptIndexException if the index is corrupt
 * @throws IOException if an I/O exception occurs
 */
public String[] queryFieldByUuid(TocContext context, String fieldName, String uuid)
        throws CorruptIndexException, IOException {
    TermDocs termDocs = null;
    try {
        uuid = Val.chkStr(uuid);
        if (uuid.length() > 0) {
            IndexSearcher searcher = this.getSearcher(context);
            IndexReader reader = searcher.getIndexReader();
            MapFieldSelector selector = new MapFieldSelector(new String[] { fieldName });
            termDocs = reader.termDocs();
            termDocs.seek(new Term(Storeables.FIELD_UUID, uuid));
            if (termDocs.next()) {
                Document document = reader.document(termDocs.doc(), selector);
                return document.getValues(fieldName);
            }
        }
    } finally {
        try {
            if (termDocs != null)
                termDocs.close();
        } catch (Exception ef) {
        }
    }
    return null;
}

From source file:com.github.hotware.lucene.extension.highlight.BaseObjectFragmentsBuilder.java

License:BEER-WARE LICENSE

private static Field[] getFields(IndexReader reader, int docId, final String fieldName) throws IOException {
    // according to javadoc, doc.getFields(fieldName) cannot be used with
    // lazy loaded field???
    final List<Field> fields = new ArrayList<>();
    reader.document(docId, new StoredFieldVisitor() {

        @Override//from   w w  w .  j a  va 2s .  c  o m
        public void stringField(FieldInfo fieldInfo, String value) {
            FieldType ft = new FieldType(TextField.TYPE_STORED);
            ft.setStoreTermVectors(fieldInfo.hasVectors());
            fields.add(new Field(fieldInfo.name, value, ft));
        }

        @Override
        public Status needsField(FieldInfo fieldInfo) {
            return fieldInfo.name.equals(fieldName) ? Status.YES : Status.NO;
        }

    });
    return fields.toArray(new Field[fields.size()]);
}

From source file:com.gnizr.core.search.SearchIndexManager.java

License:Mozilla Public License

/**
 * Finds the representative bookmark document for a ginve URL hash.
 * //from   www  .ja v  a 2 s .c  om
 * @param urlHash
 *            a URL MD5 Hash.
 * 
 * @return a Lucene document of the representative bookmark
 */
public Document findLeadDocument(String urlHash) {
    IndexReader reader = null;
    TermDocs termDocs = null;
    Document leadDoc = null;
    try {
        boolean exists = IndexReader.indexExists(indexDirectory);
        if (exists == true) {
            reader = IndexReader.open(indexDirectory);
            Term key = new Term(DocumentCreator.FIELD_URL_MD5, urlHash);
            termDocs = reader.termDocs(key);
            boolean found = false;
            while (termDocs.next() && found == false) {
                int pos = termDocs.doc();
                // use FieldSelector for more efficient loading of Fields.
                // load only what's needed to determine a leading document
                Document d = reader.document(pos, new FieldSelector() {
                    private static final long serialVersionUID = 1426724242925499003L;

                    public FieldSelectorResult accept(String field) {
                        if (field.equals(DocumentCreator.FIELD_INDEX_TYPE)) {
                            return FieldSelectorResult.LOAD_AND_BREAK;
                        } else {
                            return FieldSelectorResult.NO_LOAD;
                        }
                    }
                });
                String[] values = d.getValues(DocumentCreator.FIELD_INDEX_TYPE);
                if (values != null) {
                    List<String> vList = Arrays.asList(values);
                    if (vList.contains(DocumentCreator.INDEX_TYPE_LEAD) == true) {
                        leadDoc = reader.document(pos);
                        found = true;
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error("FindLeadDocument failed to find doc: " + urlHash + ", exception=" + e);
    } finally {
        try {
            if (termDocs != null) {
                termDocs.close();
            }
            if (reader != null) {
                reader.close();
            }
        } catch (Exception e) {
            logger.error("FindLeadDocument can't close reader or termDocs: " + e);
        }
    }
    return leadDoc;
}

From source file:com.gnizr.core.search.SearchIndexManager.java

License:Mozilla Public License

/**
 * Finds a non-representative bookmark document for a given URL hash.
 * /*from   w w w  .j  a v  a2  s.  c om*/
 * @param urlHash
 *            a URL MD5 Hash.
 * 
 * @return a Lucene document of a non-representative bookmark
 */
public Document findNonLeadDocument(String urlHash) {
    IndexReader reader = null;
    TermDocs termDocs = null;
    Document leadDoc = null;
    try {
        boolean exists = IndexReader.indexExists(indexDirectory);
        if (exists == true) {
            reader = IndexReader.open(indexDirectory);
            Term key = new Term(DocumentCreator.FIELD_URL_MD5, urlHash);
            termDocs = reader.termDocs(key);
            boolean found = false;
            while (termDocs.next() && found == false) {
                int pos = termDocs.doc();
                // use FieldSelector for more efficient loading of Fields.
                // load only what's needed to determine a leading document
                Document d = reader.document(pos, new FieldSelector() {
                    private static final long serialVersionUID = 1426724242925499003L;

                    public FieldSelectorResult accept(String field) {
                        if (field.equals(DocumentCreator.FIELD_INDEX_TYPE)) {
                            return FieldSelectorResult.LOAD_AND_BREAK;
                        } else {
                            return FieldSelectorResult.NO_LOAD;
                        }
                    }

                });
                String[] values = d.getValues(DocumentCreator.FIELD_INDEX_TYPE);
                if (values != null) {
                    List<String> vList = Arrays.asList(values);
                    if (vList.contains(DocumentCreator.INDEX_TYPE_LEAD) == false) {
                        leadDoc = reader.document(pos);
                        found = true;
                    }
                } else {
                    leadDoc = reader.document(pos);
                    found = true;
                }
            }
        }
    } catch (Exception e) {
        logger.error("FindLeadDocument failed to find doc hash: " + urlHash + ", exception=" + e);
    } finally {
        try {
            if (termDocs != null) {
                termDocs.close();
            }
            if (reader != null) {
                reader.close();
            }
        } catch (Exception e) {
            logger.error("FindLeadDocument can't close reader or termDocs: " + e);
        }
    }
    return leadDoc;
}

From source file:com.nearinfinity.blur.manager.IndexManager.java

License:Apache License

public static void fetchRow(IndexReader reader, String table, Selector selector, FetchResult fetchResult)
        throws CorruptIndexException, IOException {
    fetchResult.table = table;//w ww. j a  v  a 2 s.co m
    String locationId = selector.locationId;
    int lastSlash = locationId.lastIndexOf('/');
    int docId = Integer.parseInt(locationId.substring(lastSlash + 1));
    if (docId >= reader.maxDoc()) {
        throw new RuntimeException("Location id [" + locationId + "] with docId [" + docId + "] is not valid.");
    }
    if (selector.isRecordOnly()) {
        // select only the row for the given data or location id.
        if (reader.isDeleted(docId)) {
            fetchResult.exists = false;
            fetchResult.deleted = true;
            return;
        } else {
            fetchResult.exists = true;
            fetchResult.deleted = false;
            Document document = reader.document(docId, getFieldSelector(selector));
            fetchResult.recordResult = getColumns(document);
            return;
        }
    } else {
        if (reader.isDeleted(docId)) {
            fetchResult.exists = false;
            fetchResult.deleted = true;
            return;
        } else {
            fetchResult.exists = true;
            fetchResult.deleted = false;
            String rowId = getRowId(reader, docId);
            TermDocs termDocs = reader.termDocs(new Term(ROW_ID, rowId));
            fetchResult.rowResult = new FetchRowResult(
                    getRow(new TermDocIterable(termDocs, reader, getFieldSelector(selector))));
            return;
        }
    }
}

From source file:com.nearinfinity.blur.manager.IndexManager.java

License:Apache License

private static String getRowId(IndexReader reader, int docId) throws CorruptIndexException, IOException {
    Document document = reader.document(docId, new FieldSelector() {
        private static final long serialVersionUID = 4912420100148752051L;

        @Override//from   ww w .ja va 2  s  . c o  m
        public FieldSelectorResult accept(String fieldName) {
            if (ROW_ID.equals(fieldName)) {
                return FieldSelectorResult.LOAD_AND_BREAK;
            }
            return FieldSelectorResult.NO_LOAD;
        }
    });
    return document.get(ROW_ID);
}

From source file:com.o19s.solr.swan.highlight.BaseFragmentsBuilder.java

License:Apache License

protected Field[] getFields(IndexReader reader, int docId, final String fieldName) throws IOException {
    // according to javadoc, doc.getFields(fieldName) cannot be used with lazy loaded field???
    final List<Field> fields = new ArrayList<Field>();
    reader.document(docId, new StoredFieldVisitor() {

        @Override/*w  w w .  jav a 2 s  .com*/
        public void stringField(FieldInfo fieldInfo, String value) {
            FieldType ft = new FieldType(TextField.TYPE_STORED);
            ft.setStoreTermVectors(fieldInfo.hasVectors());
            fields.add(new Field(fieldInfo.name, value, ft));
        }

        @Override
        public Status needsField(FieldInfo fieldInfo) {
            return fieldInfo.name.equals(fieldName) ? Status.YES : Status.NO;
        }
    });
    return fields.toArray(new Field[fields.size()]);
}