Skip to content

Commit

Permalink
Merge pull request #4 from aura-nw/feat/search-product
Browse files Browse the repository at this point in the history
[DEV] Feat/search product
  • Loading branch information
peara authored Aug 15, 2024
2 parents 5511440 + b9876d4 commit c197de5
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 10 deletions.
33 changes: 33 additions & 0 deletions src/apis/routes/elastic-search/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Hapi from '@hapi/hapi';
import { queryProducts } from '#common/elastic-search';
import Joi from 'joi';

export const searchProductionRoute: Hapi.ServerRoute = {
method: 'GET',
path: '/search',
options: {
description: 'Search product by name, owner',
notes: 'Search product by name, owner',
tags: ['api', 'Product'],
plugins: {
'hapi-swagger': {},
},
validate: {
query: Joi.object({
name: Joi.string().required().example('Product name'),
limit: Joi.number().max(100).default(10).example(10),
}),
},
cache: {
// client side cache for 30s
expiresIn: 30 * 1000,
privacy: 'private',
},
// response: { schema: Joi.object({}) },
},
handler: (request: Hapi.Request) => {
const { name, limit } = request.query;
console.log('request.query', request.query);
return queryProducts({ keyword: name, limit });
},
};
3 changes: 3 additions & 0 deletions src/apis/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ export * from '#apis/routes/products/update';
export * from '#apis/routes/products/create';
export * from '#apis/routes/products/get';

// Search API
export * from '#apis/routes/elastic-search/search';

export * from '#apis/routes/utils/upload';
5 changes: 5 additions & 0 deletions src/common/elastic-search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { config } from '#configs/index';
import { Client } from '@elastic/elasticsearch';
import * as productIndex from 'elastic-search/indexes/products';

let elasticsearch: Client;
if (config.elasticsearchUrl) {
Expand All @@ -9,4 +10,8 @@ if (config.elasticsearchUrl) {
});
}

export const queryProducts = async (params: any) => {
return await productIndex.querySearch(params);
};

export { elasticsearch };
17 changes: 7 additions & 10 deletions src/elastic-search/indexes/products/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,32 +112,29 @@ export const initIndex = async (): Promise<void> => {
}
};

export const query = async (params: {
export const querySearch = async (params: {
keyword?: string;
limit?: number;
}): Promise<any> => {
let esQuery = undefined;

esQuery = {
bool: {
filter: [
{
term: { category: params.keyword },
},
],
query_string: {
query: `*${params.keyword || ''}*`,
fields: ['name', 'owner.name'],
},
};

const esSearchParams = {
index: INDEX_NAME,
query: esQuery,
size: params.limit,
size: params.limit || 10,
};

const esResult = await elasticsearch.search<ProductDocument>(esSearchParams);
console.log('elasticsearch-products', JSON.stringify(esResult));
const results = esResult.hits.hits.map((hit) => hit._source!);

return { esResult };
return results;
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
63 changes: 63 additions & 0 deletions src/elastic-search/scripts/query.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,69 @@ curl -X GET 'http://localhost:9200/products/_search?pretty' -H 'Content-Type: ap
}
}'

curl -X GET 'http://localhost:9200/products/_search?pretty' -H 'Content-Type: application/json' -d '{
"query" : {
"multi_match": {
"query": "pizze",
"fields": [ "*name" ]
}
}
}'

curl -X GET 'http://localhost:9200/products/_search?pretty' -H 'Content-Type: application/json' -d '{
"query" : {
"wildcard": {
"name": {
"value": "Olala"
}
}
}
}'

curl -X GET 'http://localhost:9200/products/_search?pretty' -H 'Content-Type: application/json' -d '{
"query" : {
"regexp": {
"name": {
"value": "la+",
"flags": "ALL",
"case_insensitive": true,
"rewrite": "constant_score"
}
}
}
}'

curl -X GET 'http://localhost:9200/products/_search?pretty' -H 'Content-Type: application/json' -d '{
"query": {
"match_phrase": {
"name": {
"query": "Olala"
}
}
}
}'

# Search proximity with multiple fields
curl -X GET 'http://localhost:9200/products/_search?pretty' -H 'Content-Type: application/json' -d '{
"query": {
"query_string": {
"query": "*Olala*",
"fields": ["name", "owner.name"]
}
}
}'

curl -X GET 'http://localhost:9200/products/_search?pretty' -H 'Content-Type: application/json' -d '{
"query": {
"fuzzy": {
"name": {
"value": "Olala",
"fuzziness": "AUTO:3..5"
}
}
}
}'

curl -X GET 'http://localhost:9200/products/_search?pretty' -H 'Content-Type: application/json' -d '{
"query" : {
"bool": {
Expand Down

0 comments on commit c197de5

Please sign in to comment.