import { put, takeEvery, call } from 'redux-saga/effects';
import * as reducer from 'app/redux/SearchReducer';
import { conductSearch } from 'app/utils/ServerApiClient';

export const searchWatches = [takeEvery('search/SEARCH_DISPATCH', search)];

export function* search(action) {
    const { q, s, scroll_id } = action.payload;
    const append = action.payload.scroll_id ? true : false;
    yield put(reducer.searchPending({ pending: true }));
    const luceneQuery = {
        term: {
            searchable: {
                value: q,
                boost: 1.0,
            },
        },
    };
    try {
        const requestParams = {
            body: {
                searchQuery: {
                    size: 30,
                    query: luceneQuery,
                },
            },
        };
        if (scroll_id) {
            requestParams.body.scrollQuery = {
                scroll: '1m',
                scroll_id,
            };
        }
        const searchResponse = yield call(conductSearch, requestParams);
        const searchJSON = yield call([searchResponse, searchResponse.json]);
        yield put(reducer.searchResult({ ...searchJSON, append }));
    } catch (error) {
        console.log('Search error', error);
        yield put(reducer.searchError({ error }));
    }
    yield put(reducer.searchPending({ pending: false }));
}

@@ -439,24 +439,27 @@
 rue %7D));%0A   
+ //
  const lucen
@@ -468,24 +468,27 @@
 uery = %7B%0A   
+ //
      term: %7B
@@ -487,24 +487,27 @@
  term: %7B%0A   
+ //
          sea
@@ -516,24 +516,27 @@
 hable: %7B%0A   
+ //
             
@@ -545,24 +545,27 @@
 alue: q,%0A   
+ //
             
@@ -577,16 +577,185 @@
 t: 1.0,%0A
+    //         %7D,%0A    //     %7D,%0A    // %7D;%0A    const luceneQuery = %7B%0A        match_phrase: %7B%0A            searchable: %7B%0A                query: q,%0A                slop: 3,%0A
         

add phrase matching