|
@@ -29,10 +29,11 @@ import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
|
|
|
-public abstract class ParseContext {
|
|
|
+public abstract class ParseContext implements Iterable<ParseContext.Document>{
|
|
|
|
|
|
/** Fork of {@link org.apache.lucene.document.Document} with additional functionality. */
|
|
|
public static class Document implements Iterable<IndexableField> {
|
|
@@ -171,6 +172,11 @@ public abstract class ParseContext {
|
|
|
this.in = in;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Iterable<Document> nonRootDocuments() {
|
|
|
+ return in.nonRootDocuments();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public DocumentMapperParser docMapperParser() {
|
|
|
return in.docMapperParser();
|
|
@@ -211,11 +217,6 @@ public abstract class ParseContext {
|
|
|
return in.rootDoc();
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public List<Document> docs() {
|
|
|
- return in.docs();
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public Document doc() {
|
|
|
return in.doc();
|
|
@@ -280,6 +281,11 @@ public abstract class ParseContext {
|
|
|
public List<Mapper> getDynamicMappers() {
|
|
|
return in.getDynamicMappers();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Iterator<Document> iterator() {
|
|
|
+ return in.iterator();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public static class InternalParseContext extends ParseContext {
|
|
@@ -309,9 +315,10 @@ public abstract class ParseContext {
|
|
|
|
|
|
private long numNestedDocs;
|
|
|
|
|
|
-
|
|
|
private final List<Mapper> dynamicMappers;
|
|
|
|
|
|
+ private boolean docsReversed = false;
|
|
|
+
|
|
|
public InternalParseContext(@Nullable Settings indexSettings, DocumentMapperParser docMapperParser, DocumentMapper docMapper,
|
|
|
SourceToParse source, XContentParser parser) {
|
|
|
this.indexSettings = indexSettings;
|
|
@@ -360,8 +367,7 @@ public abstract class ParseContext {
|
|
|
return documents.get(0);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public List<Document> docs() {
|
|
|
+ List<Document> docs() {
|
|
|
return this.documents;
|
|
|
}
|
|
|
|
|
@@ -426,8 +432,35 @@ public abstract class ParseContext {
|
|
|
public List<Mapper> getDynamicMappers() {
|
|
|
return dynamicMappers;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Iterable<Document> nonRootDocuments() {
|
|
|
+ if (docsReversed) {
|
|
|
+ throw new IllegalStateException("documents are already reversed");
|
|
|
+ }
|
|
|
+ return documents.subList(1, documents.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ void postParse() {
|
|
|
+ // reverse the order of docs for nested docs support, parent should be last
|
|
|
+ if (documents.size() > 1) {
|
|
|
+ docsReversed = true;
|
|
|
+ Collections.reverse(documents);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Iterator<Document> iterator() {
|
|
|
+ return documents.iterator();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns an Iterable over all non-root documents. If there are no non-root documents
|
|
|
+ * the iterable will return an empty iterator.
|
|
|
+ */
|
|
|
+ public abstract Iterable<Document> nonRootDocuments();
|
|
|
+
|
|
|
public abstract DocumentMapperParser docMapperParser();
|
|
|
|
|
|
/**
|
|
@@ -506,8 +539,6 @@ public abstract class ParseContext {
|
|
|
|
|
|
public abstract Document rootDoc();
|
|
|
|
|
|
- public abstract List<Document> docs();
|
|
|
-
|
|
|
public abstract Document doc();
|
|
|
|
|
|
protected abstract void addDoc(Document doc);
|