Search in sources :

Example 41 with IndexableField

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) + ""));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 42 with IndexableField

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")));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 43 with IndexableField

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());
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 44 with IndexableField

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);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Fields(org.apache.lucene.index.Fields) MultiFields(org.apache.lucene.index.MultiFields) FastStringReader(org.elasticsearch.common.io.FastStringReader) HashMap(java.util.HashMap) Terms(org.apache.lucene.index.Terms) Document(org.apache.lucene.document.Document)

Example 45 with IndexableField

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++;
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) HashSet(java.util.HashSet)

Aggregations

IndexableField (org.apache.lucene.index.IndexableField)276 Document (org.apache.lucene.document.Document)90 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)75 Matchers.containsString (org.hamcrest.Matchers.containsString)57 BytesRef (org.apache.lucene.util.BytesRef)53 ArrayList (java.util.ArrayList)50 Field (org.apache.lucene.document.Field)34 Test (org.junit.Test)28 IOException (java.io.IOException)27 HashMap (java.util.HashMap)24 IndexReader (org.apache.lucene.index.IndexReader)24 Directory (org.apache.lucene.store.Directory)23 Map (java.util.Map)22 TopDocs (org.apache.lucene.search.TopDocs)22 Term (org.apache.lucene.index.Term)21 HashSet (java.util.HashSet)20 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)20 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)20 Query (org.apache.lucene.search.Query)19 Analyzer (org.apache.lucene.analysis.Analyzer)18