reactivesearch
reactivesearch copied to clipboard
[Question]: Refresh search using same query parameters
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?
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.
Is it possible to do the same with ReactiveList?
@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
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 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?
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?
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.
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());
@bietkul Why there is no any way to trigger search? It super basic functionality. Why we need to invent some hacks for this? WTF?