_constrctQueryParam()

in lib/protocol/encoder.js [1026:1313]


    _constrctQueryParam(queryType, aQuery) {
        switch (queryType) {
            case TableStore.QueryType.MATCH_QUERY://1
                var properties = {
                    fieldName: aQuery.fieldName,
                    text: aQuery.text,
                    weight: aQuery.weight,
                };
                if (typeof aQuery.minimumShouldMatch === "number") {
                    properties.minimumShouldMatch = aQuery.minimumShouldMatch;
                }
                if (aQuery.operator) {
                    properties.operator = aQuery.operator
                }

                var matchQuery = tsSearchProtos.MatchQuery.create(properties);

                return matchQuery;
                break;
            case TableStore.QueryType.MATCH_PHRASE_QUERY://2
                var properties = {
                    fieldName: aQuery.fieldName,
                    text: aQuery.text,
                    weight: aQuery.weight,
                };
                var matchPhraseQuery = tsSearchProtos.MatchPhraseQuery.create(properties);

                return matchPhraseQuery;
                break;
            case TableStore.QueryType.TERM_QUERY://3
                var properties = {
                    fieldName: aQuery.fieldName,
                    term: TableStore.PlainBufferBuilder.serializeSearchValue(aQuery.term, "term"),
                    weight: aQuery.weight,
                };
                var termQuery = tsSearchProtos.TermQuery.create(properties);

                return termQuery;
                break;
            case TableStore.QueryType.RANGE_QUERY://4
                var properties = {
                    fieldName: aQuery.fieldName
                };
                if (aQuery.rangeFrom !== undefined && aQuery.rangeFrom !== null) {
                    properties.rangeFrom= TableStore.PlainBufferBuilder.serializeSearchValue(aQuery.rangeFrom, "rangeFrom");
                    if (aQuery.includeLower !== undefined) {
                        properties.includeLower = aQuery.includeLower;
                    }
                }
                if (aQuery.rangeTo !== undefined && aQuery.rangeTo !== null) {
                    properties.rangeTo= TableStore.PlainBufferBuilder.serializeSearchValue(aQuery.rangeTo, "rangeTo");
                    if (aQuery.includeUpper !== undefined) {
                        properties.includeUpper = aQuery.includeUpper;
                    }
                }

                var rangeQuery = tsSearchProtos.RangeQuery.create(properties);

                return rangeQuery;
                break;
            case TableStore.QueryType.PREFIX_QUERY://5
                var properties = {
                    fieldName: aQuery.fieldName,
                    prefix: aQuery.prefix,
                    weight: aQuery.weight,
                };

                var prefixQuery = tsSearchProtos.PrefixQuery.create(properties);

                return prefixQuery;
                break;
            case TableStore.QueryType.BOOL_QUERY://6
                var properties = {};

                if (Object.prototype.toString.call(aQuery.mustQueries) === '[object Array]') {
                    properties.mustQueries = [];
                    TableStore.util.arrayEach(aQuery.mustQueries, function (query) {
                        properties.mustQueries.push(TableStore.encoder._makeQuery(query));
                    })
                }
                if (Object.prototype.toString.call(aQuery.mustNotQueries) === '[object Array]') {
                    properties.mustNotQueries = [];
                    TableStore.util.arrayEach(aQuery.mustNotQueries, function (query) {
                        properties.mustNotQueries.push(TableStore.encoder._makeQuery(query));
                    })
                }
                if (Object.prototype.toString.call(aQuery.filterQueries) === '[object Array]') {
                    properties.filterQueries = [];
                    TableStore.util.arrayEach(aQuery.filterQueries, function (query) {
                        properties.filterQueries.push(TableStore.encoder._makeQuery(query));
                    })
                }
                if (Object.prototype.toString.call(aQuery.shouldQueries) === '[object Array]') {
                    if (typeof aQuery.minimumShouldMatch !== 'number' || aQuery.minimumShouldMatch % 1 !== 0) {
                        throw new Error("Expect minimumShouldMatch which should be an int32 number");
                    }
                    properties.minimumShouldMatch = aQuery.minimumShouldMatch;
                    properties.shouldQueries = [];
                    TableStore.util.arrayEach(aQuery.shouldQueries, function (query) {
                        properties.shouldQueries.push(TableStore.encoder._makeQuery(query));
                    })
                }

                var boolQuery = tsSearchProtos.BoolQuery.create(properties);

                return boolQuery;
                break;
            case TableStore.QueryType.CONST_SCORE_QUERY://7
                var properties = {
                    filter: TableStore.encoder._makeQuery(aQuery.filter)
                };
                var constScoreQuery = tsSearchProtos.ConstScoreQuery.create(properties);

                return constScoreQuery;
                break;
            case TableStore.QueryType.FUNCTION_SCORE_QUERY://8
                var properties = {
                    query: TableStore.encoder._makeQuery(aQuery.query)
                };

                if (!aQuery.fieldValueFactor || typeof aQuery.fieldValueFactor.fieldName !== 'string') {
                    throw new Error("Except fieldValueFactor.fieldName which should be string");
                }
                var fieldValueFactorProperties = {
                    fieldName: aQuery.fieldValueFactor.fieldName
                };
                var fieldValueFactor = tsSearchProtos.FieldValueFactor.create(fieldValueFactorProperties);

                properties.fieldValueFactor = fieldValueFactor;

                var functionScoreQuery = tsSearchProtos.FunctionScoreQuery.create(properties);

                return functionScoreQuery;
                break;
            case TableStore.QueryType.NESTED_QUERY://9
                var properties = {
                    path: aQuery.path,
                    query: TableStore.encoder._makeQuery(aQuery.query),
                    scoreMode: aQuery.scoreMode ||  TableStore.ScoreMode.SCORE_MODE_AVG,
                    weight: aQuery.weight,
                };
                if (aQuery.innerHits) {
                    properties.innerHits = TableStore.encoder._makeInnerHits(aQuery.innerHits);
                }

                var nestedQeury = tsSearchProtos.NestedQuery.create(properties);

                return nestedQeury;
                break;
            case TableStore.QueryType.WILDCARD_QUERY://10
                var properties = {
                    fieldName: aQuery.fieldName,
                    value: aQuery.value,
                    weight: aQuery.weight,
                };

                var wildcardQuery = tsSearchProtos.WildcardQuery.create(properties);

                return wildcardQuery;
                break;
            case TableStore.QueryType.MATCH_ALL_QUERY://11
                var matchAllQuery = tsSearchProtos.MatchAllQuery.create();

                return matchAllQuery;
                break;
            case TableStore.QueryType.GEO_BOUNDING_BOX_QUERY://12
                var properties = {
                    fieldName: aQuery.fieldName,
                    topLeft: aQuery.topLeft,
                    bottomRight: aQuery.bottomRight
                };
                var geoBoundingBoxQuery = tsSearchProtos.GeoBoundingBoxQuery.create(properties);

                return geoBoundingBoxQuery;
                break;
            case TableStore.QueryType.GEO_DISTANCE_QUERY://13
                var properties = {
                    fieldName: aQuery.fieldName,
                    centerPoint: aQuery.centerPoint,
                    distance: aQuery.distance
                };
                var geoDistanceQuery = tsSearchProtos.GeoDistanceQuery.create(properties);

                return geoDistanceQuery;
                break;
            case TableStore.QueryType.GEO_POLYGON_QUERY://14
                var properties = {
                    fieldName: aQuery.fieldName,
                    points: aQuery.points
                };
                var geoPolygonQuery = tsSearchProtos.GeoPolygonQuery.create(properties);

                return geoPolygonQuery;
                break;
            case TableStore.QueryType.TERMS_QUERY://15
                var properties = {
                    fieldName: aQuery.fieldName,
                    terms: TableStore.encoder._makeTerms(aQuery.terms),
                    weight: aQuery.weight,
                };
                var termsQuery = tsSearchProtos.TermsQuery.create(properties);

                return termsQuery;
                break;
            case TableStore.QueryType.EXISTS_QUERY://16
                var properties = {
                    fieldName: aQuery.fieldName
                };
                var termsQuery = tsSearchProtos.TermsQuery.create(properties);

                return termsQuery;
                break;
            case TableStore.QueryType.KNN_VECTOR_QUERY://17
                var properties = {
                    fieldName: aQuery.fieldName,
                }
                if (aQuery.topK) {
                    properties.topK = aQuery.topK;
                }
                if (aQuery.float32QueryVector) {
                    properties.float32QueryVector = aQuery.float32QueryVector;
                }
                if (aQuery.filter) {
                    properties.filter = TableStore.encoder._makeQuery(aQuery.filter);
                }
                if (aQuery.weight) {
                    properties.weight = aQuery.weight;
                }
                var knnVectorQuery = tsSearchProtos.KnnVectorQuery.create(properties);

                return knnVectorQuery;
                break;
            case TableStore.QueryType.FUNCTIONS_SCORE_QUERY://18
                var properties = {};
                if(aQuery.query){
                    properties.query = TableStore.encoder._makeQuery(aQuery.query);
                }
                if(aQuery.minScore){
                    properties.minScore = aQuery.minScore;
                }
                if(aQuery.maxScore){
                    properties.maxScore = aQuery.maxScore;
                }
                if(aQuery.scoreMode){
                    properties.scoreMode = aQuery.scoreMode;
                }
                if(aQuery.combineMode){
                    properties.combineMode = aQuery.combineMode;
                }
                properties.functions = [];
                if(aQuery.functions){
                    TableStore.util.arrayEach(aQuery.functions, function (scoreFunction) {
                        var scoreFunctionProperties = {
                            weight: scoreFunction.weight || null,
                        }
                        if(scoreFunction.filter) {
                            scoreFunctionProperties.filter = TableStore.encoder._makeQuery(scoreFunction.filter);
                        }
                        if (scoreFunction.fieldValueFactorFunction) {
                            scoreFunctionProperties.fieldValueFactor = tsSearchProtos.FieldValueFactorFunction.create(scoreFunction.fieldValueFactorFunction);
                        }
                        if (scoreFunction.decayFunction) {
                            const paramBody = TableStore.encoder._constructDecayParam(scoreFunction.decayFunction.decayParamType, scoreFunction.decayFunction.decayParam);
                            const writer = Object.getPrototypeOf(paramBody).constructor.encode(paramBody);
                            var decayFunctionProperties = {
                                fieldName: scoreFunction.decayFunction.fieldName,
                                mathFunction: scoreFunction.decayFunction.mathFunction || null,
                                paramType: scoreFunction.decayFunction.decayParamType,
                                param: writer.finish(),
                                decay: scoreFunction.decayFunction.decay || null,
                                multiValueMode: scoreFunction.decayFunction.multiValueMode || null,
                            }
                            scoreFunctionProperties.decay = tsSearchProtos.DecayFunction.create(decayFunctionProperties);
                        }
                        if (scoreFunction.randomFunction) {
                            scoreFunctionProperties.random = tsSearchProtos.RandomScoreFunction.create(scoreFunction.randomFunction);
                        }
                        properties.functions.push(scoreFunctionProperties);
                    })
                }
                var functionsScoreQuery = tsSearchProtos.FunctionsScoreQuery.create(properties);

                return functionsScoreQuery;
                break;
            default:
                throw new Error("queryTypeExpectation not exist queryType: " + queryType);
        }
    },