public static boolean isLocal(@NonNull final Document doc) { Fieldable fld = doc.getFieldable(FIELD_BINARY_NAME); if (fld == null) { return false; } else { final String binName = fld.stringValue(); switch (binName.charAt(binName.length()-1)) { case EK_LOCAL_ANNOTATION: case EK_LOCAL_CLASS: case EK_LOCAL_ENUM: case EK_LOCAL_INTERFACE: return true; default: return false; } } }
public SearchItem toResult(int documentId) throws IOException { Document document = searcher.doc(documentId); String type = document.getFieldable(FieldNames.TYPE).stringValue(); NumericField date = (NumericField)document.getFieldable(FieldNames.DATE); Fieldable path = document.getFieldable(FieldNames.PATH); NumericField version = (NumericField)document.getFieldable(FieldNames.VERSION); return new SearchItem ( Integer.parseInt(type), path.stringValue(), (version != null) ? version.getNumericValue().intValue() : -1, new Date(date.getNumericValue().longValue()) ); }
private long getLargestTxIdByCommitTime(SolrIndexReader reader, Long lastTxCommitTimeBeforeHoles) throws IOException { long txid = -1; if (lastTxCommitTimeBeforeHoles != -1) { TermDocs docs = reader.termDocs(new Term(QueryConstants.FIELD_TXCOMMITTIME, NumericEncoder .encode(lastTxCommitTimeBeforeHoles))); while (docs.next()) { Document doc = reader.document(docs.doc()); Fieldable field = doc.getFieldable(QueryConstants.FIELD_TXID); if (field != null) { long currentTxId = Long.valueOf(field.stringValue()); if (currentTxId > txid) { txid = currentTxId; } } } } return txid; }
private long getLargestChangeSetIdByCommitTime(SolrIndexReader reader, Long lastChangeSetCommitTimeBeforeHoles) throws IOException { long txid = -1; if (lastChangeSetCommitTimeBeforeHoles != -1) { TermDocs docs = reader.termDocs(new Term(QueryConstants.FIELD_ACLTXCOMMITTIME, NumericEncoder .encode(lastChangeSetCommitTimeBeforeHoles))); while (docs.next()) { Document doc = reader.document(docs.doc()); Fieldable field = doc.getFieldable(QueryConstants.FIELD_ACLTXID); if (field != null) { long currentTxId = Long.valueOf(field.stringValue()); if (currentTxId > txid) { txid = currentTxId; } } } } return txid; }
/** * Makes the fieldable to index. * @param value the value * @return the fieldable */ @Override protected Fieldable makeFieldable(Object value) { Double dValue = null; if (value instanceof Double) { dValue = (Double)value; } else if (value instanceof String) { try { PropertyValueType valueType = PropertyValueType.DOUBLE; dValue = (Double)valueType.evaluate((String)value); } catch (IllegalArgumentException e) {} } if (dValue != null) { boolean bIndex = !this.getIndexingOption().equals(Field.Index.NO); NumericField fld = new NumericField(this.getName(),this.getStorageOption(),bIndex); fld.setDoubleValue(dValue); return fld; } else { return null; } }
/** * Makes the fieldable to index. * @param value the value * @return the fieldable */ protected Fieldable makeFieldable(Object value) { Timestamp tsValue = null; if (value instanceof Timestamp) { tsValue = (Timestamp)value; } else if (value instanceof String) { try { PropertyValueType valueType = PropertyValueType.TIMESTAMP; tsValue = (Timestamp)valueType.evaluate((String)value); } catch (IllegalArgumentException e) {} } if (tsValue != null) { boolean bIndex = !this.getIndexingOption().equals(Field.Index.NO); NumericField fld = new NumericField(this.getName(),this.getStorageOption(),bIndex); fld.setLongValue(tsValue.getTime()); return fld; } else { return null; } }
/** * Makes the fieldable to index. * @param value the value * @return the fieldable */ @Override protected Fieldable makeFieldable(Object value) { Long lValue = null; if (value instanceof Long) { lValue = (Long)value; } else if (value instanceof String) { try { PropertyValueType valueType = PropertyValueType.LONG; lValue = (Long)valueType.evaluate((String)value); } catch (IllegalArgumentException e) {} } if (lValue != null) { boolean bIndex = !this.getIndexingOption().equals(Field.Index.NO); NumericField fld = new NumericField(this.getName(),this.getStorageOption(),bIndex); fld.setLongValue(lValue); return fld; } else { return null; } }
private void removeFromCache( long entityId ) throws IOException, CorruptIndexException { IndexSearcher searcher = searcher(); Query query = type.idTermQuery( entityId ); TopDocs docs = searcher.search( query, 1 ); if ( docs.totalHits > 0 ) { Document document = searcher.doc( docs.scoreDocs[0].doc ); for ( Fieldable field : document.getFields() ) { String key = field.name(); Object value = field.stringValue(); removeFromCache( entityId, key, value ); } } }
@Override final public LinkedHashMap<String, FieldValue> getDocumentStoredField(final int docId) throws IOException { LinkedHashMap<String, FieldValue> documentFields = new LinkedHashMap<String, FieldValue>(); Document doc = indexReader.document(docId, FieldSelectors.LoadFieldSelector.INSTANCE); String currentFieldName = null; FieldValue currentFieldValue = null; for (Fieldable field : doc.getFields()) { if (!field.isStored()) continue; FieldValue fieldValue = null; String fieldName = field.name(); if (currentFieldName != null && currentFieldName.equals(fieldName)) fieldValue = currentFieldValue; else { fieldValue = documentFields.get(fieldName); if (fieldValue == null) { fieldValue = new FieldValue(fieldName); documentFields.put(fieldName, fieldValue); } currentFieldName = fieldName; currentFieldValue = fieldValue; } currentFieldValue.addValue(new FieldValueItem(FieldValueOriginEnum.STORAGE, field.stringValue())); } return documentFields; }
@Override public String getValue(String key) { for (Fieldable field : fields) { if (field.name().equals(key)) { return field.stringValue(); } } return null; }
@Override public String[] getValues(String key) { final List<String> result = new ArrayList<String>(); for (Fieldable field : fields) { if (field.name().equals(key)) { result.add(field.stringValue()); } } return result.toArray(result.isEmpty() ? EMPTY : new String[result.size()]); }
public List<Fieldable> getBasicFields() { if( basicFields == null ) { basicFields = standardIndexer.getBasicFields(this); } return basicFields; }
public List<Fieldable> queryACLEntries(Object domainObject, String privilege, String prefix) { List<Fieldable> fields = new ArrayList<Fieldable>(); List<ACLEntryMapping> allEntriesForObject = aclManager.getAllEntriesForObject(domainObject, privilege); int i = 0; for( ACLEntryMapping entry : allEntriesForObject ) { char grant = entry.getGrant(); boolean all = entry.getExpression().equals(SecurityConstants.getRecipient(Recipient.EVERYONE)); if( all && grant == SecurityConstants.REVOKE ) { break; } fields.add(AbstractIndexingExtension.keyword(prefix + entry.getId(), String.format("%03d%c", i, grant))); //$NON-NLS-1$ if( all ) { break; } i++; } return fields; }
@Override public void indexFast(IndexedItem indexedItem) { Item item = indexedItem.getItem(); List<ActivateRequest> activateRequests = activationService.getAllRequests(item); Document itemdoc = indexedItem.getItemdoc(); List<Fieldable> basicFields = indexedItem.getBasicFields(); List<Document> activationDocs = indexedItem.getDocumentsForIndex(ActivationConstants.ACTIVATION_INDEX_ID); for( ActivateRequest request : activateRequests ) { if( request.getCourse() != null ) { itemdoc.add(keyword(FreeTextQuery.FIELD_COURSE_ID, request.getCourse().getUuid())); Document doc = new Document(); addAllFields(doc, basicFields); doc.add(keyword(FreeTextQuery.FIELD_COURSE_ID, request.getCourse().getUuid())); doc.add(keyword(FreeTextQuery.FIELD_ACTIVATION_ID, Long.toString(request.getId()))); doc.add(keyword(FreeTextQuery.FIELD_ACTIVATION_STATUS, String.valueOf(request.getStatus()))); doc.add(indexed(FreeTextQuery.FIELD_OWNER, request.getUser())); doc.removeField(FreeTextQuery.FIELD_REALLASTMODIFIED); doc.add(keyword(FreeTextQuery.FIELD_REALLASTMODIFIED, new UtcDate(request.getTime()).format(Dates.ISO))); doc.add(indexed(FreeTextQuery.FIELD_ACTIVATION_FROM, new UtcDate(request.getFrom()).format(Dates.ISO))); doc.add(indexed(FreeTextQuery.FIELD_ACTIVATION_UNTIL, new UtcDate(request.getUntil()).format(Dates.ISO))); addAllFields(doc, indexedItem.getACLEntries(ActivationConstants.VIEW_ACTIVATION_ITEM)); activationDocs.add(doc); } } }
public void addAllFields(Document doc, List<Fieldable> fields) { for( Fieldable field : fields ) { doc.add(field); } }
private void ensureIndxedFiles() { if (indexedFiles != null) { return; } indexedFiles = new LinkedHashSet<String>(); for (Fieldable f : doc.getFieldables(INDEXED_FILE_FIELD)) { String v = f.stringValue(); indexedFiles.add(v); } }
private void addWaveFieldsToIndex(WaveId waveId, WaveViewData waveData, Document doc) throws WaveletStateException { WaveDigestWithSupplements digestWithSupplements = digester.generateDigestWithSupplements(waveData); WaveDigest digest = digestWithSupplements.getDigest(); Map<ParticipantId, WaveDigestSupplement> supplements = digestWithSupplements.getSupplements(); if (digest.getParticipants().size() > 0) { addField(doc, IndexCondition.Field.WAVE_ID, digest.getWaveId()); addField(doc, IndexCondition.Field.CREATOR, digest.getCreator()); for (String participant : digest.getParticipants()) { doc.add(new Field(IndexCondition.Field.PARTICIPANTS.toString(), participant, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); } addField(doc, IndexCondition.Field.TITLE, digest.getTitle()); addField(doc, IndexCondition.Field.CONTENT, digest.getTitle()); addField(doc, IndexCondition.Field.SNIPPET, digest.getSnippet()); addField(doc, IndexCondition.Field.BLIP_COUNT, Integer.toString(digest.getBlipCount())); for (ReadableWaveletData wavelet : waveData.getWavelets()) { if (IdUtil.isConversationalId(wavelet.getWaveletId()) || IdUtil.isConversationalId(wavelet.getWaveletId())) { for (String tag : wavelet.getTags()) { addField(doc, IndexCondition.Field.TAG, tag); addField(doc, IndexCondition.Field.CONTENT, tag); } addField(doc, IndexCondition.Field.CONTENT, textCollator.collateTextForWavelet(wavelet)); } } addField(doc, IndexCondition.Field.CREATED, Long.toString(digest.getCreated())); addField(doc, IndexCondition.Field.LAST_MODIFIED, Long.toString(digest.getLastModified())); for (ParticipantId participantId : supplements.keySet()) { WaveDigestSupplement supplement = supplements.get(participantId); addField(doc, IndexCondition.Field.IN_, IndexCondition.Field.IN_.toString() + participantId.getAddress(), supplement.getFolder()); addField(doc, IndexCondition.Field.UNREAD_COUNT_, IndexCondition.Field.UNREAD_COUNT_.toString() + participantId.getAddress(), Integer.toString(supplement.getUnreadCount())); } LOG.fine("Write index for wave " + waveId.serialise()); for (Fieldable field : doc.getFields()) { LOG.fine(" " + field.name() + " : " + field.stringValue()); } } }
@Override protected void onRecord(DiscoveredRecord record, Document document) { Map<String, List<String>> fieldMap = new HashMap<String, List<String>>(); for (Fieldable field : document.getFields()) { String name = field.name(); List<String> fieldValues = fieldMap.get(name); if (fieldValues == null) { fieldValues = new ArrayList<String>(); fieldMap.put(name, fieldValues); } fieldValues.add(field.stringValue()); } mapping.put(record, fieldMap); }
/** * Appends the field to a document prior to writing the document to the index. * <p/> * The field will not be appended if it's name or value is empty. * @param document the Lucene document * @param value the input value to write */ protected void appendForWrite(Document document, Object value) { String sName = getName(); Fieldable fld = null; String sValueInput = null; if (value != null) { sValueInput = value.toString(); fld = this.makeFieldable(value); } if (sName.length() == 0) { LOGGER.fine("The field has not been named and will not be stored."); } else if (fld == null) { LOGGER.log(Level.FINER, "{0} has an empty value and will not be stored.", sName); } else { if (fld != null) { if (LOGGER.isLoggable(Level.FINER)) { String sTmp = fld.stringValue(); if (sTmp.length() > 101) sTmp = sTmp.substring(0,101)+" ..."; StringBuilder sb = new StringBuilder(); sb.append("Appending field:\n "); sb.append(" name=\"").append(fld.name()).append("\""); sb.append(" stored=\"").append(fld.isStored()).append("\""); sb.append(" indexed=\"").append(fld.isIndexed()).append("\""); sb.append(" analyzed=\"").append(fld.isTokenized()).append("\""); sb.append(" termVector=\"").append(fld.isTermVectorStored()).append("\""); sb.append("\n storeValue=\"").append(sTmp).append("\""); if ((sValueInput != null) && !sValueInput.equals(fld.stringValue())) { sTmp = sValueInput; if (sTmp.length() > 101) sTmp = sTmp.substring(0,101)+" ..."; sb.append("\n inputValue=\"").append(sValueInput).append("\""); } LOGGER.finer(sb.toString()); } document.add(fld); } } }
/** * Makes the fieldable to index. * <p/> * Sub-classes should override this method if converted values need to * be written for subsequent search (ex. Doubles, Longs, Timestamps ...). * <p/> * The default behavior is to return the supplied value. * <br/>Null or empty values will not be stored. * @param value the value * @return the fieldable */ protected Fieldable makeFieldable(Object value) { if ((value != null) && (value instanceof String)) { String sValue = (String)value; if (sValue.length() > 0) { Field.Store storage = getStorageOption(); Field.Index indexing = getIndexingOption(); Field.TermVector termVector = getTermVectorOption(); return new Field(this.getName(),sValue,storage,indexing,termVector); } } return null; }
/** * Makes the fieldable to index. * @param value the value * @return the fieldable */ @Override protected Fieldable makeFieldable(Object value) { if ((value != null) && (value instanceof String)) { String sValue = ((String)value).toLowerCase(); if (sValue.length() > 0) { Field.Store storage = getStorageOption(); Field.Index indexing = getIndexingOption(); Field.TermVector termVector = getTermVectorOption(); return new Field(this.getName(),sValue,storage,indexing,termVector); } } return null; }
/** * 将lucene的document文档转换成相应的pojo类 <br> * 遵守一一对应原则 * * @param doc * @return */ public static <T> T document2Pojo(Document doc,Class<T> clazz) { List<Fieldable> fields = doc.getFields(); Integer fieldsLength = fields.size(); String fieldName = null; T t = null; try { t = clazz.newInstance(); Method[] methods = clazz.getDeclaredMethods(); String methodName = null; String menberName = null; Integer methodsLength = methods.length; String fieldValue = null; for (int i = 0; i < fieldsLength; i++) { fieldName = fields.get(i).name(); fieldValue = fields.get(i).stringValue(); for (int j = 0; j < methodsLength; j++) { methodName = methods[j].getName(); if (methodName.startsWith("set")) { menberName = Introspector.decapitalize(methodName .substring(3, methodName.length())); if (menberName.equals(fieldName)) { methods[j].invoke( t, typeConvertorForString(fieldValue, methods[j].getParameterTypes()[0])); break; } } } } } catch (Exception e) { e.printStackTrace(); } return t; }
static boolean documentIsEmpty( Document document ) { List<Fieldable> fields = document.getFields(); for ( Fieldable field : fields ) { if ( !LuceneIndex.KEY_DOC_ID.equals( field.name() ) ) { return false; } } return true; }
Fieldable instantiateField( String key, Object value, Index analyzed ) { Fieldable field = null; if ( value instanceof Number ) { Number number = (Number) value; NumericField numberField = new NumericField( key, Store.YES, true ); if ( value instanceof Long ) { numberField.setLongValue( number.longValue() ); } else if ( value instanceof Float ) { numberField.setFloatValue( number.floatValue() ); } else if ( value instanceof Double ) { numberField.setDoubleValue( number.doubleValue() ); } else { numberField.setIntValue( number.intValue() ); } field = numberField; } else { field = new Field( key, value.toString(), Store.YES, analyzed ); } return field; }
final public static FieldValueItem[] buildArray(Fieldable[] fieldables) { FieldValueItem[] array = new FieldValueItem[fieldables.length]; int i = 0; for (Fieldable fieldable : fieldables) array[i++] = new FieldValueItem(fieldable); return array; }
protected boolean add( Document doc, Fieldable field ) { if (field instanceof StoredField) { StoredField stored = (StoredField) field; if (!stored.isEmpty()) { doc.add(field); return true; } } else { doc.add(field); return true; } return false; }
public static String getSimpleName(final Document doc) { final Fieldable field = doc.getFieldable(FIELD_SIMPLE_NAME); return field == null ? null : field.stringValue(); }
@SuppressWarnings("deprecation") //NOI18N private static Fieldable sourceNameField(String relativePath) { return new Field(FIELD_PRIMARY_KEY, relativePath, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS); }
public List<Fieldable> getFields() { return fields; }
private Fieldable sourceNameField(@NonNull String primaryKey) { return new Field(FIELD_PRIMARY_KEY, primaryKey, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS); }
ReusableIndexDocument(@NonNull final MemIndexDocument memDoc) { Parameters.notNull("memDoc", memDoc); //NOI18N for (Fieldable field : memDoc.getFields()) { doc.add(field); } }
public List<Fieldable> getACLEntries(String privilege) { return aclMap.get(privilege); }
private void indexMimeEntry(String mimeEntry, List<Fieldable> fields) { fields.add(AbstractIndexingExtension.indexed(FreeTextQuery.FIELD_ATTACHMENT_MIME_TYPES, mimeEntry)); }
@Override public void indexSlow(IndexedItem indexedItem) { List<Fieldable> fields = textExtracter.indexAttachments(indexedItem, freetextIndex.getSearchSettings()); addAllFields(indexedItem.getItemdoc(), fields); }