Skip to content

Commit

Permalink
feat: take into account the analyzer's score.final when sorting sugge…
Browse files Browse the repository at this point in the history
…stions (#11)

BREAKING CHANGE: analyzer score now affects typeahead results
  • Loading branch information
bcoe committed Nov 9, 2017
1 parent 59b032b commit c8b1297
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ queries.search.suggestions('gulp', esClient)
Available options:

- `size`: The total number of results to return, defaults to `25`
- `analyzerWeight`: How much should we weight the analyzer's `score.final` by? defaults to `1.0`.
- `scoreWeight`: How much should we weight the search `_score`? defaults to `0.3`.

#### .search.similar(q, esClient, [options]) -> Promise

Expand All @@ -92,8 +94,9 @@ queries.search.similar('chaik', esClient)
Available options:

- `size`: The total number of results to return, defaults to `10`.
- `analyzerWeight`: How much should we weight the analyzer values by? defaults to `2.2`.
- `scoreWeight`: How much should we weight the fuzzy score by? defaults to `1.5`.
- `analyzerWeight`: How much should we weight the analyzer's `score.final` by? defaults to `2.2`.
- `scoreWeight`: How much should we weight the search `_score`? defaults to `1.5`.
- `boostExact`: How much should the score of exact matches be boosted? defaults to `100000`.
- `minScore`: defaults to `4.5`.

_the above default values were based on trial and error examining the
Expand Down
12 changes: 9 additions & 3 deletions lib/searchSuggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ const toEsClient = require('./util/toEsClient');

function searchSuggestions(q, esClient, options) {
esClient = toEsClient(esClient);
options = Object.assign({ size: 25 }, options);
options = Object.assign({
size: 25,
analyzerWeight: 1,
scoreWeight: 0.3,
boostExact: 100000
}, options);

const text = parseQuery.discardQualifiers(q);
const script = `(doc["score.final"].value * ${options.analyzerWeight}) * (_score * ${options.scoreWeight}) + doc[\"package.name.raw\"].value.equals(text) ? ${options.boostExact} : 0`;

if (!text) {
return Promise.resolve([]);
Expand Down Expand Up @@ -77,8 +83,8 @@ function searchSuggestions(q, esClient, options) {
},
script_score: {
lang: 'groovy',
script: 'doc["package.name.raw"].value.equals(text) ? 100000 + _score : _score',
params: { text },
script: script,
params: { text }
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/search-suggestions-sass.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
},
"script_score": {
"lang": "groovy",
"script": "doc[\"package.name.raw\"].value.equals(text) ? 100000 + _score : _score",
"params": {
"text": "sass"
"script": "(doc[\"score.final\"].value * 1) * (_score * 0.3) + doc[\"package.name.raw\"].value.equals(text) ? 100000 : 0",
"params": {
"text": "sass"
}
}
}
Expand Down Expand Up @@ -1923,4 +1923,4 @@
"32007"
]
}
]
]

0 comments on commit c8b1297

Please sign in to comment.