add production search config

This commit is contained in:
keyan 2022-01-28 12:37:23 -06:00
parent d22991bc3b
commit af2c990a51
5 changed files with 59 additions and 82 deletions

View File

@ -277,55 +277,64 @@ export default {
},
search: async (parent, { q: query, cursor }, { models, search }) => {
const decodedCursor = decodeCursor(cursor)
let sitems
const sitems = await search.search({
index: 'item',
size: LIMIT,
from: decodedCursor.offset,
body: {
query: {
bool: {
must: {
bool: {
should: [
{
try {
sitems = await search.search({
index: 'item',
size: LIMIT,
from: decodedCursor.offset,
body: {
query: {
bool: {
must: {
bool: {
should: [
{
// all terms are matched in fields
multi_match: {
query,
type: 'most_fields',
fields: ['title^20', 'text'],
fuzziness: 'AUTO',
prefix_length: 3,
minimum_should_match: '100%',
boost: 2
}
},
{
multi_match: {
query,
type: 'most_fields',
fields: ['title^20', 'text'],
fuzziness: 'AUTO',
prefix_length: 3,
minimum_should_match: '100%',
boost: 2
}
},
{
// only some terms must match
multi_match: {
query,
type: 'most_fields',
fields: ['title^20', 'text'],
fuzziness: 'AUTO',
minimum_should_match: '60%'
multi_match: {
query,
type: 'most_fields',
fields: ['title^20', 'text'],
fuzziness: 'AUTO',
minimum_should_match: '60%'
}
}
}
// TODO: add wildcard matches for
// user.name and url
]
}
},
filter: {
range: {
createdAt: {
lte: decodedCursor.time
]
}
},
filter: {
range: {
createdAt: {
lte: decodedCursor.time
}
}
}
}
}
}
})
} catch (e) {
console.log(e)
return {
cursor: null,
items: []
}
})
}
const items = sitems.body.hits.hits.map(e => e._source)
return {

View File

@ -1,5 +1,14 @@
import es from '@opensearch-project/opensearch'
const os = require('@opensearch-project/opensearch')
global.es ||= new es.Client({ node: 'http://localhost:9200' })
global.os = global.os || new os.Client(
process.env.NODE_ENV === 'production'
? {
node: process.env.OPENSEARCH_URL,
auth: {
username: process.env.OPENSEARCH_USERNAME,
password: process.env.OPENSEARCH_PASSWORD
}
}
: { node: 'http://localhost:9200' })
export default global.es
module.exports = global.os

38
package-lock.json generated
View File

@ -9,7 +9,6 @@
"version": "0.1.0",
"dependencies": {
"@apollo/client": "^3.4.15",
"@elastic/elasticsearch": "^7.16.0",
"@opensearch-project/opensearch": "^1.0.2",
"@prisma/client": "^2.25.0",
"apollo-server-micro": "^2.21.2",
@ -352,25 +351,6 @@
"node": ">=6.9.0"
}
},
"node_modules/@elastic/elasticsearch": {
"version": "7.16.0",
"resolved": "https://registry.npmjs.org/@elastic/elasticsearch/-/elasticsearch-7.16.0.tgz",
"integrity": "sha512-lMY2MFZZFG3om7QNHninxZZOXYx3NdIUwEISZxqaI9dXPoL3DNhU31keqjvx1gN6T74lGXAzrRNP4ag8CJ/VXw==",
"dependencies": {
"debug": "^4.3.1",
"hpagent": "^0.1.1",
"ms": "^2.1.3",
"secure-json-parse": "^2.4.0"
},
"engines": {
"node": ">=12"
}
},
"node_modules/@elastic/elasticsearch/node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
},
"node_modules/@eslint/eslintrc": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-0.4.3.tgz",
@ -12233,24 +12213,6 @@
"to-fast-properties": "^2.0.0"
}
},
"@elastic/elasticsearch": {
"version": "7.16.0",
"resolved": "https://registry.npmjs.org/@elastic/elasticsearch/-/elasticsearch-7.16.0.tgz",
"integrity": "sha512-lMY2MFZZFG3om7QNHninxZZOXYx3NdIUwEISZxqaI9dXPoL3DNhU31keqjvx1gN6T74lGXAzrRNP4ag8CJ/VXw==",
"requires": {
"debug": "^4.3.1",
"hpagent": "^0.1.1",
"ms": "^2.1.3",
"secure-json-parse": "^2.4.0"
},
"dependencies": {
"ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
}
}
},
"@eslint/eslintrc": {
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-0.4.3.tgz",

View File

@ -10,7 +10,6 @@
},
"dependencies": {
"@apollo/client": "^3.4.15",
"@elastic/elasticsearch": "^7.16.0",
"@opensearch-project/opensearch": "^1.0.2",
"@prisma/client": "^2.25.0",
"apollo-server-micro": "^2.21.2",

View File

@ -1,7 +1,5 @@
const { gql } = require('apollo-server-micro')
const es = require('@opensearch-project/opensearch')
const search = new es.Client({ node: 'http://localhost:9200' })
const search = require('../api/search')
const ITEM_SEARCH_FIELDS = gql`
fragment ItemSearchFields on Item {