org.elasticsearch.index.mapper.DocumentMapper Java Examples
The following examples show how to use
org.elasticsearch.index.mapper.DocumentMapper.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StandardnumberMappingTests.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 6 votes |
public void testNonStandardnumber() throws Exception { String mapping = copyToStringFromClasspath("mapping.json"); DocumentMapper docMapper = createIndex("some_index") .mapperService().documentMapperParser() .parse("someType", new CompressedXContent(mapping)); String sampleText = "Hello world"; BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder() .startObject().field("someField", sampleText).endObject()); SourceToParse sourceToParse = SourceToParse.source("some_index", "someType", "1", json, XContentType.JSON); ParseContext.Document doc = docMapper.parse(sourceToParse).rootDoc(); assertEquals(0, doc.getFields("someField").length); // re-parse it String builtMapping = docMapper.mappingSource().string(); logger.warn("testNonStandardnumber: built mapping =" + builtMapping); DocumentMapper docMapper2 = createIndex("some_index2") .mapperService().documentMapperParser() .parse("someType", new CompressedXContent(builtMapping)); json = BytesReference.bytes(XContentFactory.jsonBuilder().startObject() .field("someField", sampleText).endObject()); sourceToParse = SourceToParse.source("some_index2", "someType", "1", json, XContentType.JSON); doc = docMapper2.parse(sourceToParse).rootDoc(); assertEquals(0, doc.getFields("someField").length); }
Example #2
Source File: QueryTester.java From crate with Apache License 2.0 | 6 votes |
void indexValue(String column, Object value) throws IOException { DocumentMapper mapper = indexEnv.mapperService().documentMapperSafe(); InsertSourceGen sourceGen = InsertSourceGen.of( CoordinatorTxnCtx.systemTransactionContext(), sqlExecutor.functions(), table, table.concreteIndices()[0], GeneratedColumns.Validation.NONE, Collections.singletonList(table.getReference(ColumnIdent.fromPath(column))) ); BytesReference source = sourceGen.generateSourceAndCheckConstraintsAsBytesReference(new Object[]{value}); SourceToParse sourceToParse = new SourceToParse( table.concreteIndices()[0], UUIDs.randomBase64UUID(), source, XContentType.JSON ); ParsedDocument parsedDocument = mapper.parse(sourceToParse); indexEnv.writer().addDocuments(parsedDocument.docs()); }
Example #3
Source File: ArrayMapperTest.java From crate with Apache License 2.0 | 6 votes |
@Test public void testParseDynamicNullArray() throws Exception { String mapping = Strings.toString(XContentFactory.jsonBuilder() .startObject().startObject(TYPE).startObject("properties") .endObject().endObject().endObject()); DocumentMapper mapper = mapper(INDEX, mapping); // parse source with null array BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder() .startObject() .startArray("new_array_field").nullValue().endArray() .endObject()); SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON); ParsedDocument doc = mapper.parse(sourceToParse); assertThat(doc.docs().get(0).getField("new_array_field"), is(nullValue())); assertThat(mapper.mappers().getMapper("new_array_field"), is(nullValue())); }
Example #4
Source File: ArrayMapperTest.java From crate with Apache License 2.0 | 6 votes |
@Test public void testParseDynamicEmptyArray() throws Exception { String mapping = Strings.toString(XContentFactory.jsonBuilder() .startObject().startObject(TYPE).startObject("properties") .endObject().endObject().endObject()); DocumentMapper mapper = mapper(INDEX, mapping); // parse source with empty array BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder() .startObject() .array("new_array_field") .endObject()); SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON); ParsedDocument doc = mapper.parse(sourceToParse); assertThat(doc.docs().get(0).getField("new_array_field"), is(nullValue())); assertThat(mapper.mappers().getMapper("new_array_field"), is(nullValue())); }
Example #5
Source File: ArrayMapperTest.java From crate with Apache License 2.0 | 6 votes |
@Test public void testInvalidArrayNonConvertableType() throws Exception { 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", "double") .endObject() .endObject() .endObject().endObject().endObject()); DocumentMapper mapper = mapper(INDEX, mapping); expectedException.expect(MapperParsingException.class); expectedException.expectMessage("failed to parse field [array_field] of type [double]"); BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder() .startObject() .array("array_field", true, false, true) .endObject()); SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON); mapper.parse(sourceToParse); }
Example #6
Source File: ArrayMapperTest.java From crate with Apache License 2.0 | 6 votes |
/** * create index with type and mapping and validate DocumentMapper serialization */ private DocumentMapper mapper(String indexName, String mapping) throws IOException { IndicesModule indicesModule = new IndicesModule(Collections.singletonList(new MapperPlugin() { @Override public Map<String, Mapper.TypeParser> getMappers() { return Collections.singletonMap(ArrayMapper.CONTENT_TYPE, new ArrayTypeParser()); } })); MapperService mapperService = MapperTestUtils.newMapperService( NamedXContentRegistry.EMPTY, createTempDir(), Settings.EMPTY, indicesModule, indexName ); DocumentMapperParser parser = mapperService.documentMapperParser(); DocumentMapper defaultMapper = parser.parse(TYPE, new CompressedXContent(mapping)); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); builder.startObject(); defaultMapper.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); String rebuildMapping = Strings.toString(builder); return parser.parse(TYPE, new CompressedXContent(rebuildMapping)); }
Example #7
Source File: IndexShard.java From crate with Apache License 2.0 | 6 votes |
private EngineConfig.TombstoneDocSupplier tombstoneDocSupplier() { final RootObjectMapper.Builder noopRootMapper = new RootObjectMapper.Builder("__noop"); final DocumentMapper noopDocumentMapper = new DocumentMapper.Builder(noopRootMapper, mapperService).build(mapperService); return new EngineConfig.TombstoneDocSupplier() { @Override public ParsedDocument newDeleteTombstoneDoc(String id) { return defaultDocMapper().createDeleteTombstoneDoc(shardId.getIndexName(), id); } @Override public ParsedDocument newNoopTombstoneDoc(String reason) { return noopDocumentMapper.createNoopTombstoneDoc(shardId.getIndexName(), reason); } }; }
Example #8
Source File: IndexShard.java From crate with Apache License 2.0 | 6 votes |
public static Engine.Index prepareIndex(DocumentMapper docMapper, SourceToParse source, long seqNo, long primaryTerm, long version, VersionType versionType, Engine.Operation.Origin origin, long autoGeneratedIdTimestamp, boolean isRetry, long ifSeqNo, long ifPrimaryTerm) { long startTime = System.nanoTime(); ParsedDocument doc = docMapper.parse(source); Term uid = new Term(IdFieldMapper.NAME, Uid.encodeId(doc.id())); return new Engine.Index(uid, doc, seqNo, primaryTerm, version, versionType, origin, startTime, autoGeneratedIdTimestamp, isRetry, ifSeqNo, ifPrimaryTerm); }
Example #9
Source File: FieldsVisitor.java From Elasticsearch with Apache License 2.0 | 6 votes |
public void postProcess(DocumentMapper documentMapper) { for (Map.Entry<String, List<Object>> entry : fields().entrySet()) { String indexName = entry.getKey(); FieldMapper fieldMapper = documentMapper.mappers().getMapper(indexName); if (fieldMapper == null) { // it's possible index name doesn't match field name (legacy feature) for (FieldMapper mapper : documentMapper.mappers()) { if (mapper.fieldType().names().indexName().equals(indexName)) { fieldMapper = mapper; break; } } if (fieldMapper == null) { // no index name or full name found, so skip continue; } } List<Object> fieldValues = entry.getValue(); for (int i = 0; i < fieldValues.size(); i++) { fieldValues.set(i, fieldMapper.fieldType().valueForSearch(fieldValues.get(i))); } } }
Example #10
Source File: FieldsVisitor.java From Elasticsearch with Apache License 2.0 | 6 votes |
public void postProcess(MapperService mapperService) { if (uid != null) { DocumentMapper documentMapper = mapperService.documentMapper(uid.type()); if (documentMapper != null) { // we can derive the exact type for the mapping postProcess(documentMapper); return; } } // can't derive exact mapping type for (Map.Entry<String, List<Object>> entry : fields().entrySet()) { MappedFieldType fieldType = mapperService.indexName(entry.getKey()); if (fieldType == null) { continue; } List<Object> fieldValues = entry.getValue(); for (int i = 0; i < fieldValues.size(); i++) { fieldValues.set(i, fieldType.valueForSearch(fieldValues.get(i))); } } }
Example #11
Source File: ParentChildIndexFieldData.java From Elasticsearch with Apache License 2.0 | 6 votes |
public ParentChildIndexFieldData(Index index, Settings indexSettings, MappedFieldType.Names fieldNames, FieldDataType fieldDataType, IndexFieldDataCache cache, MapperService mapperService, CircuitBreakerService breakerService) { super(index, indexSettings, fieldNames, fieldDataType, cache); this.breakerService = breakerService; if (Version.indexCreated(indexSettings).before(Version.V_2_0_0_beta1)) { parentTypes = new TreeSet<>(); for (DocumentMapper documentMapper : mapperService.docMappers(false)) { beforeCreate(documentMapper); } mapperService.addTypeListener(this); } else { ImmutableSortedSet.Builder<String> builder = ImmutableSortedSet.naturalOrder(); for (DocumentMapper mapper : mapperService.docMappers(false)) { ParentFieldMapper parentFieldMapper = mapper.parentFieldMapper(); if (parentFieldMapper.active()) { builder.add(parentFieldMapper.type()); } } parentTypes = builder.build(); } }
Example #12
Source File: IndicesTTLService.java From Elasticsearch with Apache License 2.0 | 5 votes |
/** * Returns the shards to purge, i.e. the local started primary shards that have ttl enabled and disable_purge to false */ private List<IndexShard> getShardsToPurge() { List<IndexShard> shardsToPurge = new ArrayList<>(); MetaData metaData = clusterService.state().metaData(); for (IndexService indexService : indicesService) { // check the value of disable_purge for this index IndexMetaData indexMetaData = metaData.index(indexService.index().name()); if (indexMetaData == null) { continue; } boolean disablePurge = indexMetaData.getSettings().getAsBoolean(INDEX_TTL_DISABLE_PURGE, false); if (disablePurge) { continue; } // check if ttl is enabled for at least one type of this index boolean hasTTLEnabled = false; for (String type : indexService.mapperService().types()) { DocumentMapper documentType = indexService.mapperService().documentMapper(type); if (documentType.TTLFieldMapper().enabled()) { hasTTLEnabled = true; break; } } if (hasTTLEnabled) { for (IndexShard indexShard : indexService) { if (indexShard.state() == IndexShardState.STARTED && indexShard.routingEntry().primary() && indexShard.routingEntry().started()) { shardsToPurge.add(indexShard); } } } } return shardsToPurge; }
Example #13
Source File: InnerHitsParseElement.java From Elasticsearch with Apache License 2.0 | 5 votes |
private InnerHitsContext.ParentChildInnerHits parseParentChild(XContentParser parser, QueryParseContext parseContext, SearchContext searchContext, String type) throws Exception { ParseResult parseResult = parseSubSearchContext(searchContext, parseContext, parser); DocumentMapper documentMapper = searchContext.mapperService().documentMapper(type); if (documentMapper == null) { throw new IllegalArgumentException("type [" + type + "] doesn't exist"); } return new InnerHitsContext.ParentChildInnerHits(parseResult.context(), parseResult.query(), parseResult.childInnerHits(), parseContext.mapperService(), documentMapper); }
Example #14
Source File: ParentChildIndexFieldData.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public void beforeCreate(DocumentMapper mapper) { // Remove in 3.0 synchronized (lock) { ParentFieldMapper parentFieldMapper = mapper.parentFieldMapper(); if (parentFieldMapper.active()) { // A _parent field can never be added to an existing mapping, so a _parent field either exists on // a new created or doesn't exists. This is why we can update the known parent types via DocumentTypeListener if (parentTypes.add(parentFieldMapper.type())) { clear(); } } } }
Example #15
Source File: ReferenceMappingTests.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 5 votes |
public void testRefFromID() throws Exception { IndexService indexService = createIndex("docs", Settings.EMPTY, "docs", getMapping("ref-mapping-from-id.json")); DocumentMapper docMapper = indexService.mapperService().documentMapper("docs"); BytesReference json = BytesReference.bytes(jsonBuilder().startObject() .field("title", "A title") .field("authorID", "1") .endObject()); SourceToParse sourceToParse = SourceToParse.source("docs", "docs", "1", json, XContentType.JSON); ParseContext.Document doc = docMapper.parse(sourceToParse).rootDoc(); assertEquals(1, doc.getFields("ref").length, 1); assertEquals("John Doe", doc.getFields("ref")[0].stringValue()); }
Example #16
Source File: LangdetectMappingTests.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 5 votes |
public void testToFields() throws Exception { IndexService indexService = createIndex("some_index", Settings.EMPTY, "someType", getMapping("mapping-to-fields.json")); DocumentMapper docMapper = indexService.mapperService().documentMapper("someType"); String sampleText = copyToStringFromClasspath("english.txt"); BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder() .startObject().field("someField", sampleText).endObject()); SourceToParse sourceToParse = SourceToParse.source("some_index", "someType", "1", json, XContentType.JSON); ParsedDocument doc = docMapper.parse(sourceToParse); assertEquals(1, doc.rootDoc().getFields("someField").length); assertEquals("en", doc.rootDoc().getFields("someField")[0].stringValue()); assertEquals(1, doc.rootDoc().getFields("english_field").length); assertEquals("This is a very small example of a text", doc.rootDoc().getFields("english_field")[0].stringValue()); }
Example #17
Source File: LangdetectMappingTests.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 5 votes |
public void testSimpleMapping() throws Exception { IndexService indexService = createIndex("some_index", Settings.EMPTY, "someType", getMapping("simple-mapping.json")); DocumentMapper docMapper = indexService.mapperService().documentMapper("someType"); String sampleText = copyToStringFromClasspath("english.txt"); BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder() .startObject().field("someField", sampleText).endObject()); SourceToParse sourceToParse = SourceToParse.source("some_index", "someType", "1", json, XContentType.JSON); ParsedDocument doc = docMapper.parse(sourceToParse); assertEquals(1, doc.rootDoc().getFields("someField").length); assertEquals("en", doc.rootDoc().getFields("someField")[0].stringValue()); }
Example #18
Source File: IndicesClusterStateService.java From Elasticsearch with Apache License 2.0 | 5 votes |
private boolean processMapping(String index, MapperService mapperService, String mappingType, CompressedXContent mappingSource, long mappingVersion) throws Throwable { // refresh mapping can happen when the parsing/merging of the mapping from the metadata doesn't result in the same // mapping, in this case, we send to the master to refresh its own version of the mappings (to conform with the // merge version of it, which it does when refreshing the mappings), and warn log it. boolean requiresRefresh = false; try { DocumentMapper existingMapper = mapperService.documentMapper(mappingType); if (existingMapper == null || existingMapper.mappingVersion() != mappingVersion || mappingSource.equals(existingMapper.mappingSource()) == false) { String op = existingMapper == null ? "adding" : "updating"; if (logger.isDebugEnabled() && mappingSource.compressed().length < 512) { logger.debug("[{}] {} mapping [{}], source [{}]", index, op, mappingType, mappingSource.string()); } else if (logger.isTraceEnabled()) { logger.trace("[{}] {} mapping [{}], source [{}]", index, op, mappingType, mappingSource.string()); } else { logger.debug("[{}] {} mapping [{}] (source suppressed due to length, use TRACE level if needed)", index, op, mappingType); } mapperService.merge(mappingType, mappingSource, MapperService.MergeReason.MAPPING_RECOVERY, true, true, mappingVersion); if (!mapperService.documentMapper(mappingType).mappingSource().equals(mappingSource)) { logger.debug("[{}] parsed mapping [{}], and got different sources\noriginal:\n{}\nparsed:\n{}", index, mappingType, mappingSource, mapperService.documentMapper(mappingType).mappingSource()); requiresRefresh = true; } } } catch (Throwable e) { logger.warn("[{}] failed to add mapping [{}], source [{}]", e, index, mappingType, mappingSource); throw e; } return requiresRefresh; }
Example #19
Source File: StandardnumberMappingTests.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 5 votes |
public void testSimpleStandardNumber() throws Exception { String mapping = copyToStringFromClasspath("mapping.json"); DocumentMapper docMapper = createIndex("some_index") .mapperService().documentMapperParser() .parse("someType", new CompressedXContent(mapping)); String sampleText = "978-3-551-75213-0"; BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder().startObject() .field("someField", sampleText).endObject()); SourceToParse sourceToParse = SourceToParse.source("some_index", "someType", "1", json, XContentType.JSON); ParseContext.Document doc = docMapper.parse(sourceToParse).rootDoc(); assertEquals(2, doc.getFields("someField").length); assertEquals("978-3-551-75213-0", doc.getFields("someField")[0].stringValue()); assertEquals("9783551752130", doc.getFields("someField")[1].stringValue()); }
Example #20
Source File: MappingMetaData.java From Elasticsearch with Apache License 2.0 | 5 votes |
public MappingMetaData(DocumentMapper docMapper, long mappingVersion) { this.type = docMapper.type(); this.source = docMapper.mappingSource(); this.id = new Id(docMapper.idFieldMapper().path()); this.routing = new Routing(docMapper.routingFieldMapper().required(), docMapper.routingFieldMapper().path()); this.timestamp = new Timestamp(docMapper.timestampFieldMapper().enabled(), docMapper.timestampFieldMapper().path(), docMapper.timestampFieldMapper().fieldType().dateTimeFormatter().format(), docMapper.timestampFieldMapper().defaultTimestamp(), docMapper.timestampFieldMapper().ignoreMissing()); this.version = new ParsedVersion(docMapper.versionFieldMapper().path(), docMapper.versionFieldMapper().VersionType()); this.hasParentField = docMapper.parentFieldMapper().active(); this.mappingVersion = mappingVersion; }
Example #21
Source File: ArrayMapperTest.java From crate with Apache License 2.0 | 5 votes |
@Test public void testParseNull() 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", "double") .endObject() .endObject() .endObject() .endObject() .endObject()); // @formatter: on 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())); }
Example #22
Source File: ArrayMapperTest.java From crate with Apache License 2.0 | 5 votes |
@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())); }
Example #23
Source File: IndexShard.java From Elasticsearch with Apache License 2.0 | 5 votes |
public Engine.Delete prepareDeleteOnPrimary(String type, String id, long version, VersionType versionType) { if (shardRouting.primary() == false) { throw new IllegalIndexShardStateException(shardId, state, "shard is not a primary"); } final DocumentMapper documentMapper = docMapper(type).getDocumentMapper(); return prepareDelete(type, id, documentMapper.uidMapper().term(Uid.createUid(type, id)), version, versionType, Engine.Operation .Origin.PRIMARY); }
Example #24
Source File: FetchPhase.java From Elasticsearch with Apache License 2.0 | 4 votes |
private InternalSearchHit.InternalNestedIdentity getInternalNestedIdentity(SearchContext context, int nestedSubDocId, LeafReaderContext subReaderContext, DocumentMapper documentMapper, ObjectMapper nestedObjectMapper) throws IOException { int currentParent = nestedSubDocId; ObjectMapper nestedParentObjectMapper; ObjectMapper current = nestedObjectMapper; String originalName = nestedObjectMapper.name(); InternalSearchHit.InternalNestedIdentity nestedIdentity = null; do { Query parentFilter; nestedParentObjectMapper = documentMapper.findParentObjectMapper(current); if (nestedParentObjectMapper != null) { if (nestedParentObjectMapper.nested().isNested() == false) { current = nestedParentObjectMapper; continue; } parentFilter = nestedParentObjectMapper.nestedTypeFilter(); } else { parentFilter = Queries.newNonNestedFilter(); } Query childFilter = nestedObjectMapper.nestedTypeFilter(); if (childFilter == null) { current = nestedParentObjectMapper; continue; } final Weight childWeight = context.searcher().createNormalizedWeight(childFilter, false); Scorer childScorer = childWeight.scorer(subReaderContext); if (childScorer == null) { current = nestedParentObjectMapper; continue; } DocIdSetIterator childIter = childScorer.iterator(); BitSet parentBits = context.bitsetFilterCache().getBitSetProducer(parentFilter).getBitSet(subReaderContext); int offset = 0; int nextParent = parentBits.nextSetBit(currentParent); for (int docId = childIter.advance(currentParent + 1); docId < nextParent && docId != DocIdSetIterator.NO_MORE_DOCS; docId = childIter.nextDoc()) { offset++; } currentParent = nextParent; current = nestedObjectMapper = nestedParentObjectMapper; int currentPrefix = current == null ? 0 : current.name().length() + 1; nestedIdentity = new InternalSearchHit.InternalNestedIdentity(originalName.substring(currentPrefix), offset, nestedIdentity); if (current != null) { originalName = current.name(); } } while (current != null); return nestedIdentity; }
Example #25
Source File: ChildrenParser.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException { String childType = null; XContentParser.Token token; String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.VALUE_STRING) { if ("type".equals(currentFieldName)) { childType = parser.text(); } else { throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].", parser.getTokenLocation()); } } else { throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].", parser.getTokenLocation()); } } if (childType == null) { throw new SearchParseException(context, "Missing [child_type] field for children aggregation [" + aggregationName + "]", parser.getTokenLocation()); } ValuesSourceConfig<ValuesSource.Bytes.WithOrdinals.ParentChild> config = new ValuesSourceConfig<>(ValuesSource.Bytes.WithOrdinals.ParentChild.class); DocumentMapper childDocMapper = context.mapperService().documentMapper(childType); String parentType = null; Query parentFilter = null; Query childFilter = null; if (childDocMapper != null) { ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper(); if (!parentFieldMapper.active()) { throw new SearchParseException(context, "[children] no [_parent] field not configured that points to a parent type", parser.getTokenLocation()); } parentType = parentFieldMapper.type(); DocumentMapper parentDocMapper = context.mapperService().documentMapper(parentType); if (parentDocMapper != null) { // TODO: use the query API parentFilter = parentDocMapper.typeFilter(); childFilter = childDocMapper.typeFilter(); ParentChildIndexFieldData parentChildIndexFieldData = context.fieldData().getForField(parentFieldMapper.fieldType()); config.fieldContext(new FieldContext(parentFieldMapper.fieldType().names().indexName(), parentChildIndexFieldData, parentFieldMapper.fieldType())); } else { config.unmapped(true); } } else { config.unmapped(true); } return new ParentToChildrenAggregator.Factory(aggregationName, config, parentType, parentFilter, childFilter); }
Example #26
Source File: ArrayMapperTest.java From crate with Apache License 2.0 | 4 votes |
@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, true); 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\"" + "}" + "}" + "}" + "}" + "}}}")); }
Example #27
Source File: ExportCollector.java From elasticsearch-inout-plugin with Apache License 2.0 | 4 votes |
@Override public void collect(int doc) throws IOException { fieldsVisitor.reset(); currentReader.document(doc, fieldsVisitor); Map<String, SearchHitField> searchFields = null; if (fieldsVisitor.fields() != null) { searchFields = new HashMap<String, SearchHitField>(fieldsVisitor.fields().size()); for (Map.Entry<String, List<Object>> entry : fieldsVisitor.fields().entrySet()) { searchFields.put(entry.getKey(), new InternalSearchHitField(entry.getKey(), entry.getValue())); } } DocumentMapper documentMapper = context.mapperService() .documentMapper(fieldsVisitor.uid().type()); Text typeText; if (documentMapper == null) { typeText = new StringAndBytesText(fieldsVisitor.uid().type()); } else { typeText = documentMapper.typeText(); } InternalSearchHit searchHit = new InternalSearchHit(doc, fieldsVisitor.uid().id(), typeText, sourceRequested ? fieldsVisitor.source() : null, searchFields); for (FetchSubPhase fetchSubPhase : fetchSubPhases) { FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext(); if (fetchSubPhase.hitExecutionNeeded(context)) { hitContext.reset(searchHit, arc, doc, context.searcher().getIndexReader(), doc, fieldsVisitor); fetchSubPhase.hitExecute(context, hitContext); } } searchHit.shardTarget(context.shardTarget()); exportFields.hit(searchHit); BytesStreamOutput os = new BytesStreamOutput(); XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(XContentType.JSON), os); exportFields.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.flush(); BytesReference bytes = os.bytes(); out.write(bytes.array(), bytes.arrayOffset(), bytes.length()); out.write('\n'); out.flush(); numExported++; }
Example #28
Source File: ArrayMapperTest.java From crate with Apache License 2.0 | 4 votes |
@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\"" + "}" + "}" + "}" + "}" + "}}}")); }
Example #29
Source File: InnerHitsContext.java From Elasticsearch with Apache License 2.0 | 4 votes |
public ParentChildInnerHits(SearchContext context, ParsedQuery query, Map<String, BaseInnerHits> childInnerHits, MapperService mapperService, DocumentMapper documentMapper) { super(context, query, childInnerHits); this.mapperService = mapperService; this.documentMapper = documentMapper; }
Example #30
Source File: ArrayMapperTest.java From crate with Apache License 2.0 | 4 votes |
@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\"" + "}" + "}" + "}" + "}}")); }