use of org.apache.lucene.index.IndexableField in project elasticsearch by elastic.
the class RangeFieldMapperTests method doTestNullValue.
@Override
protected void doTestNullValue(String type) throws IOException {
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", type).field("store", true);
if (type.equals("date_range")) {
mapping = mapping.field("format", DATE_FORMAT);
}
mapping = mapping.endObject().endObject().endObject().endObject();
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping.string()));
assertEquals(mapping.string(), mapper.mappingSource().toString());
// test null value for min and max
ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().startObject("field").nullField(getFromField()).nullField(getToField()).endObject().endObject().bytes());
assertEquals(2, doc.rootDoc().getFields("field").length);
IndexableField[] fields = doc.rootDoc().getFields("field");
IndexableField storedField = fields[1];
assertThat(storedField.stringValue(), containsString(type.equals("date_range") ? Long.MAX_VALUE + "" : getMax(type) + ""));
// test null max value
doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().startObject("field").field(getFromField(), getFrom(type)).nullField(getToField()).endObject().endObject().bytes());
fields = doc.rootDoc().getFields("field");
assertEquals(2, fields.length);
IndexableField pointField = fields[0];
assertEquals(2, pointField.fieldType().pointDimensionCount());
assertFalse(pointField.fieldType().stored());
storedField = fields[1];
assertTrue(storedField.fieldType().stored());
assertThat(storedField.stringValue(), containsString(type.equals("date_range") ? "1477872000000" : "5"));
assertThat(storedField.stringValue(), containsString(getMax(type) + ""));
}
use of org.apache.lucene.index.IndexableField in project elasticsearch by elastic.
the class RangeFieldMapperTests method doTestCoerce.
@Override
public void doTestCoerce(String type) throws IOException {
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", type);
if (type.equals("date_range")) {
mapping = mapping.field("format", DATE_FORMAT);
}
mapping = mapping.endObject().endObject().endObject().endObject();
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping.string()));
assertEquals(mapping.string(), mapper.mappingSource().toString());
ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().startObject("field").field(getFromField(), getFrom(type)).field(getToField(), getTo(type)).endObject().endObject().bytes());
IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(1, fields.length);
IndexableField pointField = fields[0];
assertEquals(2, pointField.fieldType().pointDimensionCount());
mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", type).field("coerce", false).endObject().endObject().endObject().endObject();
DocumentMapper mapper2 = parser.parse("type", new CompressedXContent(mapping.string()));
assertEquals(mapping.string(), mapper2.mappingSource().toString());
ThrowingRunnable runnable = () -> mapper2.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().startObject("field").field(getFromField(), "5.2").field(getToField(), "10").endObject().endObject().bytes());
MapperParsingException e = expectThrows(MapperParsingException.class, runnable);
assertThat(e.getCause().getMessage(), anyOf(containsString("passed as String"), containsString("failed to parse date")));
}
use of org.apache.lucene.index.IndexableField in project elasticsearch by elastic.
the class ScaledFloatFieldMapperTests method testStore.
public void testStore() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "scaled_float").field("store", true).field("scaling_factor", 10.0).endObject().endObject().endObject().endObject().string();
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
assertEquals(mapping, mapper.mappingSource().toString());
ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", 123).endObject().bytes());
IndexableField[] fields = doc.rootDoc().getFields("field");
assertEquals(3, fields.length);
IndexableField pointField = fields[0];
assertEquals(1, pointField.fieldType().pointDimensionCount());
assertEquals(1230, pointField.numericValue().doubleValue(), 0d);
IndexableField dvField = fields[1];
assertEquals(DocValuesType.SORTED_NUMERIC, dvField.fieldType().docValuesType());
IndexableField storedField = fields[2];
assertTrue(storedField.fieldType().stored());
assertEquals(1230, storedField.numericValue().longValue());
}
use of org.apache.lucene.index.IndexableField in project elasticsearch by elastic.
the class XMoreLikeThis method retrieveTerms.
/**
* Find words for a more-like-this query former.
*
* @param docNum the id of the lucene document from which to find terms
*/
private PriorityQueue<ScoreTerm> retrieveTerms(int docNum) throws IOException {
Map<String, Int> termFreqMap = new HashMap<>();
for (String fieldName : fieldNames) {
final Fields vectors = ir.getTermVectors(docNum);
final Terms vector;
if (vectors != null) {
vector = vectors.terms(fieldName);
} else {
vector = null;
}
// field does not store term vector info
if (vector == null) {
Document d = ir.document(docNum);
IndexableField[] fields = d.getFields(fieldName);
for (IndexableField field : fields) {
final String stringValue = field.stringValue();
if (stringValue != null) {
addTermFrequencies(new FastStringReader(stringValue), termFreqMap, fieldName);
}
}
} else {
addTermFrequencies(termFreqMap, vector, fieldName);
}
}
return createQueue(termFreqMap);
}
use of org.apache.lucene.index.IndexableField in project elasticsearch by elastic.
the class FreqTermsEnumTests method addFreqs.
private void addFreqs(Document doc, Map<String, FreqHolder> reference) {
Set<String> addedDocFreq = new HashSet<>();
for (IndexableField field : doc.getFields("field")) {
String term = field.stringValue();
FreqHolder freqHolder = reference.get(term);
if (!addedDocFreq.contains(term)) {
freqHolder.docFreq++;
addedDocFreq.add(term);
}
freqHolder.totalTermFreq++;
}
}
Aggregations