Search in sources :

Example 36 with IndexableField

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

the class SourceFieldMapperTests method testIncludes.

public void testIncludes() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("_source").array("includes", new String[] { "path1*" }).endObject().endObject().endObject().string();
    DocumentMapper documentMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    ParsedDocument doc = documentMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().startObject("path1").field("field1", "value1").endObject().startObject("path2").field("field2", "value2").endObject().endObject().bytes());
    IndexableField sourceField = doc.rootDoc().getField("_source");
    Map<String, Object> sourceAsMap;
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, new BytesArray(sourceField.binaryValue()))) {
        sourceAsMap = parser.map();
    }
    assertThat(sourceAsMap.containsKey("path1"), equalTo(true));
    assertThat(sourceAsMap.containsKey("path2"), equalTo(false));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesArray(org.elasticsearch.common.bytes.BytesArray) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 37 with IndexableField

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

the class SourceFieldMapperTests method testExcludes.

public void testExcludes() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("_source").array("excludes", new String[] { "path1*" }).endObject().endObject().endObject().string();
    DocumentMapper documentMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    ParsedDocument doc = documentMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().startObject("path1").field("field1", "value1").endObject().startObject("path2").field("field2", "value2").endObject().endObject().bytes());
    IndexableField sourceField = doc.rootDoc().getField("_source");
    Map<String, Object> sourceAsMap;
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, new BytesArray(sourceField.binaryValue()))) {
        sourceAsMap = parser.map();
    }
    assertThat(sourceAsMap.containsKey("path1"), equalTo(false));
    assertThat(sourceAsMap.containsKey("path2"), equalTo(true));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesArray(org.elasticsearch.common.bytes.BytesArray) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 38 with IndexableField

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

the class TextFieldMapperTests method testDisableNorms.

public void testDisableNorms() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "text").field("norms", 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", "1234").endObject().bytes());
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);
    assertTrue(fields[0].fieldType().omitNorms());
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 39 with IndexableField

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

the class TextFieldMapperTests method testEnableStore.

public void testEnableStore() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "text").field("store", true).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", "1234").endObject().bytes());
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);
    assertTrue(fields[0].fieldType().stored());
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 40 with IndexableField

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

the class RangeFieldMapperTests method doTestNoBounds.

public void doTestNoBounds(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 no bounds specified
    ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().startObject("field").endObject().endObject().bytes());
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(2, fields.length);
    IndexableField pointField = fields[0];
    assertEquals(2, pointField.fieldType().pointDimensionCount());
    assertFalse(pointField.fieldType().stored());
    IndexableField storedField = fields[1];
    assertTrue(storedField.fieldType().stored());
    assertThat(storedField.stringValue(), containsString(type.equals("date_range") ? Long.MAX_VALUE + "" : getMax(type) + ""));
    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)

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