Skip to content
This repository was archived by the owner on Aug 18, 2021. It is now read-only.

Commit e206709

Browse files
Froilan IrizarryFroilan Irizarry
authored andcommitted
Changed elasticsearch body construction
- The query body has been simplified to use just the multi_match query with any filters that are passed in the query paramaters - the over use of bool, should, and must queries was removed. This was throwing the _score result off by making elasticsearch aggregate scores in a erroneos way. Multi_match query now has the type (best_match) explicit. More testing with over types could help improve the search results. The name field is given the highest weight followed by the fulltext analyzed version of itself. This change is focused on the repos index. It might be good to review the search being done with the terms index
1 parent adec314 commit e206709

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

services/searcher/index.js

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -100,32 +100,22 @@ class Searcher {
100100
body.query("bool", "should", query);
101101
}
102102

103-
_addFullTextQuery(body, queryParams) {
104-
if (queryParams.q) {
105-
// need to nest `_fulltext` query as a "must"
106-
let ftBody = new Bodybuilder();
107-
108-
ftBody.query("bool", "should", {
109-
"multi_match": {
110-
"query": queryParams.q,
111-
"fields": [
112-
"name^10",
113-
"name._fulltext^10",
114-
"description^4",
115-
"agency.acronym^5",
116-
"agency.name^5",
117-
"agency.name._fulltext^5",
118-
"permissions.usageType^3",
119-
"tags",
120-
"tags._fulltext",
121-
"languages",
122-
"languages._fulltext"
123-
]
124-
}
125-
});
126-
127-
body.query("bool", "must", ftBody.build("v2")["query"]);
128-
}
103+
_addFullTextQuery(body, searchQuery) {
104+
const searchFields = [
105+
"name^10",
106+
"name._fulltext^5",
107+
"description^2",
108+
"agency.acronym",
109+
"agency.name^5",
110+
"agency.name._fulltext",
111+
"permissions.usageType",
112+
"tags^3",
113+
"tags._fulltext",
114+
"languages^3",
115+
"languages._fulltext"
116+
];
117+
118+
body.query("multi_match", 'fields', searchFields, {"query": searchQuery}, {"type": "best_fields"});
129119
}
130120

131121
_addStringFilter(body, field, filter) {
@@ -236,6 +226,7 @@ class Searcher {
236226
* @param {any} queryParams The query parameters a user is searching for
237227
*/
238228
_addSortOrder(body, queryParams) {
229+
body.sort('_score', queryParams['sort'] || 'desc');
239230
body.sort('score', 'desc');
240231
if(queryParams['sort']) {
241232
const sortValues = [];
@@ -263,19 +254,19 @@ class Searcher {
263254
body.sort(sortField, 'asc');
264255
}
265256
});
266-
} else {
267-
body.sort('_score', queryParams['sort'] || 'desc');
268257
}
269258
}
270259

271260
_searchReposQuery(queryParams) {
272261
let body = new Bodybuilder();
273262

263+
if(queryParams.q) {
264+
this._addFullTextQuery(body, queryParams.q);
265+
}
274266
this._addFieldFilters(body, queryParams);
275267
this._addSizeFromParams(body, queryParams);
276-
this._addIncludeExclude(body, queryParams);
277-
this._addFullTextQuery(body, queryParams);
278268

269+
this._addIncludeExclude(body, queryParams);
279270
this._addSortOrder(body, queryParams);
280271

281272
let query = body.build("v2");

0 commit comments

Comments
 (0)