Add support for enum fields during indexing (#1792)

* Add support for enum fields during indexing

* Add missing value extractor for enums
This commit is contained in:
Sebastian Sdorra
2021-09-01 15:40:38 +02:00
committed by GitHub
parent 9ad501b4c6
commit ea7964d224
5 changed files with 102 additions and 0 deletions

View File

@@ -200,6 +200,13 @@ class TypeConvertersTest {
assertThat(document.getField("lastName")).isNull();
}
@Test
void shouldCreateEnumField() {
Document document = convert(new TypeWithEnum(Color.GREEN));
assertThat(document.get("color")).isEqualTo("GREEN");
}
private void assertPointField(Document document, String name, Consumer<IndexableField> consumer) {
IndexableField[] fields = document.getFields(name);
assertThat(fields)
@@ -291,4 +298,19 @@ class TypeConvertersTest {
this.storedOnlyInstant = instant;
}
}
@Getter
@IndexedType
private static class TypeWithEnum {
@Indexed
private final Color color;
private TypeWithEnum(Color color) {
this.color = color;
}
}
private enum Color {
GREEN, RED, BLUE, YELLOW
}
}

View File

@@ -0,0 +1,50 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.search;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.StoredField;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ValueExtractorsTest {
@Test
void shouldExtractEnumValue() {
Document document = new Document();
document.add(new StoredField("animal", "PENGUIN"));
ValueExtractor extractor = ValueExtractors.create("animal", Animal.class);
Object value = extractor.extract(document);
assertThat(value).isEqualTo(Animal.PENGUIN);
}
enum Animal {
PENGUIN, ALPACA
}
}