reactjsreactivesearch

Is there a way to have a default search criteria


I am using reactive search to search against my elastic search index. I would like to have one field in the index be a default. For instance, in this app at the root level, you choose a project. When you progress farther into the app you can search. Right now, you are searching against all projects.

I have made a toggle button that I hide with css. This approach is a hack, and works until I clear all filters. That toggle button that no one sees clears the project, then I see results from all projects again.

        <ToggleButton
          style={{ display: 'none' }}
          componentId="project"
          dataField="project"
          defaultSelected={[projectId]}
          URLParams={false}
          multiSelect={false}
          showFilter={false}
          data={[
            { label: 'Project', value: projectId },
          ]}
          className="project"
        />

Is there a way to set a default search criteria?


Solution

  • Yes sure, You can set default search criteria for this,
    Set defaultQuery in ReactiveList component and match the value which has to be default.

    Like this:

    <ReactiveList
        componentId="results"
        dataField={datafield}
        sortBy={sort}
        react={{
            and: Query
        }}
        defaultQuery={this.defaultQuery}
    />
    
    this.defaultQuery = function() {
        return {
            match: { dataField: "Value" }
        }
    };