Add the ability to store the index name in the doc, closes #292.

This commit is contained in:
kimchy 2010-08-04 11:41:56 +03:00
parent a44d30bb61
commit d72de60b6f
14 changed files with 359 additions and 29 deletions

View File

@ -56,6 +56,8 @@ public interface DocumentMapper {
TypeFieldMapper typeMapper();
IndexFieldMapper indexMapper();
SourceFieldMapper sourceMapper();
BoostFieldMapper boostMapper();

View File

@ -0,0 +1,41 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.mapper;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.elasticsearch.common.util.concurrent.ThreadSafe;
/**
* A mapper that maps the index name of the resource into the document.
*
* @author kimchy (shay.banon)
*/
@ThreadSafe
public interface IndexFieldMapper extends FieldMapper<String>, InternalMapper {
public static final String NAME = "_index";
boolean enabled();
String value(Document document);
Term term(String value);
}

View File

@ -81,7 +81,7 @@ public class MapperService extends AbstractIndexComponent implements Iterable<Do
@Inject public MapperService(Index index, @IndexSettings Settings indexSettings, Environment environment, AnalysisService analysisService) {
super(index, indexSettings);
this.documentParser = new XContentDocumentMapperParser(analysisService);
this.documentParser = new XContentDocumentMapperParser(index, indexSettings, analysisService);
this.searchAnalyzer = new SmartIndexNameSearchAnalyzer(analysisService.defaultSearchAnalyzer());
this.dynamic = componentSettings.getAsBoolean("dynamic", true);

View File

@ -39,6 +39,8 @@ public class ParseContext {
private Document document;
private String index;
private String type;
private byte[] source;
@ -61,7 +63,8 @@ public class ParseContext {
private AllEntries allEntries = new AllEntries();
public ParseContext(XContentDocumentMapper docMapper, ContentPath path) {
public ParseContext(String index, XContentDocumentMapper docMapper, ContentPath path) {
this.index = index;
this.docMapper = docMapper;
this.path = path;
}
@ -86,6 +89,10 @@ public class ParseContext {
this.mappersAdded = true;
}
public String index() {
return this.index;
}
public String type() {
return this.type;
}

View File

@ -51,6 +51,8 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
private XContentTypeFieldMapper typeFieldMapper = new XContentTypeFieldMapper();
private XContentIndexFieldMapper indexFieldMapper = new XContentIndexFieldMapper();
private XContentSourceFieldMapper sourceFieldMapper = new XContentSourceFieldMapper();
private XContentBoostFieldMapper boostFieldMapper = new XContentBoostFieldMapper();
@ -61,6 +63,8 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
private NamedAnalyzer searchAnalyzer;
private final String index;
private final XContentObjectMapper rootObjectMapper;
private String mappingSource;
@ -69,7 +73,8 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
private XContentMapper.BuilderContext builderContext = new XContentMapper.BuilderContext(new ContentPath(1));
public Builder(XContentObjectMapper.Builder builder) {
public Builder(String index, XContentObjectMapper.Builder builder) {
this.index = index;
this.rootObjectMapper = builder.build(builderContext);
}
@ -98,6 +103,11 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
return this;
}
public Builder indexField(XContentIndexFieldMapper.Builder builder) {
this.indexFieldMapper = builder.build(builderContext);
return this;
}
public Builder boostField(XContentBoostFieldMapper.Builder builder) {
this.boostFieldMapper = builder.build(builderContext);
return this;
@ -133,7 +143,7 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
public XContentDocumentMapper build() {
Preconditions.checkNotNull(rootObjectMapper, "Mapper builder must have the root object mapper set");
return new XContentDocumentMapper(rootObjectMapper, attributes, uidFieldMapper, idFieldMapper, typeFieldMapper,
return new XContentDocumentMapper(index, rootObjectMapper, attributes, uidFieldMapper, idFieldMapper, typeFieldMapper, indexFieldMapper,
sourceFieldMapper, allFieldMapper, indexAnalyzer, searchAnalyzer, boostFieldMapper, mappingSource);
}
}
@ -141,10 +151,12 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
private ThreadLocal<ThreadLocals.CleanableValue<ParseContext>> cache = new ThreadLocal<ThreadLocals.CleanableValue<ParseContext>>() {
@Override protected ThreadLocals.CleanableValue<ParseContext> initialValue() {
return new ThreadLocals.CleanableValue<ParseContext>(new ParseContext(XContentDocumentMapper.this, new ContentPath(0)));
return new ThreadLocals.CleanableValue<ParseContext>(new ParseContext(index, XContentDocumentMapper.this, new ContentPath(0)));
}
};
private final String index;
private final String type;
private volatile ImmutableMap<String, Object> attributes;
@ -157,6 +169,8 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
private final XContentTypeFieldMapper typeFieldMapper;
private final XContentIndexFieldMapper indexFieldMapper;
private final XContentSourceFieldMapper sourceFieldMapper;
private final XContentBoostFieldMapper boostFieldMapper;
@ -175,16 +189,18 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
private final Object mutex = new Object();
public XContentDocumentMapper(XContentObjectMapper rootObjectMapper,
public XContentDocumentMapper(String index, XContentObjectMapper rootObjectMapper,
ImmutableMap<String, Object> attributes,
XContentUidFieldMapper uidFieldMapper,
XContentIdFieldMapper idFieldMapper,
XContentTypeFieldMapper typeFieldMapper,
XContentIndexFieldMapper indexFieldMapper,
XContentSourceFieldMapper sourceFieldMapper,
XContentAllFieldMapper allFieldMapper,
Analyzer indexAnalyzer, Analyzer searchAnalyzer,
@Nullable XContentBoostFieldMapper boostFieldMapper,
@Nullable String mappingSource) {
this.index = index;
this.type = rootObjectMapper.name();
this.attributes = attributes;
this.mappingSource = mappingSource;
@ -192,6 +208,7 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
this.uidFieldMapper = uidFieldMapper;
this.idFieldMapper = idFieldMapper;
this.typeFieldMapper = typeFieldMapper;
this.indexFieldMapper = indexFieldMapper;
this.sourceFieldMapper = sourceFieldMapper;
this.allFieldMapper = allFieldMapper;
this.boostFieldMapper = boostFieldMapper;
@ -252,6 +269,10 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
return this.idFieldMapper;
}
@Override public IndexFieldMapper indexMapper() {
return this.indexFieldMapper;
}
@Override public TypeFieldMapper typeMapper() {
return this.typeFieldMapper;
}
@ -333,6 +354,8 @@ public class XContentDocumentMapper implements DocumentMapper, ToXContent {
}
typeFieldMapper.parse(context);
indexFieldMapper.parse(context);
rootObjectMapper.parse(context);
// if we did not get the id, we need to parse the uid into the document now, after it was added

View File

@ -24,13 +24,18 @@ import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.collect.Maps;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentMerger;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.AbstractIndexComponent;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.settings.IndexSettings;
import javax.annotation.Nullable;
import java.io.IOException;
@ -43,7 +48,7 @@ import static org.elasticsearch.index.mapper.xcontent.XContentTypeParsers.*;
/**
* @author kimchy (shay.banon)
*/
public class XContentDocumentMapperParser implements DocumentMapperParser {
public class XContentDocumentMapperParser extends AbstractIndexComponent implements DocumentMapperParser {
private final AnalysisService analysisService;
@ -53,7 +58,12 @@ public class XContentDocumentMapperParser implements DocumentMapperParser {
private volatile ImmutableMap<String, XContentTypeParser> typeParsers;
public XContentDocumentMapperParser(AnalysisService analysisService) {
public XContentDocumentMapperParser(Index index, AnalysisService analysisService) {
this(index, ImmutableSettings.Builder.EMPTY_SETTINGS, analysisService);
}
public XContentDocumentMapperParser(Index index, @IndexSettings Settings indexSettings, AnalysisService analysisService) {
super(index, indexSettings);
this.analysisService = analysisService;
typeParsers = new MapBuilder<String, XContentTypeParser>()
.put(XContentShortFieldMapper.CONTENT_TYPE, new XContentShortFieldMapper.TypeParser())
@ -112,7 +122,7 @@ public class XContentDocumentMapperParser implements DocumentMapperParser {
XContentTypeParser.ParserContext parserContext = new XContentTypeParser.ParserContext(mapping, analysisService, typeParsers);
XContentDocumentMapper.Builder docBuilder = doc((XContentObjectMapper.Builder) rootObjectTypeParser.parse(type, mapping, parserContext));
XContentDocumentMapper.Builder docBuilder = doc(index.name(), (XContentObjectMapper.Builder) rootObjectTypeParser.parse(type, mapping, parserContext));
for (Map.Entry<String, Object> entry : mapping.entrySet()) {
String fieldName = Strings.toUnderscoreCase(entry.getKey());
@ -122,6 +132,8 @@ public class XContentDocumentMapperParser implements DocumentMapperParser {
docBuilder.sourceField(parseSourceField((Map<String, Object>) fieldNode, parserContext));
} else if (XContentIdFieldMapper.CONTENT_TYPE.equals(fieldName) || "idField".equals(fieldName)) {
docBuilder.idField(parseIdField((Map<String, Object>) fieldNode, parserContext));
} else if (XContentIndexFieldMapper.CONTENT_TYPE.equals(fieldName) || "indexField".equals(fieldName)) {
docBuilder.indexField(parseIndexField((Map<String, Object>) fieldNode, parserContext));
} else if (XContentTypeFieldMapper.CONTENT_TYPE.equals(fieldName) || "typeField".equals(fieldName)) {
docBuilder.typeField(parseTypeField((Map<String, Object>) fieldNode, parserContext));
} else if (XContentUidFieldMapper.CONTENT_TYPE.equals(fieldName) || "uidField".equals(fieldName)) {
@ -217,6 +229,20 @@ public class XContentDocumentMapperParser implements DocumentMapperParser {
return builder;
}
private XContentIndexFieldMapper.Builder parseIndexField(Map<String, Object> indexNode, XContentTypeParser.ParserContext parserContext) {
XContentIndexFieldMapper.Builder builder = XContentMapperBuilders.index();
parseField(builder, builder.name, indexNode, parserContext);
for (Map.Entry<String, Object> entry : indexNode.entrySet()) {
String fieldName = Strings.toUnderscoreCase(entry.getKey());
Object fieldNode = entry.getValue();
if (fieldName.equals("enabled")) {
builder.enabled(nodeBooleanValue(fieldNode));
}
}
return builder;
}
private Tuple<String, Map<String, Object>> extractMapping(String type, String source) throws MapperParsingException {
Map<String, Object> root;
XContentParser xContentParser = null;

View File

@ -0,0 +1,141 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.mapper.xcontent;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.Term;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
import org.elasticsearch.index.mapper.IndexFieldMapper;
import org.elasticsearch.index.mapper.MergeMappingException;
import java.io.IOException;
/**
* @author kimchy (shay.banon)
*/
public class XContentIndexFieldMapper extends XContentFieldMapper<String> implements IndexFieldMapper {
public static final String CONTENT_TYPE = "_index";
public static class Defaults extends XContentFieldMapper.Defaults {
public static final String NAME = IndexFieldMapper.NAME;
public static final String INDEX_NAME = IndexFieldMapper.NAME;
public static final Field.Index INDEX = Field.Index.NOT_ANALYZED;
public static final Field.Store STORE = Field.Store.NO;
public static final boolean OMIT_NORMS = true;
public static final boolean OMIT_TERM_FREQ_AND_POSITIONS = true;
public static final boolean ENABLED = false;
}
public static class Builder extends XContentFieldMapper.Builder<Builder, XContentIndexFieldMapper> {
private boolean enabled = Defaults.ENABLED;
public Builder() {
super(Defaults.NAME);
indexName = Defaults.INDEX_NAME;
index = Defaults.INDEX;
store = Defaults.STORE;
omitNorms = Defaults.OMIT_NORMS;
omitTermFreqAndPositions = Defaults.OMIT_TERM_FREQ_AND_POSITIONS;
}
public Builder enabled(boolean enabled) {
this.enabled = enabled;
return this;
}
@Override public XContentIndexFieldMapper build(BuilderContext context) {
return new XContentIndexFieldMapper(name, indexName, store, termVector, boost, omitNorms, omitTermFreqAndPositions, enabled);
}
}
private final boolean enabled;
protected XContentIndexFieldMapper() {
this(Defaults.NAME, Defaults.INDEX_NAME);
}
protected XContentIndexFieldMapper(String name, String indexName) {
this(name, indexName, Defaults.STORE, Defaults.TERM_VECTOR, Defaults.BOOST,
Defaults.OMIT_NORMS, Defaults.OMIT_TERM_FREQ_AND_POSITIONS, Defaults.ENABLED);
}
public XContentIndexFieldMapper(String name, String indexName, Field.Store store, Field.TermVector termVector,
float boost, boolean omitNorms, boolean omitTermFreqAndPositions, boolean enabled) {
super(new Names(name, indexName, indexName, name), Defaults.INDEX, store, termVector, boost, omitNorms, omitTermFreqAndPositions,
Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER);
this.enabled = enabled;
}
@Override public boolean enabled() {
return this.enabled;
}
@Override public String value(Document document) {
Fieldable field = document.getFieldable(names.indexName());
return field == null ? null : value(field);
}
@Override public String value(Fieldable field) {
return field.stringValue();
}
@Override public String valueFromString(String value) {
return value;
}
@Override public String valueAsString(Fieldable field) {
return value(field);
}
@Override public String indexedValue(String value) {
return value;
}
@Override public Term term(String value) {
return new Term(names.indexName(), value);
}
@Override protected Field parseCreateField(ParseContext context) throws IOException {
if (!enabled) {
return null;
}
return new Field(names.indexName(), context.index(), store, index);
}
@Override protected String contentType() {
return CONTENT_TYPE;
}
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(CONTENT_TYPE);
builder.field("store", store.name().toLowerCase());
builder.field("enabled", enabled);
builder.endObject();
}
@Override public void merge(XContentMapper mergeWith, MergeContext mergeContext) throws MergeMappingException {
// do nothing here, no merging, but also no exception
}
}

View File

@ -28,8 +28,8 @@ public final class XContentMapperBuilders {
}
public static XContentDocumentMapper.Builder doc(XContentObjectMapper.Builder objectBuilder) {
return new XContentDocumentMapper.Builder(objectBuilder);
public static XContentDocumentMapper.Builder doc(String index, XContentObjectMapper.Builder objectBuilder) {
return new XContentDocumentMapper.Builder(index, objectBuilder);
}
public static XContentSourceFieldMapper.Builder source() {
@ -48,6 +48,10 @@ public final class XContentMapperBuilders {
return new XContentTypeFieldMapper.Builder();
}
public static XContentIndexFieldMapper.Builder index() {
return new XContentIndexFieldMapper.Builder();
}
public static XContentBoostFieldMapper.Builder boost(String name) {
return new XContentBoostFieldMapper.Builder(name);
}

View File

@ -31,7 +31,7 @@ import org.elasticsearch.index.mapper.MapperService;
public class XContentMapperTests {
public static XContentDocumentMapperParser newParser() {
return new XContentDocumentMapperParser(new AnalysisService(new Index("test")));
return new XContentDocumentMapperParser(new Index("test"), new AnalysisService(new Index("test")));
}
public static MapperService newMapperService() {

View File

@ -0,0 +1,90 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.mapper.xcontent.index;
import org.apache.lucene.document.Field;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.xcontent.XContentDocumentMapper;
import org.elasticsearch.index.mapper.xcontent.XContentMapperTests;
import org.testng.annotations.Test;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
/**
* @author kimchy (shay.banon)
*/
public class IndexTypeMapperTests {
@Test public void simpleIndexMapperTests() throws Exception {
String mapping = XContentFactory.contentTextBuilder(XContentType.JSON).startObject().startObject("type")
.startObject("_index").field("enabled", true).field("store", "yes").endObject()
.endObject().endObject().string();
XContentDocumentMapper docMapper = XContentMapperTests.newParser().parse(mapping);
assertThat(docMapper.indexMapper().enabled(), equalTo(true));
assertThat(docMapper.indexMapper().store(), equalTo(Field.Store.YES));
ParsedDocument doc = docMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field", "value")
.endObject()
.copiedBytes());
assertThat(doc.doc().get("_index"), equalTo("test"));
assertThat(doc.doc().get("field"), equalTo("value"));
}
@Test public void explicitDisabledIndexMapperTests() throws Exception {
String mapping = XContentFactory.contentTextBuilder(XContentType.JSON).startObject().startObject("type")
.startObject("_index").field("enabled", false).field("store", "yes").endObject()
.endObject().endObject().string();
XContentDocumentMapper docMapper = XContentMapperTests.newParser().parse(mapping);
assertThat(docMapper.indexMapper().enabled(), equalTo(false));
assertThat(docMapper.indexMapper().store(), equalTo(Field.Store.YES));
ParsedDocument doc = docMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field", "value")
.endObject()
.copiedBytes());
assertThat(doc.doc().get("_index"), nullValue());
assertThat(doc.doc().get("field"), equalTo("value"));
}
@Test public void defaultDisabledIndexMapperTests() throws Exception {
String mapping = XContentFactory.contentTextBuilder(XContentType.JSON).startObject().startObject("type")
.endObject().endObject().string();
XContentDocumentMapper docMapper = XContentMapperTests.newParser().parse(mapping);
assertThat(docMapper.indexMapper().enabled(), equalTo(false));
assertThat(docMapper.indexMapper().store(), equalTo(Field.Store.NO));
ParsedDocument doc = docMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field", "value")
.endObject()
.copiedBytes());
assertThat(doc.doc().get("_index"), nullValue());
assertThat(doc.doc().get("field"), equalTo("value"));
}
}

View File

@ -69,7 +69,7 @@ public class XContentMultiFieldTests {
}
@Test public void testBuildThenParse() throws Exception {
XContentDocumentMapper builderDocMapper = doc(object("person").add(
XContentDocumentMapper builderDocMapper = doc("test", object("person").add(
multiField("name")
.add(stringField("name").store(Field.Store.YES))
.add(stringField("indexed").index(Field.Index.ANALYZED))

View File

@ -21,10 +21,8 @@ package org.elasticsearch.index.mapper.xcontent.multifield.merge;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.mapper.xcontent.XContentDocumentMapper;
import org.elasticsearch.index.mapper.xcontent.XContentDocumentMapperParser;
import org.elasticsearch.index.mapper.xcontent.XContentMapperTests;
import org.testng.annotations.Test;
import static org.elasticsearch.common.io.Streams.*;
@ -40,7 +38,7 @@ public class JavaMultiFieldMergeTests {
@Test public void testMergeMultiField() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/multifield/merge/test-mapping1.json");
XContentDocumentMapper docMapper = (XContentDocumentMapper) new XContentDocumentMapperParser(new AnalysisService(new Index("test"))).parse(mapping);
XContentDocumentMapper docMapper = XContentMapperTests.newParser().parse(mapping);
assertThat(docMapper.mappers().fullName("name").mapper().indexed(), equalTo(true));
assertThat(docMapper.mappers().fullName("name.indexed"), nullValue());
@ -54,7 +52,7 @@ public class JavaMultiFieldMergeTests {
mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/multifield/merge/test-mapping2.json");
XContentDocumentMapper docMapper2 = (XContentDocumentMapper) new XContentDocumentMapperParser(new AnalysisService(new Index("test"))).parse(mapping);
XContentDocumentMapper docMapper2 = XContentMapperTests.newParser().parse(mapping);
docMapper.merge(docMapper2, mergeFlags().simulate(true));

View File

@ -21,11 +21,9 @@ package org.elasticsearch.index.mapper.xcontent.simple;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.index.mapper.xcontent.XContentDocumentMapper;
import org.elasticsearch.index.mapper.xcontent.XContentDocumentMapperParser;
import org.elasticsearch.index.mapper.xcontent.XContentMapperTests;
import org.testng.annotations.Test;
import static org.apache.lucene.document.Field.Store.*;
@ -40,7 +38,7 @@ import static org.hamcrest.Matchers.*;
public class SimpleXContentMapperTests {
@Test public void testSimpleMapper() throws Exception {
XContentDocumentMapper docMapper = doc(
XContentDocumentMapper docMapper = doc("test",
object("person")
.add(object("name").add(stringField("first").store(YES).index(Field.Index.NO)))
).sourceField(source()).build();
@ -60,11 +58,11 @@ public class SimpleXContentMapperTests {
@Test public void testParseToJsonAndParse() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/simple/test-mapping.json");
XContentDocumentMapper docMapper = (XContentDocumentMapper) new XContentDocumentMapperParser(new AnalysisService(new Index("test"))).parse(mapping);
XContentDocumentMapper docMapper = XContentMapperTests.newParser().parse(mapping);
String builtMapping = docMapper.buildSource();
// System.out.println(builtMapping);
// reparse it
XContentDocumentMapper builtDocMapper = (XContentDocumentMapper) new XContentDocumentMapperParser(new AnalysisService(new Index("test"))).parse(builtMapping);
XContentDocumentMapper builtDocMapper = XContentMapperTests.newParser().parse(builtMapping);
byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/simple/test1.json");
Document doc = builtDocMapper.parse(json).doc();
assertThat(doc.get(docMapper.uidMapper().names().indexName()), equalTo(Uid.createUid("person", "1")));
@ -77,7 +75,7 @@ public class SimpleXContentMapperTests {
@Test public void testSimpleParser() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/simple/test-mapping.json");
XContentDocumentMapper docMapper = (XContentDocumentMapper) new XContentDocumentMapperParser(new AnalysisService(new Index("test"))).parse(mapping);
XContentDocumentMapper docMapper = XContentMapperTests.newParser().parse(mapping);
assertThat((String) docMapper.attributes().get("param1"), equalTo("value1"));
@ -93,7 +91,7 @@ public class SimpleXContentMapperTests {
@Test public void testSimpleParserNoTypeNoId() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/simple/test-mapping.json");
XContentDocumentMapper docMapper = (XContentDocumentMapper) new XContentDocumentMapperParser(new AnalysisService(new Index("test"))).parse(mapping);
XContentDocumentMapper docMapper = XContentMapperTests.newParser().parse(mapping);
byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/simple/test1-notype-noid.json");
Document doc = docMapper.parse("person", "1", json).doc();
assertThat(doc.get(docMapper.uidMapper().names().indexName()), equalTo(Uid.createUid("person", "1")));

View File

@ -39,13 +39,13 @@ public class SimpleAttachmentMapperTests {
private XContentDocumentMapperParser mapperParser;
@BeforeTest public void setupMapperParser() {
mapperParser = new XContentDocumentMapperParser(new AnalysisService(new Index("test")));
mapperParser = new XContentDocumentMapperParser(new Index("test"), new AnalysisService(new Index("test")));
mapperParser.putTypeParser(XContentAttachmentMapper.CONTENT_TYPE, new XContentAttachmentMapper.TypeParser());
}
@Test public void testSimpleMappings() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/test-mapping.json");
XContentDocumentMapper docMapper = (XContentDocumentMapper) mapperParser.parse(mapping);
XContentDocumentMapper docMapper = mapperParser.parse(mapping);
byte[] json = jsonBuilder().startObject().field("_id", 1).field("file", copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/testXHTML.html")).endObject().copiedBytes();
Document doc = docMapper.parse(json).doc();
@ -55,7 +55,7 @@ public class SimpleAttachmentMapperTests {
// re-parse it
String builtMapping = docMapper.buildSource();
docMapper = (XContentDocumentMapper) mapperParser.parse(builtMapping);
docMapper = mapperParser.parse(builtMapping);
json = jsonBuilder().startObject().field("_id", 1).field("file", copyToBytesFromClasspath("/org/elasticsearch/index/mapper/xcontent/testXHTML.html")).endObject().copiedBytes();