Skip to content

Commit

Permalink
iter
Browse files Browse the repository at this point in the history
  • Loading branch information
javanna committed Oct 23, 2023
1 parent b575b73 commit df33843
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.lucene.search.join.ScoreMode;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.TransportVersions;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
Expand All @@ -40,6 +41,7 @@
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Tuple;
Expand All @@ -53,6 +55,7 @@
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.SourceToParse;
import org.elasticsearch.index.mapper.TestDocumentParserContext;
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.BoostingQueryBuilder;
import org.elasticsearch.index.query.ConstantScoreQueryBuilder;
Expand All @@ -73,9 +76,11 @@
import org.elasticsearch.join.query.HasChildQueryBuilder;
import org.elasticsearch.join.query.HasParentQueryBuilder;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.SearchPlugin;
import org.elasticsearch.script.MockScriptPlugin;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.DummyQueryParserPlugin;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.test.InternalSettingsPlugin;
import org.elasticsearch.xcontent.XContentBuilder;
Expand All @@ -94,6 +99,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -132,7 +138,13 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
return pluginList(InternalSettingsPlugin.class, PercolatorPlugin.class, FoolMeScriptPlugin.class, ParentJoinPlugin.class);
return pluginList(
InternalSettingsPlugin.class,
PercolatorPlugin.class,
FoolMeScriptPlugin.class,
ParentJoinPlugin.class,
CustomQueriesPlugin.class
);
}

@Override
Expand Down Expand Up @@ -542,7 +554,7 @@ public void testPercolatorFieldMapper() throws Exception {
assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name()).get(0).stringValue(), equalTo(EXTRACTION_FAILED));
}

public void testParseScriptQueryWithParams() throws Exception {
public void testParseScriptScoreQueryWithParams() throws Exception {
addQueryFieldMappings();
ScriptScoreQueryBuilder scriptScoreQueryBuilder = new ScriptScoreQueryBuilder(
new MatchAllQueryBuilder(),
Expand All @@ -559,6 +571,21 @@ public void testParseScriptQueryWithParams() throws Exception {
assertNotNull(doc);
}

public void testParseCustomParserQuery() throws Exception {
addQueryFieldMappings();
ParsedDocument doc = mapperService.documentMapper()
.parse(
new SourceToParse(
"1",
BytesReference.bytes(
XContentFactory.jsonBuilder().startObject().field(fieldName, new CustomParserQueryBuilder()).endObject()
),
XContentType.JSON
)
);
assertNotNull(doc);
}

public void testStoringQueries() throws Exception {
addQueryFieldMappings();
QueryBuilder[] queries = new QueryBuilder[] {
Expand Down Expand Up @@ -1133,4 +1160,138 @@ public String pluginScriptLang() {
return Script.DEFAULT_SCRIPT_LANG;
}
}

public static class CustomQueriesPlugin extends Plugin implements SearchPlugin {
@Override
public List<QuerySpec<?>> getQueries() {
return Collections.singletonList(
new QuerySpec<QueryBuilder>(
CustomParserQueryBuilder.NAME,
CustomParserQueryBuilder::new,
CustomParserQueryBuilder::fromXContent
)
);
}
}

public static final class CustomParserQueryBuilder extends AbstractQueryBuilder<CustomParserQueryBuilder> {
private static final String NAME = "CUSTOM";

CustomParserQueryBuilder() {}

CustomParserQueryBuilder(StreamInput in) throws IOException {
super(in);
}

@Override
protected void doWriteTo(StreamOutput out) {
// only the superclass has state
}

@Override
protected Query doToQuery(SearchExecutionContext context) {
return new DummyQueryParserPlugin.DummyQuery();
}

@Override
protected int doHashCode() {
return 0;
}

@Override
protected boolean doEquals(CustomParserQueryBuilder other) {
return true;
}

@Override
public TransportVersion getMinimalSupportedVersion() {
return TransportVersions.ZERO;
}

@Override
public String getWriteableName() {
return NAME;
}

@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.array("list", "value0", "value1", "value2");
builder.array("listOrdered", "value0", "value1", "value2");
builder.field("map");
builder.map(Map.of("key1", "value1", "key2", "value2"));
builder.field("mapOrdered");
builder.map(Map.of("key3", "value3", "key4", "value4"));
builder.field("mapStrings");
builder.map(Map.of("key5", "value5", "key6", "value6"));
builder.field("mapSupplier");
builder.map(Map.of("key7", "value7", "key8", "value8"));
builder.endObject();
}

public static CustomParserQueryBuilder fromXContent(XContentParser parser) throws IOException {
{
assertEquals("list", parser.nextFieldName());
List<Object> list = parser.list();
assertEquals(3, list.size());
for (int i = 0; i < 3; i++) {
assertEquals("value" + i, list.get(i).toString());
}
assertEquals(XContentParser.Token.END_ARRAY, parser.currentToken());
}
{
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("listOrdered", parser.currentName());
List<Object> listOrdered = parser.listOrderedMap();
assertEquals(3, listOrdered.size());
for (int i = 0; i < 3; i++) {
assertEquals("value" + i, listOrdered.get(i).toString());
}
assertEquals(XContentParser.Token.END_ARRAY, parser.currentToken());
}
{
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("map", parser.currentName());
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
Map<String, Object> map = parser.map();
assertEquals(2, map.size());
assertEquals("value1", map.get("key1").toString());
assertEquals("value2", map.get("key2").toString());
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
}
{
assertEquals("mapOrdered", parser.currentName());
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
Map<String, Object> mapOrdered = parser.mapOrdered();
assertEquals(2, mapOrdered.size());
assertEquals("value3", mapOrdered.get("key3").toString());
assertEquals("value4", mapOrdered.get("key4").toString());
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
}
{
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("mapStrings", parser.currentName());
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
Map<String, Object> mapStrings = parser.map();
assertEquals(2, mapStrings.size());
assertEquals("value5", mapStrings.get("key5").toString());
assertEquals("value6", mapStrings.get("key6").toString());
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
}
{
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals("mapSupplier", parser.currentName());
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
Map<String, Object> mapSupplier = parser.map(HashMap::new, XContentParser::text);
assertEquals(2, mapSupplier.size());
assertEquals("value7", mapSupplier.get("key7").toString());
assertEquals("value8", mapSupplier.get("key8").toString());
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
}

assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
return new CustomParserQueryBuilder();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ protected XContentParser delegate() {
return parsers.peek();
}

/*
The following methods (map* and list*) are known not be called by DocumentParser when parsing documents, but we support indexing
percolator queries which are also parsed through DocumentParser, and their parsing code is completely up to each query, which are
also pluggable. That means that this parser needs to fully support parsing arbitrary content, when dots expansion is turned off.
We do throw UnsupportedOperationException when dots expansion is enabled as we don't expect such methods to be ever called in
those circumstances.
*/

@Override
public String nextFieldName() throws IOException {
if (contentPath.isWithinLeafObject()) {
return super.nextFieldName();
}
throw new UnsupportedOperationException();
}

@Override
public Map<String, Object> map() throws IOException {
if (contentPath.isWithinLeafObject()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.xcontent.json.JsonXContent;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -359,6 +360,38 @@ public void testParseMapUOE() throws Exception {
expectThrows(UnsupportedOperationException.class, dotExpandedParser::map);
}

public void testParseMapOrderedUOE() throws Exception {
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
createParser(JsonXContent.jsonXContent, ""),
new ContentPath()
);
expectThrows(UnsupportedOperationException.class, dotExpandedParser::mapOrdered);
}

public void testParseMapStringsUOE() throws Exception {
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
createParser(JsonXContent.jsonXContent, ""),
new ContentPath()
);
expectThrows(UnsupportedOperationException.class, dotExpandedParser::mapStrings);
}

public void testParseMapSupplierUOE() throws Exception {
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
createParser(JsonXContent.jsonXContent, ""),
new ContentPath()
);
expectThrows(UnsupportedOperationException.class, () -> dotExpandedParser.map(HashMap::new, XContentParser::text));
}

public void testNextFieldNameUOE() throws Exception {
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
createParser(JsonXContent.jsonXContent, ""),
new ContentPath()
);
expectThrows(UnsupportedOperationException.class, () -> dotExpandedParser.nextFieldName());
}

public void testParseMap() throws Exception {
String jsonInput = """
{"params":{"one":"one",
Expand Down Expand Up @@ -389,6 +422,14 @@ public void testParseListUOE() throws Exception {
expectThrows(UnsupportedOperationException.class, dotExpandedParser::list);
}

public void testParseListOrderedUOE() throws Exception {
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
createParser(JsonXContent.jsonXContent, ""),
new ContentPath()
);
expectThrows(UnsupportedOperationException.class, dotExpandedParser::listOrderedMap);
}

public void testParseList() throws Exception {
String jsonInput = """
{"params":["one","two"]}\
Expand Down

0 comments on commit df33843

Please sign in to comment.