appbaseio / reactivesearch

Search UI components for React and Vue
https://opensource.appbase.io/reactivesearch
Apache License 2.0
4.9k stars 466 forks source link

defaultQuery not applied to DataSearch #1594

Closed edwinsiebel closed 3 years ago

edwinsiebel commented 3 years ago

Affected Projects React

Library Version: x.y.z stable, "@appbaseio/reactivesearch": "^3.14"

Describe the bug When using a DataSearch component with autosuggest, and a custom render method, the defaultQuery is not applied. This is stated in the documentation.

To Reproduce Steps to reproduce the behavior:

<DataSearch
    componentId="q"
    defaultQuery={ ( value, props ) => {
        query: {
            terms: {
                country: [ "India" ]
            }
        }
    } }
    dataField={ SEARCH_FIELDS }
    fieldWeights={ FIELD_WEIGHTS }
    autosuggest={ true }
    debounce={ 100 }
    fuzziness={ 1 }
    highlight={ false }
    type="search"
    className="searchbar__container"
    innerClass={ {
        input: 'searchbar__input',
    } }
    URLParams={ true }
    enablePredictiveSuggestions={ true }
    enablePopularSuggestions={ true }
    render={ ( {
        error,
        data,
        value,
        downshiftProps: { isOpen, getItemProps, highlightedIndex },
    } ) => {
        if ( error ) {
            return (
                <div>
                    Something went wrong! Error details{ ' ' }
                    { JSON.stringify( error ) }
                </div>
            );
        }
        if ( isOpen && Boolean( value.length ) ) {
            return (
                <ul className="autosuggest | list-unstyled">
                    { data.slice( 0, 5 ).map( ( item, index ) => {
                        const entity = new FactoryEntity( item.source );
                        return (
                            <li
                                id="selected_option"
                                className="autosuggest__item"
                                key={ entity.id }
                                { ...getItemProps( {
                                    item,
                                } ) }
                            >
                                <a
                                    href={ `${ entity.url }` }
                                    className="autosuggest__link"
                                >
                                    <SuggestionItem
                                        currentValue={ value }
                                        suggestion={ item }
                                    />
                                </a>
                            </li>
                        );
                    } ) }
                </ul>
            );
        }
    } }
/>

See screenshot: no 'India` is applied Expected behavior The query should apply/add/merge the (custom) defaultQuery with the defaultQuery.

Screenshots Screenshot from 2021-01-20 13-52-50

Desktop (please complete the following information):

Additional context

edwinsiebel commented 3 years ago

Any reaction from the team?

bietkul commented 3 years ago

@edwinsiebel You can find the defaultQuery working here https://codesandbox.io/s/dawn-resonance-2b047?file=/src/index.js. FYI, the defaultQuery would affect the suggestions query in DataSearch. If you want to update results then please use customQuery instead.

rgb-panda commented 3 years ago

@edwinsiebel I'm facing the same issue when I have a customQuery already defined, and I want to use defaultQuery as well to adjust the suggestions query. Were you able to figure this out?

edwinsiebel commented 3 years ago

No, unfortunately not. Have tried various implementations, however no results.

@bietkul , seems more have problems with this situation. Can you make a working example, as described by @rgb-panda ?

bietkul commented 3 years ago

@rgb-panda @edwinsiebel Sharing an example with both customQuery and defaultQuery props in DataSearch component. I don't see any issues with that. Make sure you're using the latest version of the libs even then the issue persists feel free to re-open the issue with a reproducible example. https://codesandbox.io/s/dawn-resonance-2b047?file=/src/index.js

djsiroky commented 3 years ago

@edwinsiebel I know this issue is closed, but I noticed in your steps to reproduce code you're not returning the query. I came across this same issue when using the documentation example for defaultQuery, so one of the following changes may get things working for you:

<DataSearch
    componentId="q"
    defaultQuery={ ( value, props ) => ({
        query: {
            terms: {
                country: [ "India" ]
            }
        }
    }) }

or

<DataSearch
    componentId="q"
    defaultQuery={ ( value, props ) => {
        return {
            query: {
                terms: {
                    country: [ "India" ]
                }
            }
        }
    }}

Edit: Opened a PR in the docs repo to fix this

edwinsiebel commented 3 years ago

Hi @djsiroky ,

Thanks for the heads up. Will have to work on it again soon, so will take this change into account. And great that you updated the doc's as well!

rgb-panda commented 1 year ago

Looking at this almost a year later, and still face the same issue when trying to adjust the suggestions query to limit what it's doing. It seems to work only the first time, then any other time after the first search it reverts back to the default query with includes : [*] and size: 100

my query below is completely ignored

defaultQuery={() => ( { size: "10", _source: { includes: ["my_field"] } } ) }

rgb-panda commented 1 year ago

tried to provide the full query as it is in the library default with just size and _source difference, and again it just works the first time. After my first actual search, any subsequent suggestion query ignores it

defaultQuery={(value,props) => ( { "query": { "bool": { "must": [ { "bool": { "must": [ { "bool": { "should": [ { "multi_match": { "query": value, "fields": [ "my_field" ], "type": "cross_fields", "operator": "and" } }, { "multi_match": { "query": value, "fields": [ "my_field" ], "type": "phrase", "operator": "and" } }, { "multi_match": { "query": value, "fields": [ "my_field" ], "type": "phrase_prefix", "operator": "and" } } ], "minimum_should_match": "1" } } ] } } ] } }, "size": 10, "_source":["my_field"] } ) }