Java Code Examples for org.elasticsearch.index.mapper.ParseContext#Document
The following examples show how to use
org.elasticsearch.index.mapper.ParseContext#Document .
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: EngineTestCase.java From crate with Apache License 2.0 | 6 votes |
protected static ParsedDocument testParsedDocument( String id, String routing, ParseContext.Document document, BytesReference source, Mapping mappingUpdate, boolean recoverySource) { Field uidField = new Field("_id", Uid.encodeId(id), IdFieldMapper.Defaults.FIELD_TYPE); Field versionField = new NumericDocValuesField("_version", 0); SeqNoFieldMapper.SequenceIDFields seqID = SeqNoFieldMapper.SequenceIDFields.emptySeqID(); document.add(uidField); document.add(versionField); document.add(seqID.seqNo); document.add(seqID.seqNoDocValue); document.add(seqID.primaryTerm); BytesRef ref = source.toBytesRef(); if (recoverySource) { document.add(new StoredField(SourceFieldMapper.RECOVERY_SOURCE_NAME, ref.bytes, ref.offset, ref.length)); document.add(new NumericDocValuesField(SourceFieldMapper.RECOVERY_SOURCE_NAME, 1)); } else { document.add(new StoredField(SourceFieldMapper.NAME, ref.bytes, ref.offset, ref.length)); } return new ParsedDocument(versionField, seqID, id, routing, Arrays.asList(document), source, mappingUpdate); }
Example 2
Source File: test.java From vscode-extension with MIT License | 6 votes |
private void updateDocs(final Term uid, final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException { if (softDeleteEnabled) { if (docs.size() > 1) { indexWriter.softUpdateDocuments(uid, docs, softDeletesField); } else { indexWriter.softUpdateDocument(uid, docs.get(0), softDeletesField); } } else { if (docs.size() > 1) { indexWriter.updateDocuments(uid, docs); } else { indexWriter.updateDocument(uid, docs.get(0)); } } numDocUpdates.inc(docs.size()); }
Example 3
Source File: MultiDocumentPercolatorIndex.java From Elasticsearch with Apache License 2.0 | 6 votes |
MemoryIndex indexDoc(ParseContext.Document d, Analyzer analyzer, MemoryIndex memoryIndex) { for (IndexableField field : d.getFields()) { if (field.fieldType().indexOptions() == IndexOptions.NONE && field.name().equals(UidFieldMapper.NAME)) { continue; } try { // TODO: instead of passing null here, we can have a CTL<Map<String,TokenStream>> and pass previous, // like the indexer does try (TokenStream tokenStream = field.tokenStream(analyzer, null)) { if (tokenStream != null) { memoryIndex.addField(field.name(), tokenStream, field.boost()); } } } catch (IOException e) { throw new ElasticsearchException("Failed to create token stream", e); } } return memoryIndex; }
Example 4
Source File: FieldNamesFieldMapper.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException { if (fieldType().isEnabled() == false) { return; } for (ParseContext.Document document : context.docs()) { final List<String> paths = new ArrayList<>(); for (IndexableField field : document.getFields()) { paths.add(field.name()); } for (String path : paths) { for (String fieldName : extractFieldNames(path)) { if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { document.add(new Field(fieldType().names().indexName(), fieldName, fieldType())); } } } } }
Example 5
Source File: RecoverySourceHandlerTests.java From crate with Apache License 2.0 | 6 votes |
private Engine.Index getIndex(final String id) { final String type = "test"; final ParseContext.Document document = new ParseContext.Document(); document.add(new TextField("test", "test", Field.Store.YES)); final Field idField = new Field("_id", Uid.encodeId(id), IdFieldMapper.Defaults.FIELD_TYPE); final Field versionField = new NumericDocValuesField("_version", Versions.MATCH_ANY); final SeqNoFieldMapper.SequenceIDFields seqID = SeqNoFieldMapper.SequenceIDFields.emptySeqID(); document.add(idField); document.add(versionField); document.add(seqID.seqNo); document.add(seqID.seqNoDocValue); document.add(seqID.primaryTerm); final BytesReference source = new BytesArray(new byte[] { 1 }); final ParsedDocument doc = new ParsedDocument(versionField, seqID, id, type, List.of(document), source, null); return new Engine.Index( new Term("_id", Uid.encodeId(doc.id())), doc, UNASSIGNED_SEQ_NO, 0, Versions.MATCH_ANY, VersionType.INTERNAL, PRIMARY, System.nanoTime(), -1, false, UNASSIGNED_SEQ_NO, 0); }
Example 6
Source File: InternalEngine.java From crate with Apache License 2.0 | 6 votes |
private void updateDocs(final Term uid, final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException { if (softDeleteEnabled) { if (docs.size() > 1) { indexWriter.softUpdateDocuments(uid, docs, softDeletesField); } else { indexWriter.softUpdateDocument(uid, docs.get(0), softDeletesField); } } else { if (docs.size() > 1) { indexWriter.updateDocuments(uid, docs); } else { indexWriter.updateDocument(uid, docs.get(0)); } } numDocUpdates.inc(docs.size()); }
Example 7
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 8
Source File: test.java From vscode-extension with MIT License | 5 votes |
private void addStaleDocs(final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException { assert softDeleteEnabled : "Add history documents but soft-deletes is disabled"; for (ParseContext.Document doc : docs) { doc.add(softDeletesField); // soft-deleted every document before adding to Lucene } if (docs.size() > 1) { indexWriter.addDocuments(docs); } else { indexWriter.addDocument(docs.get(0)); } }
Example 9
Source File: InternalEngine.java From crate with Apache License 2.0 | 5 votes |
private void addStaleDocs(final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException { assert softDeleteEnabled : "Add history documents but soft-deletes is disabled"; for (ParseContext.Document doc : docs) { doc.add(softDeletesField); // soft-deleted every document before adding to Lucene } if (docs.size() > 1) { indexWriter.addDocuments(docs); } else { indexWriter.addDocument(docs.get(0)); } }
Example 10
Source File: InternalEngine.java From crate with Apache License 2.0 | 5 votes |
private void addDocs(final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException { if (docs.size() > 1) { indexWriter.addDocuments(docs); } else { indexWriter.addDocument(docs.get(0)); } numDocAppends.inc(docs.size()); }
Example 11
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 12
Source File: ReferenceMapper.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 5 votes |
private static void parseCopyFields(ParseContext originalContext, List<String> copyToFields) throws IOException { if (!originalContext.isWithinCopyTo() && !copyToFields.isEmpty()) { ParseContext context = originalContext.createCopyToContext(); for (String field : copyToFields) { // In case of a hierarchy of nested documents, we need to figure out // which document the field should go to ParseContext.Document targetDoc = null; for (ParseContext.Document doc = context.doc(); doc != null; doc = doc.getParent()) { if (field.startsWith(doc.getPrefix())) { targetDoc = doc; break; } } if (targetDoc == null) { throw new IllegalArgumentException("target doc is null"); } final ParseContext copyToContext; if (targetDoc == context.doc()) { copyToContext = context; } else { copyToContext = context.switchDoc(targetDoc); } // simplified - no dynamic field creation FieldMapper fieldMapper = copyToContext.docMapper().mappers().getMapper(field); if (fieldMapper != null) { fieldMapper.parse(copyToContext); } else { throw new MapperParsingException("attempt to copy value to non-existing field [" + field + "]"); } } } }
Example 13
Source File: LangdetectMapper.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 5 votes |
@SuppressWarnings("unchecked") private static void parseLanguageToFields(ParseContext originalContext, Object languageToFields) throws IOException { List<Object> fieldList = languageToFields instanceof List ? (List<Object>)languageToFields : Collections.singletonList(languageToFields); ParseContext context = originalContext.createCopyToContext(); for (Object field : fieldList) { ParseContext.Document targetDoc = null; for (ParseContext.Document doc = context.doc(); doc != null; doc = doc.getParent()) { if (field.toString().startsWith(doc.getPrefix())) { targetDoc = doc; break; } } if (targetDoc == null) { throw new IllegalArgumentException("target doc is null"); } final ParseContext copyToContext; if (targetDoc == context.doc()) { copyToContext = context; } else { copyToContext = context.switchDoc(targetDoc); } FieldMapper fieldMapper = copyToContext.docMapper().mappers().getMapper(field.toString()); if (fieldMapper != null) { fieldMapper.parse(copyToContext); } else { throw new MapperParsingException("attempt to copy value to non-existing field [" + field + "]"); } } }
Example 14
Source File: test.java From vscode-extension with MIT License | 5 votes |
private void addDocs(final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException { if (docs.size() > 1) { indexWriter.addDocuments(docs); } else { indexWriter.addDocument(docs.get(0)); } numDocAppends.inc(docs.size()); }
Example 15
Source File: ArrayMapperTest.java From crate with Apache License 2.0 | 4 votes |
private static Set<String> uniqueValuesFromFields(ParseContext.Document fields, String fieldName) { return Stream.of(fields.getFields(fieldName)) .map(IndexableField::binaryValue) .map(BytesRef::utf8ToString) .collect(Collectors.toSet()); }
Example 16
Source File: EngineTestCase.java From crate with Apache License 2.0 | 4 votes |
protected static ParseContext.Document testDocumentWithTextField() { return testDocumentWithTextField("test"); }
Example 17
Source File: EngineTestCase.java From crate with Apache License 2.0 | 4 votes |
protected static ParseContext.Document testDocumentWithTextField(String value) { ParseContext.Document document = testDocument(); document.add(new TextField("value", value, Field.Store.YES)); return document; }
Example 18
Source File: EngineTestCase.java From crate with Apache License 2.0 | 4 votes |
protected static ParseContext.Document testDocument() { return new ParseContext.Document(); }
Example 19
Source File: EngineTestCase.java From crate with Apache License 2.0 | 4 votes |
protected static ParsedDocument testParsedDocument( String id, String routing, ParseContext.Document document, BytesReference source, Mapping mappingUpdate) { return testParsedDocument(id, routing, document, source, mappingUpdate, false); }
Example 20
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\"" + "}" + "}" + "}" + "}}")); }