Java Code Examples for org.apache.lucene.document.Field#stringValue()

The following examples show how to use org.apache.lucene.document.Field#stringValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MatchingField.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @return value most responsible for this document being a match
 */
public String getFieldValue() {
    String fieldName = getFieldName();
    Field f = doc.getField(fieldName);
    if (f == null) {
        StringBuffer sb = new StringBuffer();
        sb.append("[length=" + terms.length + ";  ");
        for (Object o : terms) {
            sb.append(o + ", ");
        }
        sb.append("]");
        log.info("Unable to get matchingFieldValue for field : " + fieldName +
                " with query: " + query + ", and terms = " + sb.toString());
        log.info("Document = " + doc);
        return "";
    }
    String value = f.stringValue();
    if (needNumberToolsAdjust.containsKey(fieldName)) {
        Long temp = NumberTools.stringToLong(value);
        value = temp.toString();
    }
    return value;
}
 
Example 2
Source File: LuceneResultSetRow.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public QName getQName()
{
    Field field = getDocument().getField("QNAME");
    if (field != null)
    {
        String qname = field.stringValue();
        if((qname == null) || (qname.length() == 0))
        {
            return null;
        }
        else
        {
           return QName.createQName(qname);
        }
    }
    else
    {
        return null;
    }
}
 
Example 3
Source File: MatchingField.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @return value most responsible for this document being a match
 */
public String getFieldValue() {
    String fieldName = getFieldName();
    Field f = doc.getField(fieldName);
    if (f == null) {
        StringBuffer sb = new StringBuffer();
        sb.append("[length=" + terms.length + ";  ");
        for (Object o : terms) {
            sb.append(o + ", ");
        }
        sb.append("]");
        log.info("Unable to get matchingFieldValue for field : " + fieldName +
                " with query: " + query + ", and terms = " + sb.toString());
        log.info("Document = " + doc);
        return "";
    }
    String value = f.stringValue();
    if (needNumberToolsAdjust.containsKey(fieldName)) {
        Long temp = NumberTools.stringToLong(value);
        value = temp.toString();
    }
    return value;
}
 
Example 4
Source File: SecureRealTimeGetComponent.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
/**
 * @param doc SolrDocument to check
 * @param idField field where the id is stored
 * @param fieldType type of id field
 * @param filterQuery Query to filter by
 * @param searcher SolrIndexSearcher on which to apply the filter query
 * @returns the internal docid, or -1 if doc is not found or doesn't match filter
 */
private static int getFilteredInternalDocId(SolrDocument doc, SchemaField idField, FieldType fieldType,
      Query filterQuery, SolrIndexSearcher searcher) throws IOException {
  int docid = -1;
  Field f = (Field)doc.getFieldValue(idField.getName());
  String idStr = f.stringValue();
  BytesRef idBytes = new BytesRef();
  fieldType.readableToIndexed(idStr, idBytes);
  // get the internal document id
  long segAndId = searcher.lookupId(idBytes);

    // if docid is valid, run it through the filter
  if (segAndId >= 0) {
    int segid = (int) segAndId;
    AtomicReaderContext ctx = searcher.getTopReaderContext().leaves().get((int) (segAndId >> 32));
    docid = segid + ctx.docBase;
    Weight weight = filterQuery.createWeight(searcher);
    Scorer scorer = weight.scorer(ctx, null);
    if (scorer == null || segid != scorer.advance(segid)) {
      // filter doesn't match.
      docid = -1;
    }
  }
  return docid;
}
 
Example 5
Source File: LuceneResultSetRow.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public QName getPrimaryAssocTypeQName()
{
    
    Field field = getDocument().getField("PRIMARYASSOCTYPEQNAME");
    if (field != null)
    {
        String qname = field.stringValue();
        return QName.createQName(qname);
    }
    else
    {
        return ContentModel.ASSOC_CHILDREN;
    }
}
 
Example 6
Source File: LuceneResultSetRow.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public ChildAssociationRef getChildAssocRef()
{
    Field field = getDocument().getField("PRIMARYPARENT");
    String primaryParent = null;
    if (field != null)
    {
        primaryParent = field.stringValue();
    }
    NodeRef childNodeRef = getNodeRef();
    NodeRef parentNodeRef = primaryParent == null ? null : tenantService.getBaseName(new NodeRef(primaryParent));
    return new ChildAssociationRef(getPrimaryAssocTypeQName(), parentNodeRef, getQName(), childNodeRef);
}
 
Example 7
Source File: ReferenceCountingReadOnlyIndexReaderFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String getPathLinkId(int n) throws IOException
{
    Document document = document(n, new SingleFieldSelector("ID", true));
    Field[] fields = document.getFields("ID");
    Field field = fields[fields.length - 1];
    return (field == null) ? null : field.stringValue();
}
 
Example 8
Source File: ReferenceCountingReadOnlyIndexReaderFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String getPath(int n) throws IOException
{
    // return getStringValue(n, "PATH");
    Document d = document(n, new SingleFieldSelector("PATH", true));
    Field f = d.getField("PATH");
    return f == null ? null : f.stringValue();
}
 
Example 9
Source File: ReferenceCountingReadOnlyIndexReaderFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String getType(int n) throws IOException
{
    // return getStringValue(n, "TYPE");
    Document d = document(n, new SingleFieldSelector("TYPE", true));
    Field f = d.getField("TYPE");
    return f == null ? null : f.stringValue();
}
 
Example 10
Source File: RealTimeGetComponent.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static SolrInputDocument toSolrInputDocument(SolrDocument doc, IndexSchema schema) {
  SolrInputDocument out = new SolrInputDocument();
  for( String fname : doc.getFieldNames() ) {
    boolean fieldArrayListCreated = false;
    SchemaField sf = schema.getFieldOrNull(fname);
    if (sf != null) {
      if ((!sf.hasDocValues() && !sf.stored()) || schema.isCopyFieldTarget(sf)) continue;
    }
    for (Object val: doc.getFieldValues(fname)) {
      if (val instanceof Field) {
        Field f = (Field) val;
        if (sf != null) {
          val = sf.getType().toObject(f);   // object or external string?
        } else {
          val = f.stringValue();
          if (val == null) val = f.numericValue();
          if (val == null) val = f.binaryValue();
          if (val == null) val = f;
        }
      } else if(val instanceof SolrDocument) {
        val = toSolrInputDocument((SolrDocument) val, schema);
        if(!fieldArrayListCreated && doc.getFieldValue(fname) instanceof Collection) {
          // previous value was array so we must return as an array even if was a single value array
          out.setField(fname, Lists.newArrayList(val));
          fieldArrayListCreated = true;
          continue;
        }
      }
      out.addField(fname, val);
    }
  }
  return out;
}
 
Example 11
Source File: DocumentUtil.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public String convert(Document doc) {
    Field field = doc.getField(FIELD_SOURCE);
    return field == null ? null : field.stringValue();
}
 
Example 12
Source File: ReferenceCountingReadOnlyIndexReaderFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public String getIsCategory(int n) throws IOException
{
    Document d = document(n, new SingleFieldSelector("ISCATEGORY", true));
    Field f = d.getField("ISCATEGORY");
    return f == null ? null : f.stringValue();
}
 
Example 13
Source File: ReferenceCountingReadOnlyIndexReaderFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private String getStringValue(int n, String fieldName) throws IOException
{
    Document document = document(n);
    Field field = document.getField(fieldName);
    return (field == null) ? null : field.stringValue();
}
 
Example 14
Source File: JsonPreAnalyzedParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public String toFormattedString(Field f) throws IOException {
  Map<String,Object> map = new LinkedHashMap<>();
  map.put(VERSION_KEY, VERSION);
  if (f.fieldType().stored()) {
    String stringValue = f.stringValue();
    if (stringValue != null) {
      map.put(STRING_KEY, stringValue);
    }
    BytesRef binaryValue = f.binaryValue();
    if (binaryValue != null) {
      map.put(BINARY_KEY, Base64.byteArrayToBase64(binaryValue.bytes, binaryValue.offset, binaryValue.length));
    }
  }
  TokenStream ts = f.tokenStreamValue();
  if (ts != null) {
    List<Map<String,Object>> tokens = new LinkedList<>();
    while (ts.incrementToken()) {
      Iterator<Class<? extends Attribute>> it = ts.getAttributeClassesIterator();
      String cTerm = null;
      String tTerm = null;
      Map<String,Object> tok = new TreeMap<>();
      while (it.hasNext()) {
        Class<? extends Attribute> cl = it.next();
        Attribute att = ts.getAttribute(cl);
        if (att == null) {
          continue;
        }
        if (cl.isAssignableFrom(CharTermAttribute.class)) {
          CharTermAttribute catt = (CharTermAttribute)att;
          cTerm = new String(catt.buffer(), 0, catt.length());
        } else if (cl.isAssignableFrom(TermToBytesRefAttribute.class)) {
          TermToBytesRefAttribute tatt = (TermToBytesRefAttribute)att;
          tTerm = tatt.getBytesRef().utf8ToString();
        } else {
          if (cl.isAssignableFrom(FlagsAttribute.class)) {
            tok.put(FLAGS_KEY, Integer.toHexString(((FlagsAttribute)att).getFlags()));
          } else if (cl.isAssignableFrom(OffsetAttribute.class)) {
            tok.put(OFFSET_START_KEY, ((OffsetAttribute)att).startOffset());
            tok.put(OFFSET_END_KEY, ((OffsetAttribute)att).endOffset());
          } else if (cl.isAssignableFrom(PayloadAttribute.class)) {
            BytesRef p = ((PayloadAttribute)att).getPayload();
            if (p != null && p.length > 0) {
              tok.put(PAYLOAD_KEY, Base64.byteArrayToBase64(p.bytes, p.offset, p.length));
            }
          } else if (cl.isAssignableFrom(PositionIncrementAttribute.class)) {
            tok.put(POSINCR_KEY, ((PositionIncrementAttribute)att).getPositionIncrement());
          } else if (cl.isAssignableFrom(TypeAttribute.class)) {
            tok.put(TYPE_KEY, ((TypeAttribute)att).type());
          } else {
            tok.put(cl.getName(), att.toString());
          }
        }
      }
      String term = null;
      if (cTerm != null) {
        term = cTerm;
      } else {
        term = tTerm;
      }
      if (term != null && term.length() > 0) {
        tok.put(TOKEN_KEY, term);
      }
      tokens.add(tok);
    }
    map.put(TOKENS_KEY, tokens);
  }
  return JSONUtil.toJSON(map, -1);
}
 
Example 15
Source File: SimplePreAnalyzedParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public String toFormattedString(Field f) throws IOException {
  StringBuilder sb = new StringBuilder();
  sb.append(VERSION + " ");
  if (f.fieldType().stored()) {
    String s = f.stringValue();
    if (s != null) {
      // encode the equals sign
      s = s.replaceAll("=", "\\=");
      sb.append('=');
      sb.append(s);
      sb.append('=');
    }
  }
  TokenStream ts = f.tokenStreamValue();
  if (ts != null) {
    StringBuilder tok = new StringBuilder();
    boolean next = false;
    while (ts.incrementToken()) {
      if (next) {
        sb.append(' ');
      } else {
        next = true;
      }
      tok.setLength(0);
      Iterator<Class<? extends Attribute>> it = ts.getAttributeClassesIterator();
      String cTerm = null;
      String tTerm = null;
      while (it.hasNext()) {
        Class<? extends Attribute> cl = it.next();
        Attribute att = ts.getAttribute(cl);
        if (att == null) {
          continue;
        }
        if (cl.isAssignableFrom(CharTermAttribute.class)) {
          CharTermAttribute catt = (CharTermAttribute)att;
          cTerm = escape(catt.buffer(), catt.length());
        } else if (cl.isAssignableFrom(TermToBytesRefAttribute.class)) {
          TermToBytesRefAttribute tatt = (TermToBytesRefAttribute)att;
          char[] tTermChars = tatt.getBytesRef().utf8ToString().toCharArray();
          tTerm = escape(tTermChars, tTermChars.length);
        } else {
          if (tok.length() > 0) tok.append(',');
          if (cl.isAssignableFrom(FlagsAttribute.class)) {
            tok.append("f=").append(Integer.toHexString(((FlagsAttribute) att).getFlags()));
          } else if (cl.isAssignableFrom(OffsetAttribute.class)) {
            tok.append("s=").append(((OffsetAttribute) att).startOffset()).append(",e=").append(((OffsetAttribute) att).endOffset());
          } else if (cl.isAssignableFrom(PayloadAttribute.class)) {
            BytesRef p = ((PayloadAttribute)att).getPayload();
            if (p != null && p.length > 0) {
              tok.append("p=").append(bytesToHex(p.bytes, p.offset, p.length));
            } else if (tok.length() > 0) {
              tok.setLength(tok.length() - 1); // remove the last comma
            }
          } else if (cl.isAssignableFrom(PositionIncrementAttribute.class)) {
            tok.append("i=").append(((PositionIncrementAttribute) att).getPositionIncrement());
          } else if (cl.isAssignableFrom(TypeAttribute.class)) {
            tok.append("y=").append(escape(((TypeAttribute) att).type()));
          } else {
            
            tok.append(cl.getName()).append('=').append(escape(att.toString()));
          }
        }
      }
      String term = null;
      if (cTerm != null) {
        term = cTerm;
      } else {
        term = tTerm;
      }
      if (term != null && term.length() > 0) {
        if (tok.length() > 0) {
          tok.insert(0, term + ",");
        } else {
          tok.insert(0, term);
        }
      }
      sb.append(tok);
    }
  }
  return sb.toString();
}