Search in sources :

Example 11 with IndexableField

use of org.apache.lucene.index.IndexableField in project elasticsearch by elastic.

the class DateFieldMapperTests method testTimeZoneParsing.

/**
     * Test that time zones are correctly parsed by the {@link DateFieldMapper}.
     * There is a known bug with Joda 2.9.4 reported in https://github.com/JodaOrg/joda-time/issues/373.
     */
public void testTimeZoneParsing() throws Exception {
    final String timeZonePattern = "yyyy-MM-dd" + randomFrom("ZZZ", "[ZZZ]", "'['ZZZ']'");
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "date").field("format", timeZonePattern).endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
    assertEquals(mapping, mapper.mappingSource().toString());
    final DateTimeZone randomTimeZone = randomBoolean() ? DateTimeZone.forID(randomFrom("UTC", "CET")) : randomDateTimeZone();
    final DateTime randomDate = new DateTime(2016, 03, 11, 0, 0, 0, randomTimeZone);
    ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", DateTimeFormat.forPattern(timeZonePattern).print(randomDate)).endObject().bytes());
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(2, fields.length);
    assertEquals(randomDate.withZone(DateTimeZone.UTC).getMillis(), fields[0].numericValue().longValue());
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime)

Example 12 with IndexableField

use of org.apache.lucene.index.IndexableField in project elasticsearch by elastic.

the class CompletionFieldMapperTests method testParsingMultiValued.

public void testParsingMultiValued() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject().array("completion", "suggestion1", "suggestion2").endObject().bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertSuggestFields(fields, 2);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 13 with IndexableField

use of org.apache.lucene.index.IndexableField in project elasticsearch by elastic.

the class HalfFloatFielddataTests method testSingleValued.

public void testSingleValued() throws IOException {
    Directory dir = newDirectory();
    // we need the default codec to check for singletons
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null).setCodec(TestUtil.getDefaultCodec()));
    Document doc = new Document();
    for (IndexableField f : NumberFieldMapper.NumberType.HALF_FLOAT.createFields("half_float", 3f, false, true, false)) {
        doc.add(f);
    }
    w.addDocument(doc);
    final DirectoryReader dirReader = DirectoryReader.open(w);
    LeafReader reader = getOnlyLeafReader(dirReader);
    SortedNumericDoubleValues values = new SortedNumericDVIndexFieldData.SortedNumericHalfFloatFieldData(reader, "half_float").getDoubleValues();
    assertNotNull(FieldData.unwrapSingleton(values));
    values.setDocument(0);
    assertEquals(1, values.count());
    assertEquals(3f, values.valueAt(0), 0f);
    IOUtils.close(dirReader, w, dir);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) LeafReader(org.apache.lucene.index.LeafReader) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 14 with IndexableField

use of org.apache.lucene.index.IndexableField in project elasticsearch by elastic.

the class CompletionFieldMapperTests method testParsingFull.

public void testParsingFull() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject().startArray("completion").startObject().field("input", "suggestion1").field("weight", 3).endObject().startObject().field("input", "suggestion2").field("weight", 4).endObject().startObject().field("input", "suggestion3").field("weight", 5).endObject().endArray().endObject().bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertSuggestFields(fields, 3);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 15 with IndexableField

use of org.apache.lucene.index.IndexableField in project elasticsearch by elastic.

the class CompletionFieldMapperTests method testParsingWithWeight.

public void testParsingWithWeight() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject().startObject("completion").field("input", "suggestion").field("weight", 2).endObject().endObject().bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertSuggestFields(fields, 1);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

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