Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: Refresh search using same query parameters #1173

Open
AaronPlave opened this issue Sep 25, 2019 · 9 comments
Open

[Question]: Refresh search using same query parameters #1173

AaronPlave opened this issue Sep 25, 2019 · 9 comments

Comments

@AaronPlave
Copy link
Contributor

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 AaronPlave changed the title [Question]: Refresh search [Question]: Refresh search using same query parameters Sep 25, 2019
@bietkul
Copy link
Contributor

bietkul commented Sep 25, 2019

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.

@z4m0
Copy link

z4m0 commented Oct 4, 2019

Is it possible to do the same with ReactiveList?

@z4m0
Copy link

z4m0 commented Oct 5, 2019

@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
Copy link

z4m0 commented Oct 6, 2019

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.

@DustinKLo
Copy link

@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?

@kud
Copy link

kud commented Oct 25, 2021

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
Copy link

kud commented Oct 29, 2021

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.

@ghost
Copy link

ghost commented Apr 13, 2022

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());

@slavaGanzin
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants