dramatically improve search

This commit is contained in:
keyan 2023-11-10 09:22:30 -06:00
parent 7cb3a7ad64
commit 3499f92436
2 changed files with 52 additions and 55 deletions

View File

@ -1,4 +1,3 @@
import { ITEM_FILTER_THRESHOLD } from '../../lib/constants'
import { decodeCursor, LIMIT, nextCursorEncoded } from '../../lib/cursor'
import { whenToFrom } from '../../lib/time'
import { getItem } from './item'
@ -121,27 +120,16 @@ export default {
whatArr.push({ match: { 'sub.name': sub } })
}
const sortArr = []
let sortField
switch (sort) {
case 'recent':
sortArr.push({ createdAt: 'desc' })
sortArr.push('_score')
break
case 'comments':
sortArr.push({ ncomments: 'desc' })
sortArr.push('_score')
sortField = 'ncomments'
break
case 'sats':
sortArr.push({ sats: 'desc' })
sortArr.push('_score')
break
case 'match':
sortArr.push('_score')
sortArr.push({ wvotes: 'desc' })
sortField = 'sats'
break
default:
sortArr.push({ wvotes: 'desc' })
sortArr.push('_score')
sortField = 'wvotes'
break
}
@ -154,21 +142,21 @@ export default {
multi_match: {
query,
type: 'most_fields',
fields: ['title^100', 'text'],
fields: ['title^1000', 'text'],
minimum_should_match: '100%',
boost: 400
boost: 1000
}
},
{
// all terms are matched in fields
// all terms are matched in fields fuzzily
multi_match: {
query,
type: 'most_fields',
fields: ['title^100', 'text'],
fields: ['title^1000', 'text'],
fuzziness: 'AUTO',
prefix_length: 3,
minimum_should_match: '100%',
boost: 20
boost: 10
}
},
{
@ -176,10 +164,10 @@ export default {
multi_match: {
query,
type: 'most_fields',
fields: ['title^100', 'text'],
fields: ['title^1000', 'text'],
fuzziness: 'AUTO',
prefix_length: 3,
minimum_should_match: sortArr.length > 1 ? '100%' : '60%'
minimum_should_match: '60%'
}
}
]
@ -203,6 +191,8 @@ export default {
size: LIMIT,
from: decodedCursor.offset,
body: {
query: {
function_score: {
query: {
bool: {
must: [
@ -233,11 +223,18 @@ export default {
createdAt: whenRange
}
},
{ range: { wvotes: { gt: -1 * ITEM_FILTER_THRESHOLD } } }
{ range: { wvotes: { gte: 0 } } }
]
}
},
sort: sortArr,
field_value_factor: {
field: sortField,
modifier: sort === 'comments' ? 'square' : 'log2p',
factor: 1.2
},
boost_mode: 'multiply'
}
},
highlight: {
fields: {
title: { number_of_fragments: 0, pre_tags: ['***'], post_tags: ['***'] },

View File

@ -97,7 +97,7 @@ export default function Search ({ sub }) {
name='sort'
size='sm'
overrideValue={sort}
items={['zaprank', 'match', 'recent', 'comments', 'sats']}
items={['zaprank', 'comments', 'sats']}
/>
for
<Select