Skip to content

Commit

Permalink
feat: add data indexing API
Browse files Browse the repository at this point in the history
  • Loading branch information
csg01123119 committed Aug 13, 2024
1 parent 8dcdfe0 commit 4539878
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 553 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
jest: true
},
rules: {
indent: ['error', 2],
indent: 'off',
'no-underscore-dangle': [0],
'no-plusplus': [0],
'no-return-await': [0],
Expand Down
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1745,24 +1745,24 @@ Success will return:

### .doMetaQuery(bucketName, queryParam[, options])

Query files (Objects) that meet the specified conditions and list file information according to the specified fields and sorting method.
Queries objects that meet specified conditions and lists the information about objects based on specified fields and sorting methods.

parameters:

- bucketName {String} the bucket name
- queryParam {Object} query parameters
- nextToken {String} The token that is used for the next query when the total number of objects exceeds the value of MaxResults. The object information is returned in alphabetical order starting from the value of NextToken. When this operation is called for the first time, set this field to null.
- maxResults {Number} The maximum number of objects to return. Valid values: 0 to 100. If this parameter is not set or is set to 0, 100 objects are returned.
- query {Object} query criteria
- field {String} Field name. Please refer to Appendix: <a href="https://www.alibabacloud.com/help/en/oss/developer-reference/appendix-supported-fields-and-operators" target="_blank">Support List for Fields and Operators</a>.
- value {String} The value of the field.
- [nextToken] {String} The token that is used for the next query when the total number of objects exceeds the value of MaxResults. The object information is returned in alphabetical order starting from the value of NextToken. When this operation is called for the first time, this field does not need to be set.
- [maxResults] {Number} The maximum number of objects to return. Valid values: 0 to 100.
- query {Object} The query condition.
- [field] {String} the fields. For more information about supported fields and supported operators, see <a href="https://www.alibabacloud.com/help/en/oss/developer-reference/appendix-supported-fields-and-operators#concept-2084036" target="_blank">Appendix: Supported fields and operators</a>.
- [value] {String} The value of the field.
- operation {String} The operators. Valid values: eq (equal to), gt (greater than), gte (greater than or equal to), lt (less than), lte (less than or equal to), match (fuzzy query), prefix (prefix query), and (AND), or (OR), and not (NOT).
- subQueries {Array} The subquery conditions. Options that are included in this element are the same as those of simple query. You must set subquery conditions only when Operation is set to AND, OR, or NOT.
- sort {String} The field based on which the results are sorted. For more information about the fields that can be sorted. Please refer to Appendix: <a href="https://www.alibabacloud.com/help/en/oss/developer-reference/appendix-supported-fields-and-operators" target="_blank">Support List for Fields and Operators</a>.
- order {String} `asc` | `desc` The order in which you want to sort the queried data. Default value: desc.
- aggregations {Object} The container for the information about aggregate operations.
- field {String} The name of the field. For more information about supported fields and supported operators. Please refer to Appendix: <a href="https://www.alibabacloud.com/help/en/oss/developer-reference/appendix-supported-fields-and-operators" target="_blank">Support List for Fields and Operators</a>.
- operation {String} The operator for aggregate operations. Valid values:min,max,average,sum,count,distinct,group.
- [subQueries] {Array} The subquery conditions. Options that are included in this element are the same as those of simple query. You must set subquery conditions only when Operation is set to AND, OR, or NOT.
- [sort] {String} The field based on which the results are sorted. For more information about the fields that can be sorted, see <a href="https://www.alibabacloud.com/help/en/oss/developer-reference/appendix-supported-fields-and-operators#concept-2084036" target="_blank">Appendix: Supported fields and operators</a>.
- [order] {String} `asc` | `desc` The order in which you want to sort the queried data. Default value: desc. Valid values: asc and desc.
- [aggregations] {Object} The information about aggregate operations.
- field {String} The name of the field. For more information about supported fields and supported operators, see <a href="https://www.alibabacloud.com/help/en/oss/developer-reference/appendix-supported-fields-and-operators#concept-2084036" target="_blank">Appendix: Supported fields and operators</a>.
- operation {String} The operator for aggregate operations. Valid values: min, max, average, sum, count, distinct and group.
- [options] {Object} optional parameters
- [timeout] {Number} the operation timeout (ms)

Expand All @@ -1775,8 +1775,8 @@ Success will return:
- size {Number} response size
- rt {Number} request total use time (ms)
- nextToken {String} the token that is used for the next query when the total number of objects exceeds the value of MaxResults
- files {Array} the container for the information about objects
- aggregations {Array} the container for the information about aggregate operations
- files {Array} the information about objects
- aggregations {Array} the information about aggregate operations

---

Expand Down
21 changes: 15 additions & 6 deletions lib/common/bucket/dataIndex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,29 @@ export declare function getMetaQueryStatus(
createTime: any;
updateTime: any;
}>;
interface ISubQuerie {
interface IQuery {
field?: string;
value?: string;
operation: string;
subQueries?: ISubQuerie[];
subQueries?: IQuery[];
}
declare enum EOperation {
min = 'min',
max = 'max',
average = 'average',
sum = 'sum',
count = 'count',
distinct = 'distinct',
group = 'group'
}
interface IAggregation {
field: string;
operation: string;
operation: EOperation;
}
interface IMetaQuery {
nextToken?: string;
maxResults?: number;
query: ISubQuerie;
query: IQuery;
sort?: string;
order?: 'asc' | 'desc';
aggregations?: IAggregation[];
Expand All @@ -45,8 +54,8 @@ export declare function doMetaQuery(
res: any;
status: any;
nextToken: any;
files: any;
aggregations: any;
files: any[];
aggregations: any[];
}>;
export declare function closeMetaQuery(
this: any,
Expand Down
184 changes: 94 additions & 90 deletions lib/common/bucket/dataIndex.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.closeMetaQuery = exports.doMetaQuery = exports.getMetaQueryStatus = exports.openMetaQuery = void 0;
/* eslint-disable max-len */
// https://help.aliyun.com/zh/oss/developer-reference/data-indexing
// https://www.alibabacloud.com/help/en/oss/developer-reference/dometaquery
const checkBucketName_1 = require('../utils/checkBucketName');
const obj2xml_1 = require('../utils/obj2xml');
const formatObjKey_1 = require('../utils/formatObjKey');
async function openMetaQuery(bucketName, options = {}) {
checkBucketName_1.checkBucketName(bucketName);
const params = this._bucketRequestParams('POST', bucketName, { metaQuery: '', comp: 'add' }, options);
params.successStatuses = [200];
const result = await this.request(params);
if (result.status === 200) {
return {
res: result.res,
status: result.status
};
}
throw await this.requestError(result);
return {
res: result.res,
status: result.status
};
}
exports.openMetaQuery = openMetaQuery;
async function getMetaQueryStatus(bucketName, options = {}) {
checkBucketName_1.checkBucketName(bucketName);
const params = this._bucketRequestParams('GET', bucketName, 'metaQuery', options);
params.successStatuses = [200];
const result = await this.request(params);
if (result.status === 200) {
const data = await this.parseXML(result.data);
return {
res: result.res,
status: result.status,
phase: data.Phase,
state: data.State,
createTime: data.CreateTime,
updateTime: data.UpdateTime
};
}
throw await this.requestError(result);
const data = await this.parseXML(result.data);
return {
res: result.res,
status: result.status,
phase: data.Phase,
state: data.State,
createTime: data.CreateTime,
updateTime: data.UpdateTime
};
}
exports.getMetaQueryStatus = getMetaQueryStatus;
var EOperation;
(function (EOperation) {
EOperation['min'] = 'min';
EOperation['max'] = 'max';
EOperation['average'] = 'average';
EOperation['sum'] = 'sum';
EOperation['count'] = 'count';
EOperation['distinct'] = 'distinct';
EOperation['group'] = 'group';
})(EOperation || (EOperation = {}));
async function doMetaQuery(bucketName, queryParam, options = {}) {
checkBucketName_1.checkBucketName(bucketName);
const params = this._bucketRequestParams('POST', bucketName, { metaQuery: '', comp: 'query' }, options);
params.successStatuses = [200];
const { aggregations } = queryParam;
let Aggregations;
let aggregationsParam;
if (aggregations && aggregations.length > 0) {
Aggregations = {
aggregationsParam = {
Aggregation: aggregations.map(item => ({ Field: item.field, Operation: item.operation }))
};
}
Expand All @@ -54,89 +61,86 @@ async function doMetaQuery(bucketName, queryParam, options = {}) {
Query: JSON.stringify(formatObjKey_1.formatObjKey(queryParam.query, 'firstUpperCase')),
Sort: queryParam.sort,
Order: queryParam.order,
Aggregations
Aggregations: aggregationsParam
}
};
params.mime = 'xml';
params.content = obj2xml_1.obj2xml(paramXMLObj, { headers: true, firstUpperCase: true });
const result = await this.request(params);
if (result.status === 200) {
const { NextToken, Files, Aggregations: aggRes } = await this.parseXML(result.data);
let files;
if (Files && Files.File) {
const getFileObject = item => {
var _a, _b;
return {
fileName: item.Filename,
size: item.Size,
fileModifiedTime: item.FileModifiedTime,
ossObjectType: item.OSSObjectType,
ossStorageClass: item.OSSStorageClass,
objectACL: item.ObjectACL,
eTag: item.ETag,
ossTaggingCount: item.OSSTaggingCount,
ossTagging:
(_a = item.OSSTagging) === null || _a === void 0
? void 0
: _a.map(tagging => ({
key: tagging.Key,
value: tagging.Value
})),
ossUserMeta:
(_b = item.OSSUserMeta) === null || _b === void 0
? void 0
: _b.map(meta => ({
key: meta.Key,
value: meta.Value
})),
ossCRC64: item.OSSCRC64,
serverSideEncryption: item.ServerSideEncryption,
serverSideEncryptionCustomerAlgorithm: item.ServerSideEncryptionCustomerAlgorithm
};
const { NextToken, Files, Aggregations: aggRes } = await this.parseXML(result.data);
let files = [];
if (Files && Files.File) {
const getFileObject = item => {
const ossTagging = item.OSSTagging
? item.OSSTagging.map(tagging => ({
key: tagging.Key,
value: tagging.Value
}))
: [];
const ossUserMeta = item.OSSUserMeta
? item.OSSUserMeta.map(meta => ({
key: meta.Key,
value: meta.Value
}))
: [];
return {
fileName: item.Filename,
size: item.Size,
fileModifiedTime: item.FileModifiedTime,
ossObjectType: item.OSSObjectType,
ossStorageClass: item.OSSStorageClass,
objectACL: item.ObjectACL,
eTag: item.ETag,
ossTaggingCount: item.OSSTaggingCount,
ossTagging,
ossUserMeta,
ossCRC64: item.OSSCRC64,
serverSideEncryption: item.ServerSideEncryption,
serverSideEncryptionCustomerAlgorithm: item.ServerSideEncryptionCustomerAlgorithm
};
if (Files.File instanceof Array) {
files = Files.File.map(getFileObject);
} else {
files = [getFileObject(Files.File)];
}
};
if (Files.File instanceof Array) {
files = Files.File.map(getFileObject);
} else {
files = [getFileObject(Files.File)];
}
let aggList;
if (aggRes) {
const getAggregationObject = item => {
var _a;
return {
field: item.Field,
operation: item.Operation,
value: item.Value,
groups:
(_a = item.Groups) === null || _a === void 0
? void 0
: _a.map(group => ({
value: group.Value,
count: group.Count
}))
};
}
let aggList = [];
if (aggRes) {
const getAggregationObject = item => {
var _a;
return {
field: item.Field,
operation: item.Operation,
value: item.Value,
groups:
(_a = item.Groups) === null || _a === void 0
? void 0
: _a.map(group => ({
value: group.Value,
count: group.Count
}))
};
if (aggRes.Aggregation instanceof Array) {
aggList = aggRes.Aggregation.map(getAggregationObject);
} else {
aggList = [getAggregationObject(aggRes.Aggregation)];
}
}
return {
res: result.res,
status: result.status,
nextToken: NextToken,
files,
aggregations: aggList
};
if (aggRes.Aggregation instanceof Array) {
aggList = aggRes.Aggregation.map(getAggregationObject);
} else {
aggList = [getAggregationObject(aggRes.Aggregation)];
}
}
throw await this.requestError(result);
return {
res: result.res,
status: result.status,
nextToken: NextToken,
files,
aggregations: aggList
};
}
exports.doMetaQuery = doMetaQuery;
async function closeMetaQuery(bucketName, options = {}) {
checkBucketName_1.checkBucketName(bucketName);
const params = this._bucketRequestParams('POST', bucketName, { metaQuery: '', comp: 'delete' }, options);
params.successStatuses = [200];
const result = await this.request(params);
return {
res: result.res,
Expand Down
Loading

0 comments on commit 4539878

Please sign in to comment.