|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.search.fetch.subphase; |
| 20 | + |
| 21 | +import org.apache.lucene.document.Document; |
| 22 | +import org.apache.lucene.document.SortedDocValuesField; |
| 23 | +import org.apache.lucene.index.DirectoryReader; |
| 24 | +import org.apache.lucene.index.IndexReader; |
| 25 | +import org.apache.lucene.index.IndexWriter; |
| 26 | +import org.apache.lucene.store.Directory; |
| 27 | +import org.apache.lucene.util.BytesRef; |
| 28 | +import org.elasticsearch.Version; |
| 29 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 30 | +import org.elasticsearch.common.settings.Settings; |
| 31 | +import org.elasticsearch.index.mapper.ContentPath; |
| 32 | +import org.elasticsearch.index.mapper.Mapper; |
| 33 | +import org.elasticsearch.index.mapper.ParentFieldMapper; |
| 34 | +import org.elasticsearch.test.ESTestCase; |
| 35 | + |
| 36 | +public class ParentFieldSubFetchPhaseTests extends ESTestCase { |
| 37 | + |
| 38 | + public void testGetParentId() throws Exception { |
| 39 | + ParentFieldMapper fieldMapper = createParentFieldMapper(); |
| 40 | + Directory directory = newDirectory(); |
| 41 | + IndexWriter indexWriter = new IndexWriter(directory, newIndexWriterConfig()); |
| 42 | + Document document = new Document(); |
| 43 | + document.add(new SortedDocValuesField(fieldMapper.fieldType().name(), new BytesRef("1"))); |
| 44 | + indexWriter.addDocument(document); |
| 45 | + indexWriter.close(); |
| 46 | + |
| 47 | + IndexReader indexReader = DirectoryReader.open(directory); |
| 48 | + String id = ParentFieldSubFetchPhase.getParentId(fieldMapper, indexReader.leaves().get(0).reader(), 0); |
| 49 | + assertEquals("1", id); |
| 50 | + |
| 51 | + indexReader.close(); |
| 52 | + directory.close(); |
| 53 | + } |
| 54 | + |
| 55 | + public void testGetParentIdNoParentField() throws Exception { |
| 56 | + ParentFieldMapper fieldMapper = createParentFieldMapper(); |
| 57 | + Directory directory = newDirectory(); |
| 58 | + IndexWriter indexWriter = new IndexWriter(directory, newIndexWriterConfig()); |
| 59 | + Document document = new Document(); |
| 60 | + document.add(new SortedDocValuesField("different_field", new BytesRef("1"))); |
| 61 | + indexWriter.addDocument(document); |
| 62 | + indexWriter.close(); |
| 63 | + |
| 64 | + IndexReader indexReader = DirectoryReader.open(directory); |
| 65 | + String id = ParentFieldSubFetchPhase.getParentId(fieldMapper, indexReader.leaves().get(0).reader(), 0); |
| 66 | + assertNull(id); |
| 67 | + |
| 68 | + indexReader.close(); |
| 69 | + directory.close(); |
| 70 | + } |
| 71 | + |
| 72 | + private ParentFieldMapper createParentFieldMapper() { |
| 73 | + Settings settings = Settings.builder() |
| 74 | + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) |
| 75 | + .build(); |
| 76 | + return new ParentFieldMapper.Builder("type") |
| 77 | + .type("parent_type") |
| 78 | + .build(new Mapper.BuilderContext(settings, new ContentPath(0))); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +} |
0 commit comments