Skip to content

Commit ebb8b45

Browse files
committed
fix(search): Use fts search index for typeahead results
Summary: This improves performance of the search typeahead Test Plan: Manual Reviewers: bengotow Reviewed By: bengotow Differential Revision: https://phab.nylas.com/D3363
1 parent 07f357f commit ebb8b45

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

internal_packages/thread-search/keymaps/search-bar.json

-3
This file was deleted.

internal_packages/thread-search/lib/search-bar.jsx

+10-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default class SearchBar extends React.Component {
2020

2121
componentDidMount() {
2222
this._mounted = true;
23-
this._unsubscribes = [
23+
this._unsubscribers = [
2424
SearchStore.listen(this._onChange),
2525
WorkspaceStore.listen(() => {
2626
if (this.state.focused) {
@@ -30,20 +30,22 @@ export default class SearchBar extends React.Component {
3030
];
3131
}
3232

33-
// It's important that every React class explicitly stops listening to
34-
// N1 events before it unmounts. Thank you event-kit
35-
// This can be fixed via a Reflux mixin
3633
componentWillUnmount() {
3734
this._mounted = false;
38-
for (const usub of this._unsubscribes) {
39-
usub();
40-
}
35+
this._unsubscribers.forEach((usub) => usub())
4136
}
4237

4338
_onFocusSearch = () => {
4439
ReactDOM.findDOMNode(this.refs.searchInput).focus();
4540
}
4641

42+
_onInputKeyDown = (event) => {
43+
const {key, target: {value}} = event;
44+
if (value.length > 0 && key === 'Escape') {
45+
this._onClearAndBlur();
46+
}
47+
}
48+
4749
_onValueChange = (event) => {
4850
SearchActions.queryChanged(event.target.value);
4951
if (event.target.value === '') {
@@ -136,6 +138,7 @@ export default class SearchBar extends React.Component {
136138
className={inputClass}
137139
placeholder="Search all email"
138140
value={query}
141+
onKeyDown={this._onInputKeyDown}
139142
onChange={this._onValueChange}
140143
onFocus={this._onFocus}
141144
onBlur={this._onBlur}
@@ -168,7 +171,6 @@ export default class SearchBar extends React.Component {
168171
className="search-bar"
169172
globalHandlers={{
170173
'core:focus-search': this._onFocusSearch,
171-
'search-bar:escape-search': this._onClearAndBlur,
172174
}}
173175
>
174176
<div>

internal_packages/thread-search/lib/search-query-subscription.es6

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ class SearchQuerySubscription extends MutableQuerySubscription {
5252
if (this._accountIds.length === 1) {
5353
dbQuery = dbQuery.where({accountId: this._accountIds[0]})
5454
}
55-
dbQuery = dbQuery.search(this._searchQuery).limit(30)
55+
dbQuery = dbQuery
56+
.search(this._searchQuery)
57+
.order(Thread.attributes.lastMessageReceivedTimestamp.descending())
58+
.limit(30)
59+
5660
dbQuery.then((results) => {
5761
if (results.length > 0) {
5862
this.replaceQuery(dbQuery)

internal_packages/thread-search/lib/search-store.es6

+8-8
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,17 @@ class SearchStore extends NylasStore {
117117
if (this._fetchingThreadResultsVersion) { return; }
118118
this._fetchingThreadResultsVersion = this._searchSuggestionsVersion;
119119

120-
const databaseQuery = DatabaseStore.findAll(Thread)
121-
.where(Thread.attributes.subject.like(this._searchQuery))
122-
.order(Thread.attributes.lastMessageReceivedTimestamp.descending())
123-
.limit(4);
124-
125120
const {accountIds} = FocusedPerspectiveStore.current();
126-
if (accountIds instanceof Array) {
127-
databaseQuery.where(Thread.attributes.accountId.in(accountIds));
121+
let dbQuery = DatabaseStore.findAll(Thread)
122+
if (Array.isArray(accountIds) && accountIds.length === 1) {
123+
dbQuery = dbQuery.where({accountId: accountIds[0]})
128124
}
125+
dbQuery = dbQuery
126+
.search(this._searchQuery)
127+
.order(Thread.attributes.lastMessageReceivedTimestamp.descending())
128+
.limit(4);
129129

130-
databaseQuery.then(results => {
130+
dbQuery.then(results => {
131131
// We've fetched the latest thread results - display them!
132132
if (this._searchSuggestionsVersion === this._fetchingThreadResultsVersion) {
133133
this._fetchingThreadResultsVersion = null;

0 commit comments

Comments
 (0)