| 163 | } |
| 164 | |
| 165 | function getMatchQueries(query, { usePrefixSearch, fuzzy }) { |
| 166 | const BOOST_PHRASE = 10.0 |
| 167 | const BOOST_TITLE = 4.0 |
| 168 | const BOOST_HEADINGS = 3.0 |
| 169 | const BOOST_CONTENT = 1.0 |
| 170 | const BOOST_AND = 2.5 |
| 171 | const BOOST_EXPLICIT = 3.5 |
| 172 | // Number doesn't matter so much but just make sure it's |
| 173 | // boosted low. Because we only really want this to come into |
| 174 | // play if nothing else matches. E.g. a search for `Acions` |
| 175 | // which wouldn't find anythig else anyway. |
| 176 | const BOOST_FUZZY = 0.1 |
| 177 | |
| 178 | const matchQueries = [] |
| 179 | |
| 180 | // If the query input is multiple words, it's good to know because you can |
| 181 | // make the query do `match_phrase` and you can make `match` query |
| 182 | // with the `AND` operator (`OR` is the default). |
| 183 | const isMultiWordQuery = query.includes(' ') || query.includes('-') |
| 184 | |
| 185 | if (isMultiWordQuery) { |
| 186 | // If the query contains spaces, prioritize a "match phrase" query |
| 187 | // beyond a regular "match" query. |
| 188 | // Basically, that means if you search for 'foo bar' we'd rather |
| 189 | // rank: |
| 190 | // "A common term is foo bar which is often used" |
| 191 | // above: |
| 192 | // "Some people use foo" |
| 193 | // "Bar is also a common term" |
| 194 | // |
| 195 | // So that, when all are matched you get this rank: |
| 196 | // 1. "A common term is foo bar which is often used" |
| 197 | // 2. "Some people use foo" |
| 198 | // 3. "Bar is also a common term" |
| 199 | // |
| 200 | // But note, a "match phrase" isn't the holy panacea of matches. |
| 201 | // In particular, just because there exists a document whose *content* |
| 202 | // contains the phrase "... foo bar ..." we might still prefer the |
| 203 | // matches on title that contains the words *separately*. This |
| 204 | // is why a 'match_phrase' on 'content' has a lesser boost |
| 205 | // that a 'match' on 'title'. |
| 206 | const matchPhraseStrategy = usePrefixSearch ? 'match_phrase_prefix' : 'match_phrase' |
| 207 | matchQueries.push( |
| 208 | ...[ |
| 209 | { |
| 210 | [matchPhraseStrategy]: { |
| 211 | title_explicit: { boost: BOOST_EXPLICIT * BOOST_PHRASE * BOOST_TITLE, query }, |
| 212 | }, |
| 213 | }, |
| 214 | { [matchPhraseStrategy]: { title: { boost: BOOST_PHRASE * BOOST_TITLE, query } } }, |
| 215 | { |
| 216 | [matchPhraseStrategy]: { |
| 217 | headings_explicit: { boost: BOOST_EXPLICIT * BOOST_PHRASE * BOOST_HEADINGS, query }, |
| 218 | }, |
| 219 | }, |
| 220 | { [matchPhraseStrategy]: { headings: { boost: BOOST_PHRASE * BOOST_HEADINGS, query } } }, |
| 221 | ] |
| 222 | ) |