improve related search querying

This commit is contained in:
keyan 2023-11-11 17:56:20 -06:00
parent faa3b87aa3
commit ca87bf56b3

View File

@ -14,25 +14,32 @@ const STOP_WORDS = ['a', 'an', 'and', 'are', 'as', 'at', 'be', 'but',
'levels', 'from', 'cryptocurrencies', 'confirmed', 'news', 'network', 'levels', 'from', 'cryptocurrencies', 'confirmed', 'news', 'network',
'about', 'sources', 'vote', 'considerations', 'hope', 'about', 'sources', 'vote', 'considerations', 'hope',
'keep', 'keeps', 'including', 'we', 'brings', "don't", 'do', 'keep', 'keeps', 'including', 'we', 'brings', "don't", 'do',
'interesting', 'us', 'welcome'] 'interesting', 'us', 'welcome', 'thoughts', 'results']
export default { export default {
Query: { Query: {
related: async (parent, { title, id, cursor, limit, minMatch }, { me, models, search }) => { related: async (parent, { title, id, cursor, limit, minMatch }, { me, models, search }) => {
const decodedCursor = decodeCursor(cursor) const decodedCursor = decodeCursor(cursor)
if (!title || title.trim().split(/\s+/).length < 1) {
if (id) { if (!id && (!title || title.trim().split(/\s+/).length < 1)) {
const item = await getItem(parent, { id }, { me, models }) return {
title = item?.title items: [],
} cursor: null
if (!title) {
return {
items: [],
cursor: null
}
} }
} }
const like = []
if (id) {
like.push({
_index: 'item',
_id: id
})
}
if (title) {
like.push(title)
}
const mustNot = [] const mustNot = []
if (id) { if (id) {
mustNot.push({ term: { id } }) mustNot.push({ term: { id } })
@ -44,28 +51,78 @@ export default {
from: decodedCursor.offset, from: decodedCursor.offset,
body: { body: {
query: { query: {
bool: { function_score: {
must: [ query: {
{ bool: {
more_like_this: { should: [
fields: ['title'], {
like: title, more_like_this: {
min_term_freq: 1, fields: ['title'],
min_doc_freq: 1, like,
min_word_length: 2, min_term_freq: 1,
max_query_terms: 25, min_doc_freq: 1,
minimum_should_match: minMatch || '20%', min_word_length: 2,
stop_words: STOP_WORDS max_query_terms: 12,
minimum_should_match: minMatch || '80%',
stop_words: STOP_WORDS,
boost: 10000
}
},
{
more_like_this: {
fields: ['title'],
like,
min_term_freq: 1,
min_doc_freq: 1,
min_word_length: 2,
max_query_terms: 12,
minimum_should_match: minMatch || '60%',
stop_words: STOP_WORDS,
boost: 1000
}
},
{
more_like_this: {
fields: ['title'],
like,
min_term_freq: 1,
min_doc_freq: 1,
min_word_length: 2,
max_query_terms: 12,
minimum_should_match: minMatch || '30%',
stop_words: STOP_WORDS,
boost: 100
}
},
{
more_like_this: {
fields: ['text'],
like,
min_term_freq: 1,
min_doc_freq: 1,
min_word_length: 2,
max_query_terms: 25,
minimum_should_match: minMatch || '30%',
stop_words: STOP_WORDS,
boost: 10
}
}
],
must_not: [{ exists: { field: 'parentId' } }, ...mustNot],
filter: {
range: { wvotes: { gte: minMatch ? 0 : 0.2 } }
} }
} }
], },
must_not: [{ exists: { field: 'parentId' } }, ...mustNot], field_value_factor: {
filter: { field: 'wvotes',
range: { wvotes: { gte: minMatch ? 0 : 0.2 } } modifier: 'log1p',
} factor: 1.2,
missing: 0
},
boost_mode: 'multiply'
} }
}, }
sort: ['_score', { wvotes: 'desc' }, { sats: 'desc' }]
} }
}) })