Skip to content

Commit

Permalink
Rename flat-object to flat_object and fix CI tests
Browse files Browse the repository at this point in the history
Signed-off-by: Mingshi Liu <[email protected]>
  • Loading branch information
mingshl committed Mar 22, 2023
1 parent 3eb4cf5 commit 44c3b5e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed
- Require MediaType in Strings.toString API ([#6009](https:/opensearch-project/OpenSearch/pull/6009))
- [Refactor] XContent base classes from xcontent to core library ([#5902](https:/opensearch-project/OpenSearch/pull/5902))
- Introduce a new field type: flat-object (TODO: update the link!) ([#1018](https:/opensearch-project/OpenSearch/issues/1018))
- Introduce a new field type: flat_object ([#6507](https:/opensearch-project/OpenSearch/pull/6507))

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private static void GetFlatObjectIndex(MyState state, String indexName, String f
.startObject()
.startObject("properties")
.startObject(flatObjectFieldName)
.field("type", "flat-object")
.field("type", "flat_object")
.endObject()
.endObject()
.endObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,21 @@
import static org.opensearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES;

/**
* A field mapper for flat-objects. This mapper accepts JSON object and treat as string fields in one index.
* A field mapper for flat_objects. This mapper accepts JSON object and treat as string fields in one index.
* A flat_object field contains one parent field itself and two substring fields:
* field._valueAndPath and field._value
* @opensearch.internal
*/
public final class FlatObjectFieldMapper extends DynamicKeyFieldMapper {

/**
* A flat-object mapping contains one parent field itself and two substring fields,
* field._valueAndPath and field._value
*/

public static final String CONTENT_TYPE = "flat-object";
public static final String CONTENT_TYPE = "flat_object";
private static final String VALUE_AND_PATH_SUFFIX = "._valueAndPath";
private static final String VALUE_SUFFIX = "._value";
private static final String DOT_SYMBOL = ".";
private static final String EQUAL_SYMBOL = "=";

/**
* In flat-object field mapper, field type is similar to keyword field type
* In flat_object field mapper, field type is similar to keyword field type
* Cannot be tokenized, can OmitNorms, and can setIndexOption.
* @opensearch.internal
*/
Expand Down Expand Up @@ -108,8 +105,8 @@ private static FlatObjectFieldMapper toType(FieldMapper in) {
}

/**
* The builder for the flat-object field mapper using default parameters as
* indexed: flat-object field mapper is default to be indexed.
* The builder for the flat_object field mapper using default parameters as
* indexed: flat_object field mapper is default to be indexed.
* hasDocValues: to store index and support efficient access to individual field values.
* stored: the original value of the field is not stored in the index.
* nullValue: not accept null value
Expand Down Expand Up @@ -199,7 +196,7 @@ public Mapper.Builder<?> parse(String name, Map<String, Object> node, ParserCont
}

/**
* flat-object fields type contains its own fieldType, one valueFieldType and one valueAndPathFieldType
* flat_object fields type contains its own fieldType, one valueFieldType and one valueAndPathFieldType
* @opensearch.internal
*/
public static final class FlatObjectFieldType extends StringFieldType {
Expand Down Expand Up @@ -313,15 +310,15 @@ public Object valueForDisplay(Object value) {
if (value == null) {
return null;
}
// flat-objects are internally stored as utf8 bytes
// flat_objects are internally stored as utf8 bytes
BytesRef binaryValue = (BytesRef) value;
return binaryValue.utf8ToString();
}

@Override
protected BytesRef indexedValueForSearch(Object value) {
if (getTextSearchInfo().getSearchAnalyzer() == Lucene.KEYWORD_ANALYZER) {
// flat-object analyzer with the default attribute source which encodes terms using UTF8
// flat_object analyzer with the default attribute source which encodes terms using UTF8
// in that case we skip normalization, which may be slow if there many terms need to
// parse (eg. large terms query) since Analyzer.normalize involves things like creating
// attributes through reflection
Expand All @@ -339,7 +336,7 @@ protected BytesRef indexedValueForSearch(Object value) {
}

/**
* redirect term query with rewrite value to rewriteSearchValue and directSubFieldName
* redirect queries with rewrite value to rewriteSearchValue and directSubFieldName
*/
@Override
public Query termQuery(Object value, @Nullable QueryShardContext context) {
Expand Down Expand Up @@ -473,15 +470,11 @@ public Query wildcardQuery(
boolean caseInsensitve,
QueryShardContext context
) {
// flat-object field types are always normalized, so ignore case sensitivity and force normalize the wildcard
// flat_object field types are always normalized, so ignore case sensitivity and force normalize the wildcard
// query text
throw new QueryShardException(
context,
"Can only use wildcard queries on keyword and text fields - not on ["
+ name()
+ "] which is of type ["
+ "flat-object"
+ "]"
"Can only use wildcard queries on keyword and text fields - not on [" + name() + "] which is of type [" + typeName() + "]"
);
}

Expand Down Expand Up @@ -542,6 +535,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
);
/**
* JsonToStringParser is the main parser class to transform JSON into stringFields in a XContentParser
* It reads the JSON object and parsed to a list of string
*/
XContentParser parser = JsonToStringParser.parseObject();

Expand Down Expand Up @@ -580,6 +574,15 @@ public Iterator<Mapper> iterator() {
return concat;
}

/**
* parseValueAddFields method will store data to Lucene.
* the JsonToStringXContentParser returns XContentParser with 3 string fields
* fieldName, fieldName._value, fieldName._valueAndPath.
* parseValueAddFields recognized string by the stringfield name,
* fieldName will be store through the parent FlatObjectFieldMapper,which contains all the keys
* fieldName._value will be store through the valueFieldMapper, which contains the values of the Json Object
* fieldName._valueAndPath will be store through the valueAndPathFieldMapper, which contains the "path=values" format
*/
private void parseValueAddFields(ParseContext context, String value, String fieldName) throws IOException {

NamedAnalyzer normalizer = fieldType().normalizer();
Expand All @@ -590,11 +593,6 @@ private void parseValueAddFields(ParseContext context, String value, String fiel
String[] valueTypeList = fieldName.split("\\._");
String valueType = "._" + valueTypeList[valueTypeList.length - 1];

/**
* the JsonToStringXContentParser returns XContentParser with 3 string fields
* fieldName, fieldName._value, fieldName._valueAndPath
*/

if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) {
// convert to utf8 only once before feeding postings/dv/stored fields

Expand All @@ -604,13 +602,6 @@ private void parseValueAddFields(ParseContext context, String value, String fiel
if (fieldType().hasDocValues() == false && fieldType.omitNorms()) {
createFieldNamesField(context);
}
/**
* Indentified by the stringfield name,
* fieldName will be store through the parent FlatFieldMapper,which contains all the keys
* fieldName._value will be store through the valueFieldMapper, which contains the values of the Json Object
* fieldName._valueAndPath will be store through the valueAndPathFieldMapper, which contains the values of
* the Json Object.
*/
if (fieldName.equals(fieldType().name())) {
context.doc().add(field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import static org.hamcrest.core.StringContains.containsString;

public class FlatObjectFieldMapperTests extends MapperServiceTestCase {
private static final String FIELD_TYPE = "flat-object";
private static final String FIELD_TYPE = "flat_object";

// @Override
public FlatObjectFieldMapper.Builder newBuilder() {
return new FlatObjectFieldMapper.Builder("flat-object");
return new FlatObjectFieldMapper.Builder(FIELD_TYPE);
}

public final void testExistsQueryDocValuesDisabledWithNorms() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.opensearch.index.analysis.AnalyzerScope;
import org.opensearch.index.analysis.NamedAnalyzer;
import org.opensearch.index.mapper.BinaryFieldMapper;
import org.opensearch.index.mapper.FlatObjectFieldMapper;
import org.opensearch.index.mapper.MappedFieldType;
import org.opensearch.index.mapper.NumberFieldMapper;
import org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType;
Expand Down Expand Up @@ -103,7 +104,8 @@ protected List<String> unsupportedMappedFieldTypes() {
NumberFieldMapper.NumberType.DOUBLE.typeName(), // floating points are not supported at all
NumberFieldMapper.NumberType.FLOAT.typeName(),
NumberFieldMapper.NumberType.HALF_FLOAT.typeName(),
BinaryFieldMapper.CONTENT_TYPE // binary fields are not supported because they cannot be searched
BinaryFieldMapper.CONTENT_TYPE, // binary fields are not supported because they cannot be searched
FlatObjectFieldMapper.CONTENT_TYPE // flat_object fields does not support aggregations
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.opensearch.index.analysis.AnalyzerScope;
import org.opensearch.index.analysis.NamedAnalyzer;
import org.opensearch.index.mapper.BinaryFieldMapper;
import org.opensearch.index.mapper.FlatObjectFieldMapper;
import org.opensearch.index.mapper.GeoPointFieldMapper;
import org.opensearch.index.mapper.MappedFieldType;
import org.opensearch.index.mapper.TextFieldMapper;
Expand Down Expand Up @@ -102,7 +103,8 @@ protected List<ValuesSourceType> getSupportedValuesSourceTypes() {
protected List<String> unsupportedMappedFieldTypes() {
return Arrays.asList(
BinaryFieldMapper.CONTENT_TYPE, // binary fields are not supported because they do not have analyzers
GeoPointFieldMapper.CONTENT_TYPE // geopoint fields cannot use term queries
GeoPointFieldMapper.CONTENT_TYPE, // geopoint fields cannot use term queries
FlatObjectFieldMapper.CONTENT_TYPE // flat_object fields are not supported aggregations
);
}

Expand Down

0 comments on commit 44c3b5e

Please sign in to comment.