org.apache.lucene.index.IndexableField#name ( )源码实例Demo

下面列出了org.apache.lucene.index.IndexableField#name ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public void prepare(PercolateContext context, ParsedDocument parsedDocument) {
    MemoryIndex memoryIndex = cache.get();
    for (IndexableField field : parsedDocument.rootDoc().getFields()) {
        if (field.fieldType().indexOptions() == IndexOptions.NONE && field.name().equals(UidFieldMapper.NAME)) {
            continue;
        }
        try {
            Analyzer analyzer = context.mapperService().documentMapper(parsedDocument.type()).mappers().indexAnalyzer();
            // TODO: instead of passing null here, we can have a CTL<Map<String,TokenStream>> and pass previous,
            // like the indexer does
            try (TokenStream tokenStream = field.tokenStream(analyzer, null)) {
                if (tokenStream != null) {
                    memoryIndex.addField(field.name(), tokenStream, field.boost());
                }
             }
        } catch (Exception e) {
            throw new ElasticsearchException("Failed to create token stream for [" + field.name() + "]", e);
        }
    }
    context.initialize(new DocEngineSearcher(memoryIndex), parsedDocument);
}
 
源代码2 项目: lucene-solr   文件: TestBlockPostingsFormat2.java
/** tests terms with ttf % blocksize = 0 */
public void testTTFBlockSizeMultiple() throws Exception {
  Document doc = newDocument();
  for (int i = 0; i < Lucene50PostingsFormat.BLOCK_SIZE/2; i++) {
    for (IndexableField f : doc.getFields()) {
      String proto = (f.name() + " " + f.name() + " " + f.name() + " " + f.name() + " " 
                     + f.name() + "_2 " + f.name() + "_2 " + f.name() + "_2 " + f.name() + "_2");
      StringBuilder val = new StringBuilder();
      for (int j = 0; j < 16; j++) {
        val.append(proto);
        val.append(" ");
      }
      ((Field) f).setStringValue(val.toString());
    }
    iw.addDocument(doc);
  }
}
 
源代码3 项目: lucene-solr   文件: RealTimeGetComponent.java
private static SolrInputDocument toSolrInputDocument(Document doc, IndexSchema schema) {
  SolrInputDocument out = new SolrInputDocument();
  for( IndexableField f : doc.getFields() ) {
    String fname = f.name();
    SchemaField sf = schema.getFieldOrNull(f.name());
    Object val = null;
    if (sf != null) {
      if ((!sf.hasDocValues() && !sf.stored()) || schema.isCopyFieldTarget(sf)) continue;
      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;
    }

    // todo: how to handle targets of copy fields (including polyfield sub-fields)?
    out.addField(fname, val);
  }
  return out;
}
 
源代码4 项目: lucene-solr   文件: TrieField.java
private void storedToIndexed(IndexableField f, final BytesRefBuilder bytes) {
  final Number val = f.numericValue();
  if (val != null) {
    switch (type) {
      case INTEGER:
        LegacyNumericUtils.intToPrefixCoded(val.intValue(), 0, bytes);
        break;
      case FLOAT:
        LegacyNumericUtils.intToPrefixCoded(NumericUtils.floatToSortableInt(val.floatValue()), 0, bytes);
        break;
      case LONG: //fallthrough!
      case DATE:
        LegacyNumericUtils.longToPrefixCoded(val.longValue(), 0, bytes);
        break;
      case DOUBLE:
        LegacyNumericUtils.longToPrefixCoded(NumericUtils.doubleToSortableLong(val.doubleValue()), 0, bytes);
        break;
      default:
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
    }
  } else {
    // the old BinaryField encoding is no longer supported
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid field contents: "+f.name());
  }
}
 
源代码5 项目: rdf4j   文件: LuceneDocument.java
@Override
public Set<String> getPropertyNames() {
	List<IndexableField> fields = doc.getFields();
	Set<String> names = new HashSet<>();
	for (IndexableField field : fields) {
		String name = field.name();
		if (SearchFields.isPropertyField(name)) {
			names.add(name);
		}
	}
	return names;
}
 
源代码6 项目: modernmt   文件: DefaultDocumentBuilder.java
@Override
public ScoreEntry asScoreEntry(Document self) {
    Language source = null;
    Language target = null;

    for (IndexableField field : self.getFields()) {
        String name = field.name();

        if (name.startsWith(LANGUAGE_PREFIX_FIELD)) {
            Language l = Language.fromString(name.substring(LANGUAGE_PREFIX_FIELD.length()));

            if (source == null) {
                source = l;
            } else {
                target = l;
                break;
            }
        }
    }

    if (source == null || target == null)
        throw new IllegalArgumentException("Invalid document: missing language info.");

    LanguageDirection language;
    if (source.toLanguageTag().compareTo(target.toLanguageTag()) < 0)
        language = new LanguageDirection(source, target);
    else
        language = new LanguageDirection(target, source);

    return asScoreEntry(self, language);
}
 
@Override
public void writeField(FieldInfo info, IndexableField field) throws IOException {
  write(FIELD);
  write(Integer.toString(info.number));
  newLine();
  
  write(NAME);
  write(field.name());
  newLine();
  
  write(TYPE);
  final Number n = field.numericValue();

  if (n != null) {
    if (n instanceof Byte || n instanceof Short || n instanceof Integer) {
      write(TYPE_INT);
      newLine();
        
      write(VALUE);
      write(Integer.toString(n.intValue()));
      newLine();
    } else if (n instanceof Long) {
      write(TYPE_LONG);
      newLine();

      write(VALUE);
      write(Long.toString(n.longValue()));
      newLine();
    } else if (n instanceof Float) {
      write(TYPE_FLOAT);
      newLine();
        
      write(VALUE);
      write(Float.toString(n.floatValue()));
      newLine();
    } else if (n instanceof Double) {
      write(TYPE_DOUBLE);
      newLine();
        
      write(VALUE);
      write(Double.toString(n.doubleValue()));
      newLine();
    } else {
      throw new IllegalArgumentException("cannot store numeric type " + n.getClass());
    }
  } else { 
    BytesRef bytes = field.binaryValue();
    if (bytes != null) {
      write(TYPE_BINARY);
      newLine();
      
      write(VALUE);
      write(bytes);
      newLine();
    } else if (field.stringValue() == null) {
      throw new IllegalArgumentException("field " + field.name() + " is stored but does not have binaryValue, stringValue nor numericValue");
    } else {
      write(TYPE_STRING);
      newLine();
      write(VALUE);
      write(field.stringValue());
      newLine();
    }
  }
}
 
@Override
public void writeField(FieldInfo info, IndexableField field)
    throws IOException {

  ++numStoredFieldsInDoc;

  int bits = 0;
  final BytesRef bytes;
  final String string;

  Number number = field.numericValue();
  if (number != null) {
    if (number instanceof Byte || number instanceof Short || number instanceof Integer) {
      bits = NUMERIC_INT;
    } else if (number instanceof Long) {
      bits = NUMERIC_LONG;
    } else if (number instanceof Float) {
      bits = NUMERIC_FLOAT;
    } else if (number instanceof Double) {
      bits = NUMERIC_DOUBLE;
    } else {
      throw new IllegalArgumentException("cannot store numeric type " + number.getClass());
    }
    string = null;
    bytes = null;
  } else {
    bytes = field.binaryValue();
    if (bytes != null) {
      bits = BYTE_ARR;
      string = null;
    } else {
      bits = STRING;
      string = field.stringValue();
      if (string == null) {
        throw new IllegalArgumentException("field " + field.name() + " is stored but does not have binaryValue, stringValue nor numericValue");
      }
    }
  }

  final long infoAndBits = (((long) info.number) << TYPE_BITS) | bits;
  bufferedDocs.writeVLong(infoAndBits);

  if (bytes != null) {
    bufferedDocs.writeVInt(bytes.length);
    bufferedDocs.writeBytes(bytes.bytes, bytes.offset, bytes.length);
  } else if (string != null) {
    bufferedDocs.writeString(string);
  } else {
    if (number instanceof Byte || number instanceof Short || number instanceof Integer) {
      bufferedDocs.writeZInt(number.intValue());
    } else if (number instanceof Long) {
      writeTLong(bufferedDocs, number.longValue());
    } else if (number instanceof Float) {
      writeZFloat(bufferedDocs, number.floatValue());
    } else if (number instanceof Double) {
      writeZDouble(bufferedDocs, number.doubleValue());
    } else {
      throw new AssertionError("Cannot get here");
    }
  }
}
 
源代码9 项目: lucene-solr   文件: TrieField.java
@Override
public Object toObject(IndexableField f) {
  final Number val = f.numericValue();
  if (val != null) {

    if (f.fieldType().stored() == false && f.fieldType().docValuesType() == DocValuesType.NUMERIC ) {
      long bits = val.longValue();
      switch (type) {
        case INTEGER:
          return (int)bits;
        case FLOAT:
          return Float.intBitsToFloat((int)bits);
        case LONG:
          return bits;
        case DOUBLE:
          return Double.longBitsToDouble(bits);
        case DATE:
          return new Date(bits);
        default:
          throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
      }
    }

    // normal stored case
    return (type == NumberType.DATE) ? new Date(val.longValue()) : val;
  } else {
    // multi-valued numeric docValues currently use SortedSet on the indexed terms.
    BytesRef term = f.binaryValue();
    switch (type) {
      case INTEGER:
        return LegacyNumericUtils.prefixCodedToInt(term);
      case FLOAT:
        return NumericUtils.sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(term));
      case LONG:
        return LegacyNumericUtils.prefixCodedToLong(term);
      case DOUBLE:
        return NumericUtils.sortableLongToDouble(LegacyNumericUtils.prefixCodedToLong(term));
      case DATE:
        return new Date(LegacyNumericUtils.prefixCodedToLong(term));
      default:
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
    }
  }

}
 
源代码10 项目: incubator-retired-blur   文件: HighlightHelper.java
/**
 * NOTE: This method will not preserve the correct field types.
 * 
 * @param preTag
 * @param postTag
 */
public static Document highlight(int docId, Document document, Query query, FieldManager fieldManager,
    IndexReader reader, String preTag, String postTag) throws IOException, InvalidTokenOffsetsException {

  String fieldLessFieldName = fieldManager.getFieldLessFieldName();

  Query fixedQuery = fixSuperQuery(query, null, fieldLessFieldName);

  Analyzer analyzer = fieldManager.getAnalyzerForQuery();

  SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter(preTag, postTag);
  Document result = new Document();
  for (IndexableField f : document) {
    String name = f.name();
    if (fieldLessFieldName.equals(name) || FIELDS_NOT_TO_HIGHLIGHT.contains(name)) {
      result.add(f);
      continue;
    }
    String text = f.stringValue();
    Number numericValue = f.numericValue();

    Query fieldFixedQuery;
    if (fieldManager.isFieldLessIndexed(name)) {
      fieldFixedQuery = fixSuperQuery(query, name, fieldLessFieldName);
    } else {
      fieldFixedQuery = fixedQuery;
    }

    if (numericValue != null) {
      if (shouldNumberBeHighlighted(name, numericValue, fieldFixedQuery)) {
        String numberHighlight = preTag + text + postTag;
        result.add(new StringField(name, numberHighlight, Store.YES));
      }
    } else {
      Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(fieldFixedQuery, name));
      TokenStream tokenStream = TokenSources.getAnyTokenStream(reader, docId, name, analyzer);
      TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);
      for (int j = 0; j < frag.length; j++) {
        if ((frag[j] != null) && (frag[j].getScore() > 0)) {
          result.add(new StringField(name, frag[j].toString(), Store.YES));
        }
      }
    }
  }
  return result;
}