Search in sources :

Example 51 with ParsedDocument

use of org.elasticsearch.index.mapper.ParsedDocument in project crate by crate.

the class ArrayMapperTest method testCopyToFieldsOfInnerMapping.

@Test
public void testCopyToFieldsOfInnerMapping() throws Exception {
    // @formatter:off
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("string_array").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER_TYPE).field("type", "text").field("index", "true").field("copy_to", "string_array_ft").endObject().endObject().endObject().endObject().endObject());
    // @formatter:on
    DocumentMapper mapper = mapper(INDEX, mapping);
    FieldMapper arrayMapper = (FieldMapper) mapper.mappers().getMapper("string_array");
    assertThat(arrayMapper, is(instanceOf(ArrayMapper.class)));
    assertThat(arrayMapper.copyTo().copyToFields(), contains("string_array_ft"));
    // @formatter:on
    BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startArray("string_array").value("foo").value("bar").endArray().endObject());
    SourceToParse sourceToParse = new SourceToParse(INDEX, "1", bytesReference, XContentType.JSON);
    ParsedDocument doc = mapper.parse(sourceToParse);
    // @formatter:off
    List<String> copyValues = new ArrayList<>();
    Document document = doc.docs().get(0);
    IndexableField[] fields = document.getFields("string_array_ft");
    for (var field : fields) {
        if (field.fieldType().docValuesType() == DocValuesType.SORTED_SET) {
            copyValues.add(field.binaryValue().utf8ToString());
        }
    }
    assertThat(copyValues, containsInAnyOrder("foo", "bar"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ArrayList(java.util.ArrayList) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) Document(org.elasticsearch.index.mapper.ParseContext.Document) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 52 with ParsedDocument

use of org.elasticsearch.index.mapper.ParsedDocument in project crate by crate.

the class ArrayMapperTest method testParseNullOnObjectArray.

@Test
public void testParseNullOnObjectArray() throws Exception {
    // @formatter: on
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER_TYPE).field("type", "object").startObject("properties").endObject().endObject().endObject().endObject().endObject().endObject());
    // @formatter: off
    DocumentMapper mapper = mapper(INDEX, mapping);
    BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().nullField("array_field").endObject());
    SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON);
    ParsedDocument parsedDoc = mapper.parse(sourceToParse);
    assertThat(parsedDoc.docs().size(), is(1));
    assertThat(parsedDoc.docs().get(0).getField("array_field"), is(nullValue()));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 53 with ParsedDocument

use of org.elasticsearch.index.mapper.ParsedDocument in project crate by crate.

the class ArrayMapperTest method testSimpleArrayMapping.

@Test
public void testSimpleArrayMapping() throws Exception {
    // @formatter:off
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER_TYPE).field("type", "keyword").endObject().endObject().endObject().endObject().endObject());
    // @formatter:on
    DocumentMapper mapper = mapper(INDEX, mapping);
    assertThat(mapper.mappers().getMapper("array_field"), is(instanceOf(ArrayMapper.class)));
    BytesReference bytesReference = BytesReference.bytes(JsonXContent.contentBuilder().startObject().array("array_field", "a", "b", "c").endObject());
    SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON);
    ParsedDocument doc = mapper.parse(sourceToParse);
    assertThat(doc.dynamicMappingsUpdate() == null, is(true));
    assertThat(doc.docs().size(), is(1));
    ParseContext.Document fields = doc.docs().get(0);
    Set<String> values = uniqueValuesFromFields(fields, "array_field");
    assertThat(values, Matchers.containsInAnyOrder("a", "b", "c"));
    assertThat(mapper.mappingSource().string(), is("{\"default\":{" + "\"properties\":{" + "\"array_field\":{" + "\"type\":\"array\"," + "\"inner\":{" + "\"type\":\"keyword\"" + "}" + "}" + "}" + "}}"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Document(org.elasticsearch.index.mapper.ParseContext.Document) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ParseContext(org.elasticsearch.index.mapper.ParseContext) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 54 with ParsedDocument

use of org.elasticsearch.index.mapper.ParsedDocument in project crate by crate.

the class ArrayMapperTest method testObjectArrayMapping.

@Test
public void testObjectArrayMapping() throws Exception {
    // @formatter: off
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER_TYPE).field("type", "object").field("dynamic", true).startObject("properties").startObject("s").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject().endObject());
    DocumentMapper mapper = mapper(INDEX, mapping);
    // child object mapper
    assertThat(mapper.objectMappers().get("array_field"), is(instanceOf(ObjectArrayMapper.class)));
    BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startArray("array_field").startObject().field("s", "a").endObject().startObject().field("s", "b").endObject().startObject().field("s", "c").endObject().endArray().endObject());
    SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON);
    ParsedDocument doc = mapper.parse(sourceToParse);
    // @formatter: off
    assertThat(doc.dynamicMappingsUpdate(), nullValue());
    assertThat(doc.docs().size(), is(1));
    assertThat(uniqueValuesFromFields(doc.docs().get(0), "array_field.s"), containsInAnyOrder("a", "b", "c"));
    assertThat(mapper.mappers().getMapper("array_field.s"), instanceOf(KeywordFieldMapper.class));
    assertThat(mapper.mappingSource().string(), is("{\"default\":{" + "\"properties\":{" + "\"array_field\":{" + "\"type\":\"array\"," + "\"inner\":{" + "\"dynamic\":\"true\"," + "\"properties\":{" + "\"s\":{" + "\"type\":\"keyword\"" + "}" + "}" + "}" + "}" + "}}}"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 55 with ParsedDocument

use of org.elasticsearch.index.mapper.ParsedDocument in project crate by crate.

the class ArrayMapperTest method testObjectArrayMappingNewColumn.

@Test
public void testObjectArrayMappingNewColumn() throws Exception {
    // @formatter: off
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER_TYPE).field("type", "object").field("dynamic", true).startObject("properties").startObject("s").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject().endObject());
    DocumentMapper mapper = mapper(INDEX, mapping);
    // child object mapper
    assertThat(mapper.objectMappers().get("array_field"), is(instanceOf(ObjectArrayMapper.class)));
    BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startArray("array_field").startObject().field("s", "a").field("new", true).endObject().endArray().endObject());
    SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON);
    ParsedDocument doc = mapper.parse(sourceToParse);
    Mapping mappingUpdate = doc.dynamicMappingsUpdate();
    assertThat(mappingUpdate, notNullValue());
    mapper = mapper.merge(mappingUpdate);
    assertThat(doc.docs().size(), is(1));
    String[] values = doc.docs().get(0).getValues("array_field.new");
    assertThat(values, arrayContainingInAnyOrder(is("T"), is("1")));
    String mappingSourceString = new CompressedXContent(mapper, XContentType.JSON, ToXContent.EMPTY_PARAMS).string();
    assertThat(mappingSourceString, is("{\"default\":{" + "\"properties\":{" + "\"array_field\":{" + "\"type\":\"array\"," + "\"inner\":{" + "\"dynamic\":\"true\"," + "\"properties\":{" + "\"new\":{\"type\":\"boolean\"}," + "\"s\":{" + "\"type\":\"keyword\"" + "}" + "}" + "}" + "}" + "}}}"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) Mapping(org.elasticsearch.index.mapper.Mapping) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)211 Test (org.junit.Test)85 LongPoint (org.apache.lucene.document.LongPoint)59 BytesArray (org.elasticsearch.common.bytes.BytesArray)58 Matchers.containsString (org.hamcrest.Matchers.containsString)57 Store (org.elasticsearch.index.store.Store)52 Searcher (org.elasticsearch.index.engine.Engine.Searcher)46 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)35 IOException (java.io.IOException)32 AtomicLong (java.util.concurrent.atomic.AtomicLong)31 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)31 IndexableField (org.apache.lucene.index.IndexableField)30 Term (org.apache.lucene.index.Term)28 TopDocs (org.apache.lucene.search.TopDocs)28 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)27 Index (org.elasticsearch.index.Index)27 UncheckedIOException (java.io.UncheckedIOException)26 Field (org.apache.lucene.document.Field)26 TextField (org.apache.lucene.document.TextField)26 ArrayList (java.util.ArrayList)25