public void testPercolatorFieldMapper_noQuery() throws Exception { addQueryMapping(); ParsedDocument doc = mapperService .documentMapper(typeName) .parse( "test", typeName, "1", XContentFactory.jsonBuilder().startObject().endObject().bytes()); assertThat(doc.rootDoc().getFields(fieldType.getQueryBuilderFieldName()).length, equalTo(0)); try { mapperService .documentMapper(typeName) .parse( "test", typeName, "1", XContentFactory.jsonBuilder().startObject().nullField(fieldName).endObject().bytes()); } catch (MapperParsingException e) { assertThat( e.getDetailedMessage(), containsString("query malformed, must start with start_object")); } }
@Test public void testNumericDetectionDefault() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .endObject() .endObject() .string(); DocumentMapper defaultMapper = MapperTestUtils.newParser().parse(mapping); ParsedDocument doc = defaultMapper.parse( "type", "1", XContentFactory.jsonBuilder() .startObject() .field("s_long", "100") .field("s_double", "100.0") .endObject() .bytes()); FieldMapper mapper = defaultMapper.mappers().smartNameFieldMapper("s_long"); assertThat(mapper, instanceOf(StringFieldMapper.class)); mapper = defaultMapper.mappers().smartNameFieldMapper("s_double"); assertThat(mapper, instanceOf(StringFieldMapper.class)); }
@Test public void testEnabled() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("_timestamp") .field("enabled", "yes") .field("store", "yes") .endObject() .endObject() .endObject() .string(); DocumentMapper docMapper = MapperTests.newParser().parse(mapping); byte[] source = XContentFactory.jsonBuilder() .startObject() .field("field", "value") .endObject() .copiedBytes(); ParsedDocument doc = docMapper.parse(SourceToParse.source(source).type("type").id("1").timestamp(1)); assertThat(doc.rootDoc().getFieldable("_timestamp").isStored(), equalTo(true)); assertThat(doc.rootDoc().getFieldable("_timestamp").isIndexed(), equalTo(true)); assertThat(doc.rootDoc().getFieldable("_timestamp").tokenStreamValue(), notNullValue()); }
@Test public void test_all_disabled_after_default_enabled() throws Exception { XContentBuilder mapping = XContentFactory.jsonBuilder() .startObject() .startObject("properties") .startObject("some_text") .field("type", "string") .endObject() .endObject() .endObject(); XContentBuilder mappingUpdate = XContentFactory.jsonBuilder() .startObject() .startObject("_all") .field("enabled", false) .endObject() .startObject("properties") .startObject("text") .field("type", "string") .endObject() .endObject() .endObject(); testConflictWhileMergingAndMappingUnchanged(mapping, mappingUpdate); }
@Override public void login(final RestRequest request, final ActionListener<String[]> listener) { String username = request.param(usernameKey); String password = request.param(passwordKey); final BytesReference content = request.content(); final XContentType xContentType = XContentFactory.xContentType(content); XContentParser parser = null; try { parser = XContentFactory.xContent(xContentType).createParser(content); final XContentParser.Token t = parser.nextToken(); if (t != null) { final Map<String, Object> contentMap = parser.map(); username = MapUtil.getAsString(contentMap, usernameKey, username); password = MapUtil.getAsString(contentMap, passwordKey, password); } } catch (final Exception e) { listener.onFailure(e); return; } finally { if (parser != null) { parser.close(); } } if (username == null) { listener.onResponse(new String[0]); return; } processLogin(username, password, listener); }
@Test public void testLatLonInOneValueStored() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("properties") .startObject("point") .field("type", "geo_point") .field("lat_lon", true) .field("store", "yes") .endObject() .endObject() .endObject() .endObject() .string(); DocumentMapper defaultMapper = MapperTestUtils.newParser().parse(mapping); ParsedDocument doc = defaultMapper.parse( "type", "1", XContentFactory.jsonBuilder() .startObject() .field("point", "1.2,1.3") .endObject() .bytes()); assertThat(doc.rootDoc().getField("point.lat"), notNullValue()); assertThat(doc.rootDoc().getField("point.lat").numericValue().doubleValue(), equalTo(1.2)); assertThat(doc.rootDoc().getField("point.lon"), notNullValue()); assertThat(doc.rootDoc().getField("point.lon").numericValue().doubleValue(), equalTo(1.3)); assertThat(doc.rootDoc().get("point"), equalTo("1.2,1.3")); }
public void testConflictNewType() throws Exception { XContentBuilder mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type1") .startObject("properties") .startObject("foo") .field("type", "long") .endObject() .endObject() .endObject() .endObject(); MapperService mapperService = createIndex("test", Settings.settingsBuilder().build(), "type1", mapping).mapperService(); XContentBuilder update = XContentFactory.jsonBuilder() .startObject() .startObject("type2") .startObject("properties") .startObject("foo") .field("type", "double") .endObject() .endObject() .endObject() .endObject(); try { mapperService.merge( "type2", new CompressedXContent(update.string()), MapperService.MergeReason.MAPPING_UPDATE, false); fail(); } catch (IllegalArgumentException e) { // expected assertTrue( e.getMessage(), e.getMessage().contains("mapper [foo] cannot be changed from type [long] to [double]")); } try { mapperService.merge( "type2", new CompressedXContent(update.string()), MapperService.MergeReason.MAPPING_UPDATE, false); fail(); } catch (IllegalArgumentException e) { // expected assertTrue( e.getMessage(), e.getMessage().contains("mapper [foo] cannot be changed from type [long] to [double]")); } assertTrue( mapperService.documentMapper("type1").mapping().root().getMapper("foo") instanceof LongFieldMapper); assertNull(mapperService.documentMapper("type2")); }
public void testNotIndexed() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("properties") .startObject("field") .field("type", "ip") .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", "::1").endObject().bytes()); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(1, fields.length); IndexableField dvField = fields[0]; assertEquals(DocValuesType.SORTED_SET, dvField.fieldType().docValuesType()); }
public void testNoDocValues() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("properties") .startObject("field") .field("type", "ip") .field("doc_values", 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", "::1").endObject().bytes()); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(1, fields.length); IndexableField pointField = fields[0]; assertEquals(1, pointField.fieldType().pointDimensionCount()); assertEquals( new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))), pointField.binaryValue()); }
public void testJoinFieldNotSet() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .endObject() .endObject() .string(); DocumentMapper docMapper = createIndex("test") .mapperService() .documentMapperParser() .parse("type", new CompressedXContent(mapping)); ParsedDocument doc = docMapper.parse( SourceToParse.source( "test", "type", "1", XContentFactory.jsonBuilder() .startObject() .field("x_field", "x_value") .endObject() .bytes())); assertEquals(0, getNumberOfFieldWithParentPrefix(doc.rootDoc())); }
@Override public XContentBuilder newBuilder(@Nullable BytesReference autoDetectSource, boolean useFiltering) throws IOException { XContentType contentType = XContentType.fromMediaTypeOrFormat(format); if (contentType == null) { // try and guess it from the auto detect source if (autoDetectSource != null) { contentType = XContentFactory.xContentType(autoDetectSource); } } if (contentType == null) { // default to JSON contentType = XContentType.JSON; } Set<String> includes = Collections.emptySet(); Set<String> excludes = Collections.emptySet(); if (useFiltering) { Set<String> filters = Strings.splitStringByCommaToSet(filterPath); includes = filters.stream().filter(INCLUDE_FILTER).collect(toSet()); excludes = filters.stream().filter(EXCLUDE_FILTER).map(f -> f.substring(1)).collect(toSet()); } XContentBuilder builder = new XContentBuilder( XContentFactory.xContent(contentType), bytesOutput(), includes, excludes); if (pretty) { builder.prettyPrint().lfAtEnd(); } builder.humanReadable(human); return builder; }
@Override public void doTestNoDocValues(String type) throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("properties") .startObject("field") .field("type", type) .field("doc_values", 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 pointField = fields[0]; assertEquals(1, pointField.fieldType().pointDimensionCount()); assertEquals(123, pointField.numericValue().doubleValue(), 0d); }
@Test public void testDynamicFalse() throws IOException { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .field("dynamic", "false") .startObject("properties") .startObject("field1") .field("type", "string") .endObject() .endObject() .endObject() .endObject() .string(); DocumentMapper defaultMapper = MapperTestUtils.newParser().parse(mapping); ParsedDocument doc = defaultMapper.parse( "type", "1", XContentFactory.jsonBuilder() .startObject() .field("field1", "value1") .field("field2", "value2") .bytes()); assertThat(doc.rootDoc().get("field1"), equalTo("value1")); assertThat(doc.rootDoc().get("field2"), nullValue()); }
@Test public void testDynamicStrict() throws IOException { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .field("dynamic", "strict") .startObject("properties") .startObject("field1") .field("type", "string") .endObject() .endObject() .endObject() .endObject() .string(); DocumentMapper defaultMapper = MapperTestUtils.newParser().parse(mapping); try { defaultMapper.parse( "type", "1", XContentFactory.jsonBuilder() .startObject() .field("field1", "value1") .field("field2", "value2") .bytes()); assert false; } catch (StrictDynamicMappingException e) { // all is well } }
public void testInjectIntoDocDuringParsing() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .endObject() .endObject() .string(); DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); ParsedDocument doc = defaultMapper.parse( "type", "1", XContentFactory.jsonBuilder() .startObject() .field("a", "100") .startObject("b") .field("c", 42) .endObject() .endObject() .bytes()); assertFieldNames(set("a", "b", "b.c", "_uid", "_type", "_version", "_source", "_all"), doc); }
public void testSerializeDefaults() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("properties") .startObject("field") .field("type", "ip") .endObject() .endObject() .endObject() .endObject() .string(); DocumentMapper docMapper = parser.parse("type", new CompressedXContent(mapping)); IpFieldMapper mapper = (IpFieldMapper) docMapper.root().getMapper("field"); XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); mapper.doXContentBody(builder, true, ToXContent.EMPTY_PARAMS); String got = builder.endObject().string(); // it would be nice to check the entire serialized default mapper, but there are // a whole lot of bogus settings right now it picks up from calling super.doXContentBody... assertTrue(got, got.contains("\"null_value\":null")); assertTrue(got, got.contains("\"ignore_malformed\":false")); assertTrue(got, got.contains("\"include_in_all\":false")); }
public void testExplicitEnabled() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("_field_names") .field("enabled", true) .endObject() .endObject() .endObject() .string(); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class); assertTrue(fieldNamesMapper.enabled()); ParsedDocument doc = docMapper.parse( "type", "1", XContentFactory.jsonBuilder() .startObject() .field("field", "value") .endObject() .bytes()); assertFieldNames(set("field", "_uid", "_type", "_version", "_source", "_all"), doc); }
public void testNestedList() throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() .startArray("some_array") .startArray() .endArray() .startArray() .value(1) .value(3) .endArray() .startArray() .value(2) .endArray() .endArray() .endObject(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); assertEquals("some_array", parser.currentName()); if (random().nextBoolean()) { // sometimes read the start array token, sometimes not assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken()); } assertEquals( Arrays.asList(Collections.<Integer>emptyList(), Arrays.asList(1, 3), Arrays.asList(2)), parser.list()); } }
@Test public void testGeoHashValue() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("properties") .startObject("point") .field("type", "geo_point") .field("lat_lon", true) .endObject() .endObject() .endObject() .endObject() .string(); DocumentMapper defaultMapper = MapperTestUtils.newParser().parse(mapping); ParsedDocument doc = defaultMapper.parse( "type", "1", XContentFactory.jsonBuilder() .startObject() .field("point", GeoHashUtils.encode(1.2, 1.3)) .endObject() .bytes()); assertThat(doc.rootDoc().getField("point.lat"), notNullValue()); assertThat(doc.rootDoc().getField("point.lon"), notNullValue()); assertThat(doc.rootDoc().get("point"), notNullValue()); }
public void testNestedMapInList() throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() .startArray("some_array") .startObject() .field("foo", "bar") .endObject() .startObject() .endObject() .endArray() .endObject(); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); assertEquals("some_array", parser.currentName()); if (random().nextBoolean()) { // sometimes read the start array token, sometimes not assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken()); } assertEquals(Arrays.asList(singletonMap("foo", "bar"), emptyMap()), parser.list()); } }
public void testConflictFieldsMapping(String fieldName) throws Exception { // test store, ... all the parameters that are not to be changed just like in other fields XContentBuilder mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject(fieldName) .field("enabled", true) .field("store", "no") .endObject() .endObject() .endObject(); XContentBuilder mappingUpdate = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject(fieldName) .field("enabled", true) .field("store", "yes") .endObject() .startObject("properties") .startObject("text") .field("type", "string") .endObject() .endObject() .endObject() .endObject(); testConflictWhileMergingAndMappingUnchanged(mapping, mappingUpdate); }
/** test exception on missing `end` and `match` parameter in parser */ public void testParseEnd() throws IOException { { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); builder.startObject(SpanFirstQueryBuilder.NAME); builder.field("match"); spanTermQuery("description", "jumped").toXContent(builder, null); builder.endObject(); builder.endObject(); ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(builder.string())); assertTrue(e.getMessage().contains("spanFirst must have [end] set")); } { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); builder.startObject(SpanFirstQueryBuilder.NAME); builder.field("end", 10); builder.endObject(); builder.endObject(); ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(builder.string())); assertTrue(e.getMessage().contains("spanFirst must have [match] span query clause")); } }
@Test public void testUpsert() throws Exception { createIndex(); ClusterHealthResponse clusterHealth = client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); assertThat(clusterHealth.timedOut(), equalTo(false)); assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.GREEN)); client .prepareUpdate("test", "type1", "1") .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject()) .setScript("ctx._source.field += 1") .execute() .actionGet(); for (int i = 0; i < 5; i++) { GetResponse getResponse = client.prepareGet("test", "type1", "1").execute().actionGet(); assertThat(getResponse.sourceAsMap().get("field").toString(), equalTo("1")); } client .prepareUpdate("test", "type1", "1") .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject()) .setScript("ctx._source.field += 1") .execute() .actionGet(); for (int i = 0; i < 5; i++) { GetResponse getResponse = client.prepareGet("test", "type1", "1").execute().actionGet(); assertThat(getResponse.sourceAsMap().get("field").toString(), equalTo("2")); } }
public void testDisabled() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("_field_names") .field("enabled", false) .endObject() .endObject() .endObject() .string(); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class); assertFalse(fieldNamesMapper.enabled()); ParsedDocument doc = docMapper.parse( "type", "1", XContentFactory.jsonBuilder() .startObject() .field("field", "value") .endObject() .bytes()); assertNull(doc.rootDoc().get("_field_names")); }
public void testIncludeInObjectNotAllowed() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .endObject() .endObject() .string(); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping); try { docMapper.parse( "test", "type", "1", XContentFactory.jsonBuilder().startObject().field("_all", "foo").endObject().bytes()); fail("Expected failure to parse metadata field"); } catch (MapperParsingException e) { assertTrue( e.getMessage(), e.getMessage() .contains("Field [_all] is a metadata field and cannot be added inside a document")); } }
public void testDisablingBackcompat() throws Exception { // before 1.5, disabling happened by setting index:no String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("_field_names") .field("index", "no") .endObject() .endObject() .endObject() .string(); Settings indexSettings = ImmutableSettings.builder() .put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id) .build(); DocumentMapper docMapper = createIndex("test", indexSettings).mapperService().documentMapperParser().parse(mapping); FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class); assertFalse(fieldNamesMapper.enabled()); ParsedDocument doc = docMapper.parse( "type", "1", XContentFactory.jsonBuilder() .startObject() .field("field", "value") .endObject() .bytes()); assertNull(doc.rootDoc().get("_field_names")); }
@Test public void testLatLonInOneValue() throws Exception { String mapping = XContentFactory.contentTextBuilder(XContentType.JSON) .startObject() .startObject("type") .startObject("properties") .startObject("point") .field("type", "geo_point") .field("geohash", true) .field("lat_lon", false) .endObject() .endObject() .endObject() .endObject() .string(); XContentDocumentMapper defaultMapper = XContentMapperTests.newParser().parse(mapping); ParsedDocument doc = defaultMapper.parse( "type", "1", XContentFactory.jsonBuilder() .startObject() .field("point", "1.2,1.3") .endObject() .copiedBytes()); MatcherAssert.assertThat(doc.doc().getField("point.lat"), nullValue()); MatcherAssert.assertThat(doc.doc().getField("point.lon"), nullValue()); MatcherAssert.assertThat( doc.doc().get("point.geohash"), equalTo(GeoHashUtils.encode(1.2, 1.3))); }
public void testMergingMappings() throws Exception { String enabledMapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("_field_names") .field("enabled", true) .endObject() .endObject() .endObject() .string(); String disabledMapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("_field_names") .field("enabled", false) .endObject() .endObject() .endObject() .string(); DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); DocumentMapper mapperEnabled = parser.parse(enabledMapping); DocumentMapper mapperDisabled = parser.parse(disabledMapping); mapperEnabled.merge(mapperDisabled, DocumentMapper.MergeFlags.mergeFlags().simulate(false)); assertFalse(mapperEnabled.rootMapper(FieldNamesFieldMapper.class).enabled()); mapperEnabled = parser.parse(enabledMapping); mapperDisabled.merge(mapperEnabled, DocumentMapper.MergeFlags.mergeFlags().simulate(false)); assertTrue(mapperEnabled.rootMapper(FieldNamesFieldMapper.class).enabled()); }
@Test public void testThatDisablingWorksWhenMerging() throws Exception { String enabledMapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("_size") .field("enabled", true) .endObject() .endObject() .endObject() .string(); DocumentMapper enabledMapper = MapperTests.newParser().parse(enabledMapping); String disabledMapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("_size") .field("enabled", false) .endObject() .endObject() .endObject() .string(); DocumentMapper disabledMapper = MapperTests.newParser().parse(disabledMapping); enabledMapper.merge(disabledMapper, DocumentMapper.MergeFlags.mergeFlags().simulate(false)); assertThat(enabledMapper.SizeFieldMapper().enabled(), is(false)); }
public void testCompressEnabled() throws Exception { String mapping = XContentFactory.jsonBuilder() .startObject() .startObject("type") .startObject("_source") .field("compress", true) .endObject() .endObject() .endObject() .string(); DocumentMapper documentMapper = createIndex("test", settings).mapperService().documentMapperParser().parse(mapping); ParsedDocument doc = documentMapper.parse( "test", "type", "1", XContentFactory.jsonBuilder() .startObject() .field("field1", "value1") .field("field2", "value2") .endObject() .bytes()); BytesRef bytes = doc.rootDoc().getBinaryValue("_source"); assertThat(CompressorFactory.isCompressed(new BytesArray(bytes)), equalTo(true)); }