reactivesearch icon indicating copy to clipboard operation
reactivesearch copied to clipboard

[Question]: Refresh search using same query parameters

Open AaronPlave opened this issue 6 years ago • 9 comments

Affected Projects React

Is your feature request related to a problem? Please describe. I'm looking for a way to force a refresh of the elasticsearch query using the same active set of search parameters since my database may be updated fairly frequently and I don't want to force the user to refresh the page. After searching through the docs and issues I haven't found a clear way to do this. Is this functionality supported?

AaronPlave avatar Sep 25 '19 20:09 AaronPlave

You can try with the customQuery prop in DataSearch, update it whenever you want. You can also define the ref property to access the updateQuery method of DataSearch and call it.

bietkul avatar Sep 25 '19 20:09 bietkul

Is it possible to do the same with ReactiveList?

z4m0 avatar Oct 04 '19 09:10 z4m0

@bietkul I wasn't able to make updateQuery work with the same query I think this line is preventing it:

https://github.com/appbaseio/reactivecore/blob/9490a737adba49be70a9df5dd7ca55f384ac23c1/src/actions/query.js#L363

z4m0 avatar Oct 05 '19 10:10 z4m0

If anyone wonders I solved it by making the query different but ensuring that it returns the same results

defaultQuery={() => ({
  timeout: '1s',
  query: {
    bool: {
      must: filter,
      should: [{
        term: {
          dummy: String(lastRefreshTime)
        }
      }]
    }
  }
})}

Where dummy is an attribute that doesn't exist and lastRefreshTime is the time where the server was updated.

Anyway I think it should be a better way to update the data when the user presses a refresh button or the app knows that the query could return different data.

z4m0 avatar Oct 06 '19 11:10 z4m0

@z4m0 i can't seem to get your defaultQuery to work, where do you get the must: filter and where did you store your lastRefreshTime value?

DustinKLo avatar Apr 11 '20 18:04 DustinKLo

I'm interested by that. I'm doing a checker to verify if we have new fresh data, and display a button to the user in order to click it and refresh the whole reactive search context.

Is there a good way to do that?

kud avatar Oct 25 '21 14:10 kud

const Refresher = ({ defaultQuery }) => {
  const [refreshingDate, setRefreshingDate] = useState(Date.now())

  const handleClick = () => {
    setRefreshingDate(Date.now())
  }

  return (
      <ReactiveComponent
        componentId="refresher"
        customQuery={() =>
          bodybuilder()
            .notFilter("term", "DJPZ6h5jYFlNWv22", refreshingDate) // this field should not exist, it's just a way to trigger reactive search
            .build()
        }
        render={() => (
          <button type="button" onClick={handleClick}>
            Click me
          </button>
        )}
      />
  )
}

and you only need to add refresher to your list of components you follow (via reactproperty) in your component(s) to refresh the components you want to refresh.

It creates a bigger elastic search query but it works. I didn't find yet a better way to do that.

kud avatar Oct 29 '21 16:10 kud

This is how I did the refresh with ReactiveComponent which also contains custom index query (not needed if you only refresh).

<ReactiveComponent
    componentId="documentIndex"
    customQuery={props => ({
        query: {
            bool: {
                must: {
                    match: {
                        _index: "document"
                    }
                },
                must_not: {
                    term: { dummy: String(lastRefreshTime) }
                }
            }
        }
    })}
/>

and use it in other components

react={{
    and: ["documentIndex"],
}}

To refresh, simply call setLastRefreshTime(new Date());

ghost avatar Apr 13 '22 07:04 ghost

@bietkul Why there is no any way to trigger search? It super basic functionality. Why we need to invent some hacks for this? WTF?

slavaGanzin avatar Apr 02 '23 20:04 slavaGanzin