Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable copy_to in multi_fields #15152

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,6 @@ private static Mapper parseObjectOrField(ParseContext context, Mapper mapper) th
} else {
FieldMapper fieldMapper = (FieldMapper)mapper;
Mapper update = fieldMapper.parse(context);
if (fieldMapper.copyTo() != null) {
parseCopyFields(context, fieldMapper, fieldMapper.copyTo().copyToFields());
}
return update;
}
}
Expand Down Expand Up @@ -683,7 +680,7 @@ private static ObjectMapper parseDynamicValue(final ParseContext context, Object
}

/** Creates instances of the fields that the current field should be copied to */
private static void parseCopyFields(ParseContext context, FieldMapper fieldMapper, List<String> copyToFields) throws IOException {
public static void parseCopyFields(ParseContext context, FieldMapper fieldMapper, List<String> copyToFields) throws IOException {
if (!context.isWithinCopyTo() && copyToFields.isEmpty() == false) {
context = context.createCopyToContext();
for (String field : copyToFields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ public Mapper parse(ParseContext context) throws IOException {
throw new MapperParsingException("failed to parse [" + fieldType().names().fullName() + "]", e);
}
multiFields.parse(this, context);
if (copyTo() != null) {
DocumentParser.parseCopyFields(context, this, this.copyTo().copyToFields());
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

import java.io.IOException;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.hamcrest.Matchers.equalTo;

/**
Expand Down Expand Up @@ -92,4 +94,26 @@ private XContentBuilder createDynamicTemplateMapping() throws IOException {
.endArray();
}

public void testCopyToWithinMultiField() throws IOException {
String mapping = jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("copy_test")
.field("type", "string")
.startObject("fields")
.startObject("raw")
.field("type", "string")
.field("copy_to", "another_field")
.endObject()
.endObject()
.endObject()
.endObject().endObject().endObject().string();
assertAcked(
client().admin().indices().prepareCreate("test-idx")
.addMapping("type", mapping)
);
client().prepareIndex("test-idx", "type").setSource("{\"copy_test\":\"foo bar\"}").get();
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.termQuery("another_field", "foo")).get();
assertSearchResponse(searchResponse);
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.index.mapper.copyto;

import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -40,6 +41,7 @@
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.junit.Test;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -358,4 +360,21 @@ private void assertFieldValue(Document doc, String field, Number... expected) {
assertArrayEquals(expected, actual);
}

@Test
public void testCopyToInMultiFields() throws Exception {
String mapping = jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("copy_test")
.field("type", "string")
.startObject("fields")
.startObject("raw")
.field("type", "string")
.field("copy_to", "another_field")
.endObject()
.endObject()
.endObject()
.endObject().endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
ParsedDocument doc = docMapper.parse("test", "type", "1", new BytesArray("{\"copy_test\":\"foo bar\"}"));
assertThat(doc.docs().get(0).getFields("another_field")[0].stringValue(), equalTo("foo bar"));
}
}