use of org.apache.lucene.document.StoredField in project elasticsearch by elastic.
the class IpFieldMapper method parseCreateField.
@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
Object addressAsObject;
if (context.externalValueSet()) {
addressAsObject = context.externalValue();
} else {
addressAsObject = context.parser().textOrNull();
}
if (addressAsObject == null) {
addressAsObject = fieldType().nullValue();
}
if (addressAsObject == null) {
return;
}
String addressAsString = addressAsObject.toString();
InetAddress address;
if (addressAsObject instanceof InetAddress) {
address = (InetAddress) addressAsObject;
} else {
try {
address = InetAddresses.forString(addressAsString);
} catch (IllegalArgumentException e) {
if (ignoreMalformed.value()) {
return;
} else {
throw e;
}
}
}
if (context.includeInAll(includeInAll, this)) {
context.allEntries().addText(fieldType().name(), addressAsString, fieldType().boost());
}
if (fieldType().indexOptions() != IndexOptions.NONE) {
fields.add(new InetAddressPoint(fieldType().name(), address));
}
if (fieldType().hasDocValues()) {
fields.add(new SortedSetDocValuesField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address))));
}
if (fieldType().stored()) {
fields.add(new StoredField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address))));
}
}
use of org.apache.lucene.document.StoredField in project elasticsearch by elastic.
the class SourceFieldMapper method parseCreateField.
@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
if (!enabled) {
return;
}
if (!fieldType().stored()) {
return;
}
BytesReference source = context.sourceToParse().source();
// Percolate and tv APIs may not set the source and that is ok, because these APIs will not index any data
if (source == null) {
return;
}
if (filter != null) {
// we don't update the context source if we filter, we want to keep it as is...
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(source, true, context.sourceToParse().getXContentType());
Map<String, Object> filteredSource = filter.apply(mapTuple.v2());
BytesStreamOutput bStream = new BytesStreamOutput();
XContentType contentType = mapTuple.v1();
XContentBuilder builder = XContentFactory.contentBuilder(contentType, bStream).map(filteredSource);
builder.close();
source = bStream.bytes();
}
BytesRef ref = source.toBytesRef();
fields.add(new StoredField(fieldType().name(), ref.bytes, ref.offset, ref.length));
}
use of org.apache.lucene.document.StoredField in project elasticsearch by elastic.
the class Murmur3FieldMapper method parseCreateField.
@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
final Object value;
if (context.externalValueSet()) {
value = context.externalValue();
} else {
value = context.parser().textOrNull();
}
if (value != null) {
final BytesRef bytes = new BytesRef(value.toString());
final long hash = MurmurHash3.hash128(bytes.bytes, bytes.offset, bytes.length, 0, new MurmurHash3.Hash128()).h1;
fields.add(new SortedNumericDocValuesField(fieldType().name(), hash));
if (fieldType().stored()) {
fields.add(new StoredField(name(), hash));
}
}
}
use of org.apache.lucene.document.StoredField in project elasticsearch by elastic.
the class ScaledFloatFieldTypeTests method testStats.
public void testStats() throws IOException {
ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType();
ft.setName("scaled_float");
ft.setScalingFactor(0.1 + randomDouble() * 100);
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
try (DirectoryReader reader = DirectoryReader.open(w)) {
assertNull(ft.stats(reader));
}
Document doc = new Document();
doc.add(new StoredField("scaled_float", -1));
w.addDocument(doc);
try (DirectoryReader reader = DirectoryReader.open(w)) {
// field exists, but has no point values
FieldStats<?> stats = ft.stats(reader);
assertFalse(stats.hasMinMax());
assertNull(stats.getMinValue());
assertNull(stats.getMaxValue());
}
LongPoint point = new LongPoint("scaled_float", -1);
doc.add(point);
w.addDocument(doc);
point.setLongValue(10);
w.addDocument(doc);
try (DirectoryReader reader = DirectoryReader.open(w)) {
FieldStats<?> stats = ft.stats(reader);
assertEquals(-1 / ft.getScalingFactor(), stats.getMinValue());
assertEquals(10 / ft.getScalingFactor(), stats.getMaxValue());
assertEquals(3, stats.getMaxDoc());
}
w.deleteAll();
try (DirectoryReader reader = DirectoryReader.open(w)) {
assertNull(ft.stats(reader));
}
IOUtils.close(w, dir);
}
use of org.apache.lucene.document.StoredField in project lucene-solr by apache.
the class RandomSpatialOpFuzzyPrefixTreeTest method newDoc.
//Override so we can index parts of a pair separately, resulting in the detailLevel
// being independent for each shape vs the whole thing
@Override
protected Document newDoc(String id, Shape shape) {
Document doc = new Document();
doc.add(new StringField("id", id, Field.Store.YES));
if (shape != null) {
Collection<Shape> shapes;
if (shape instanceof ShapePair) {
shapes = new ArrayList<>(2);
shapes.add(((ShapePair) shape).shape1);
shapes.add(((ShapePair) shape).shape2);
} else {
shapes = Collections.singleton(shape);
}
for (Shape shapei : shapes) {
for (Field f : strategy.createIndexableFields(shapei)) {
doc.add(f);
}
}
if (//just for diagnostics
storeShape)
doc.add(new StoredField(strategy.getFieldName(), shape.toString()));
}
return doc;
}
Aggregations