Search in sources :

Example 26 with IndexableField

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

the class NumberFieldMapperTests method doTestNotIndexed.

@Override
public void doTestNotIndexed(String type) throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", type).field("index", false).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(1, fields.length);
    IndexableField dvField = fields[0];
    assertEquals(DocValuesType.SORTED_NUMERIC, dvField.fieldType().docValuesType());
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 27 with IndexableField

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

the class IpFieldMapperTests method testNullValue.

public void testNullValue() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "ip").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().nullField("field").endObject().bytes());
    assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field"));
    mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "ip").field("null_value", "::1").endObject().endObject().endObject().endObject().string();
    mapper = parser.parse("type", new CompressedXContent(mapping));
    assertEquals(mapping, mapper.mappingSource().toString());
    doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().nullField("field").endObject().bytes());
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(2, fields.length);
    IndexableField pointField = fields[0];
    assertEquals(1, pointField.fieldType().pointDimensionCount());
    assertEquals(16, pointField.fieldType().pointNumBytes());
    assertFalse(pointField.fieldType().stored());
    assertEquals(new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))), pointField.binaryValue());
    IndexableField dvField = fields[1];
    assertEquals(DocValuesType.SORTED_SET, dvField.fieldType().docValuesType());
    assertEquals(new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))), dvField.binaryValue());
    assertFalse(dvField.fieldType().stored());
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesRef(org.apache.lucene.util.BytesRef)

Example 28 with IndexableField

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

the class IpFieldMapperTests method testIgnoreMalformed.

public void testIgnoreMalformed() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "ip").endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
    assertEquals(mapping, mapper.mappingSource().toString());
    ThrowingRunnable runnable = () -> mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", ":1").endObject().bytes());
    MapperParsingException e = expectThrows(MapperParsingException.class, runnable);
    assertThat(e.getCause().getMessage(), containsString("':1' is not an IP string literal"));
    mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "ip").field("ignore_malformed", true).endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper2 = parser.parse("type", new CompressedXContent(mapping));
    ParsedDocument doc = mapper2.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", ":1").endObject().bytes());
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(0, fields.length);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 29 with IndexableField

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

the class JavaMultiFieldMergeTests method testUpgradeFromMultiFieldTypeToMultiFields.

public void testUpgradeFromMultiFieldTypeToMultiFields() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping1.json");
    MapperService mapperService = createIndex("test").mapperService();
    DocumentMapper docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), nullValue());
    BytesReference json = XContentFactory.jsonBuilder().startObject().field("name", "some name").endObject().bytes();
    Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f, notNullValue());
    f = doc.getField("name.indexed");
    assertThat(f, nullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/upgrade1.json");
    docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), nullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue());
    doc = docMapper.parse("test", "person", "1", json).rootDoc();
    f = doc.getField("name");
    assertThat(f, notNullValue());
    f = doc.getField("name.indexed");
    assertThat(f, notNullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/upgrade2.json");
    docMapper = mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue());
    mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/upgrade3.json");
    try {
        mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("mapper [name] has different [index] values"));
        assertThat(e.getMessage(), containsString("mapper [name] has different [store] values"));
    }
    // There are conflicts, so the `name.not_indexed3` has not been added
    assertNotSame(IndexOptions.NONE, docMapper.mappers().getMapper("name").fieldType().indexOptions());
    assertThat(docMapper.mappers().getMapper("name.indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed2"), notNullValue());
    assertThat(docMapper.mappers().getMapper("name.not_indexed3"), nullValue());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexableField(org.apache.lucene.index.IndexableField) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 30 with IndexableField

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

the class KeywordFieldMapperTests method testIgnoreAbove.

public void testIgnoreAbove() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "keyword").field("ignore_above", 5).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", "elk").endObject().bytes());
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(2, fields.length);
    doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", "elasticsearch").endObject().bytes());
    fields = doc.rootDoc().getFields("field");
    assertEquals(0, fields.length);
}
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