reintroduce searching by most recent

This commit is contained in:
keyan 2023-11-13 18:17:45 -06:00
parent 84b5475270
commit 7b58edb77b
2 changed files with 69 additions and 51 deletions

View File

@ -177,55 +177,88 @@ export default {
whatArr.push({ match: { 'sub.name': sub } }) whatArr.push({ match: { 'sub.name': sub } })
} }
const should = [
{
// all terms are matched in fields
multi_match: {
query,
type: 'most_fields',
fields: ['title^1000', 'text'],
minimum_should_match: '100%',
boost: 10000
}
},
{
// all terms are matched in fields fuzzily
multi_match: {
query,
type: 'most_fields',
fields: ['title^1000', 'text'],
minimum_should_match: '60%',
boost: 1000
}
}
]
let boostMode = 'multiply'
let sortField let sortField
let sortMod = 'log1p'
switch (sort) { switch (sort) {
case 'comments': case 'comments':
sortField = 'ncomments' sortField = 'ncomments'
sortMod = 'square'
break break
case 'sats': case 'sats':
sortField = 'sats' sortField = 'sats'
break break
case 'recent':
sortField = 'createdAt'
sortMod = 'square'
boostMode = 'replace'
break
default: default:
sortField = 'wvotes' sortField = 'wvotes'
sortMod = 'none'
break break
} }
const functions = [
{
field_value_factor: {
field: sortField,
modifier: sortMod,
factor: 1.2
}
}
]
// allow fuzzy matching for single terms
if (sort !== 'recent') {
should.push({
// only some terms must match unless we're sorting
multi_match: {
query,
type: 'most_fields',
fields: ['title^1000', 'text'],
fuzziness: 'AUTO',
prefix_length: 3,
minimum_should_match: '60%'
}
})
// small bias toward posts with comments
functions.push({
field_value_factor: {
field: 'ncomments',
modifier: 'ln1p',
factor: 1
}
})
}
if (query.length) { if (query.length) {
whatArr.push({ whatArr.push({
bool: { bool: {
should: [ should
{
// all terms are matched in fields
multi_match: {
query,
type: 'most_fields',
fields: ['title^1000', 'text'],
minimum_should_match: '100%',
boost: 10000
}
},
{
// all terms are matched in fields fuzzily
multi_match: {
query,
type: 'most_fields',
fields: ['title^1000', 'text'],
minimum_should_match: '60%',
boost: 1000
}
},
{
// only some terms must match unless we're sorting
multi_match: {
query,
type: 'most_fields',
fields: ['title^1000', 'text'],
fuzziness: 'AUTO',
prefix_length: 3,
minimum_should_match: '60%'
}
}
]
} }
}) })
} }
@ -282,23 +315,8 @@ export default {
] ]
} }
}, },
functions: [ functions,
{ boost_mode: boostMode
field_value_factor: {
field: sortField,
modifier: sort === 'comments' ? 'square' : 'log2p',
factor: 1.2
}
},
{
field_value_factor: {
field: 'ncomments',
modifier: 'ln1p',
factor: 1
}
}
],
boost_mode: 'multiply'
} }
}, },
highlight: { highlight: {

View File

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