Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opensearch on chain #48

Merged
merged 8 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./opensearch-handlers";
export * from "./opensearch-handlers";
109 changes: 99 additions & 10 deletions src/handlers/opensearch-handlers/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"analysis": {
"analyzer": {
"autocomplete": {
"tokenizer": "autocomplete",
"tokenizer": "autocomplete_tokenizer",
"filter": [
"lowercase"
]
Expand All @@ -17,13 +17,28 @@
}
},
"tokenizer": {
"autocomplete": {
"autocomplete_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 10,
"max_gram": 20,
"token_chars": [
"letter",
"digit"
"digit",
"whitespace",
"punctuation",
"symbol",
"custom"
],
"custom_token_chars":[
"!",
".",
"+",
"=",
"-",
"&",
"#",
"@",
"*"
]
}
}
Expand All @@ -45,10 +60,24 @@
"type": "keyword"
},
"downloadBaseUrls": {
"type": "text"
"type": "nested",
"properties": {
"url": { "type": "keyword" },
"platform": { "type": "keyword" },
"minVersion": { "type": "keyword" },
"maxVersion": { "type": "keyword" },
"packageId": { "type": "keyword" },
"version": { "type": "keyword" },
"versionCode": { "type": "keyword" },
"screenDPI": { "type": "keyword" }
}
},
"contracts": {
"type": "text"
"type": "nested",
"properties": {
"address": { "type": "keyword" },
"chainId": { "type": "keyword" }
}
},
"images": {
"properties": {
Expand Down Expand Up @@ -141,7 +170,9 @@
"type": "text"
},
"category": {
"type": "keyword"
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "autocomplete_search"
},
"packageId": {
"type": "keyword"
Expand All @@ -151,8 +182,66 @@
},
"subCategory": {
"type": "text"
},
"minted": {
"type": "boolean"
},
"referredBy": {
"properties": {
"name": {
"type":"text"
},
"url": {
"type": "text"
}
}
},
"matrics":{
"properties": {
"downloads": { "type": "integer" },
"installs": { "type": "integer" },
"uninstalls": { "type": "integer" },
"ratingsCount": { "type": "integer" },
"visits": { "type": "integer" },
"rating": { "type": "double" }
}
}
}
}

}
},
"_source": {
"autocompleteFields": [
"name",
"dappId",
"appUrl",
"category",
"minAge",
"isForMatureAudience"
],
"searchFields": [
"name",
"description",
"appUrl",
"downloadBaseUrls",
"contracts",
"images",
"repoUrl",
"dappId",
"minAge",
"isForMatureAudience",
"isSelfModerated",
"isListed",
"language",
"version",
"versionCode",
"listDate",
"availableOnPlatform",
"tags",
"category",
"geoRestrictions",
"chains",
"packageId",
"walletApiVersion",
"matrics"
]
}
}
36 changes: 17 additions & 19 deletions src/handlers/opensearch-handlers/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ import { OpenSearchConnectionOptions } from "../../interfaces";
* return client
*/
export class OpensearchClient {
connection: string;
constructor(options: OpenSearchConnectionOptions){
this.connection = options.connection;
}
public client (): Client {
return new Client({
node: this.connection,
ssl: {
rejectUnauthorized: false,
// ca: fs.readFileSync(ca_certs_path),
// You can turn off certificate verification (rejectUnauthorized: false) if you're using self-signed certificates with a hostname mismatch.
// cert: fs.readFileSync(client_cert_path),
// key: fs.readFileSync(client_key_path)
},
})
}

};

connection: string;
constructor(options: OpenSearchConnectionOptions) {
this.connection = options.connection;
}
public client(): Client {
return new Client({
node: this.connection,
ssl: {
rejectUnauthorized: false
// ca: fs.readFileSync(ca_certs_path),
// You can turn off certificate verification (rejectUnauthorized: false) if you're using self-signed certificates with a hostname mismatch.
// cert: fs.readFileSync(client_cert_path),
// key: fs.readFileSync(client_key_path)
}
});
}
}
2 changes: 1 addition & 1 deletion src/handlers/opensearch-handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./request";
export * from "./request";
185 changes: 95 additions & 90 deletions src/handlers/opensearch-handlers/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,106 @@ import { Client } from "@opensearch-project/opensearch";
import { OpenSearchConnectionOptions } from "../../interfaces";

export const methods = {
PUT: 'PUT',
POST: 'POST',
GET: 'GET',
DELETE: 'DELETE'
}
PUT: "PUT",
POST: "POST",
GET: "GET",
DELETE: "DELETE"
};
export class OpensearchRequest {
private readonly opensearchConfig = opensearchConfig
opensearchClient: Client;
constructor(options: OpenSearchConnectionOptions){
this.opensearchClient = new OpensearchClient(options).client()
}

/**
* will create a empty index
* add setting of cluster
* add mapping/schema of docs
* @param index name of the index
* @returns index name
*/
public async createIndex(index: string): Promise<any> {
return this.opensearchClient.indices.create({
index,
body: {
settings: this.opensearchConfig.settings,
mappings: this.opensearchConfig.mappings
}
})
}

/**
* insert a new doc to database
* @param index index name
* @param body doc
* @returns response
*/
public async createDoc(index: string, body: any): Promise<any> {
return this.opensearchClient.index({
index,
body,
id: body.id,
refresh: true,
})
}
private readonly opensearchConfig = opensearchConfig;
opensearchClient: Client;
constructor(options: OpenSearchConnectionOptions) {
this.opensearchClient = new OpensearchClient(options).client();
}

/**
* insert multipe docs
* @param index index name
* @param body array of docs
*/
public async createBulkDoc(index: string, body: any[]): Promise<any> {
return this.opensearchClient.helpers.bulk({
datasource: body,
onDocument(doc:any) {
return { create: { _index: index, _id: doc.id } }
}
})
}
/**
* will create a empty index
* add setting of cluster
* add mapping/schema of docs
* @param index name of the index
* @returns index name
*/
public async createIndex(index: string): Promise<any> {
return this.opensearchClient.indices.create({
index,
body: {
settings: this.opensearchConfig.settings,
mappings: this.opensearchConfig.mappings
}
});
}

/**
* do search based on providing query
* @param index index name
* @param body query
* @returns list of docs
*/
public async search(index: string, body: any): Promise<any> {
return this.opensearchClient.search({
index,
body
})
}
/**
* insert a new doc to database
* @param index index name
* @param body doc
* @returns response
*/
public async createDoc(index: string, body: any): Promise<any> {
return this.opensearchClient.index({
index,
body,
id: body.id,
refresh: true
});
}

/**
* delete a doc
* @param index index name
* @param id id of doc
* @returns response
*/
public async deleteDoc(index: string, id: string): Promise<any> {
return this.opensearchClient.delete({
index,
id,
})
}
/**
* insert multipe docs
* @param index index name
* @param body array of docs
*/
public async createBulkDoc(index: string, body: any[]): Promise<any> {
return this.opensearchClient.helpers.bulk({
datasource: body,
onDocument(doc: any) {
return { create: { _index: index, _id: doc.id } };
}
});
}

/**
* do search based on providing query
* @param index index name
* @param body query
* @returns list of docs
*/
public async search(index: string, body: any): Promise<any> {
return this.opensearchClient.search({
index,
body
});
}

public async removeAliasName(index: string, alias: string) {
const allIndexs = await this.opensearchClient.indices.getAlias({ name: alias });
if(!allIndexs || !allIndexs.body) return;
Object.keys(allIndexs.body).filter((aI) => aI != index).forEach(async (aI) => {
await this.opensearchClient.indices.deleteAlias({ index: aI, name: alias })
})
return;
}
public async attachAliasName(index: string, alias: string) {
return this.opensearchClient.indices.putAlias({ index, name: alias });
}
/**
* delete a doc
* @param index index name
* @param id id of doc
* @returns response
*/
public async deleteDoc(index: string, id: string): Promise<any> {
return this.opensearchClient.delete({
index,
id
});
}

public async removeAliasName(index: string, alias: string) {
const allIndexs = await this.opensearchClient.indices.getAlias({
name: alias
});
if (!allIndexs || !allIndexs.body) return;
Object.keys(allIndexs.body)
.filter(aI => aI != index)
.forEach(async aI => {
await this.opensearchClient.indices.deleteAlias({
index: aI,
name: alias
});
});
return;
}
public async attachAliasName(index: string, alias: string) {
return this.opensearchClient.indices.putAlias({ index, name: alias });
}
}
Loading