ReactiveSearch 如何在加载时停止初始查询

ReactiveSearch how to stop initial query on load

所以我的 ReactiveSearch 工作正常,但我注意到在初始加载时它会执行查询以获取项目 - 考虑到我的索引中可能有一百万个项目,我最好关闭它并且只有 return 个来自自动建议的结果?

       <ReactiveBase
        app="tths-shop-items"
        url="my-es-cluster"
        credentials="user:password"
        >
        <ScrollView>
          <View style={styles2.container}>
            <DataSearch
              componentId="searchbox"
              dataField={[
                'name'
              ]}
              placeholder="Search"
            />
            <ReactiveList
              componentId="results"
              dataField="name"
              size={7}
              showResultStats={true}
              pagination={true}
              react={{
                and: "searchbox"
              }}
              onData={(res) => {
              return (
                <View style={styles2.result}>
                  <Image source={{ uri: res.image.replace('http', 'https') }} style={styles2.image} />
                  <View style={styles2.item}>
                    <Text style={styles2.title}>{res.name}</Text>
                  </View>
                </View>
              )}
              }
            />
          </View>
        </ScrollView>
      </ReactiveBase>

编辑 我还尝试添加默认值以尝试停止初始查询 returning 数据。但似乎并没有达到预期的效果。

defaultValue="3245423 kjhkjhkj 2kj34h12jkh 213k4jh12"

编辑 2:

我也试过以下格式的 defaultQuery 并将其添加到 reactiveList 和 dataSearch 组件中没有区别:

              defaultQuery={() => 
                {
                  query: {
                    match_none: {}
                  }
                }
              }

所以这是一个答案,您将通过搜索框单击的值存储在 state 中,然后 fiddle 从那里使用 defaultQuery。注意默认查询 match_none: {} 如果没有搜索文本。

它有点低效,因为您仍然在查询 returns 什么都没有,但它有效 - 我将保留这个问题,以便有时间给出更好的答案。

        <ScrollView>
          <View style={styles.mainContainer}>
            <DataSearch
              componentId="searchbox"
              dataField={[
                'name'
              ]}
              placeholder="Search"
              queryFormat="and"
              noInitialQuery={true}
              onValueChange={(value) => { 
                if(value === ''){
                  this.setState({searchText: null})
                }

              }}
              onValueSelected={(value, cause, source) => {
                  this.setState({searchText: value.value})
                }
              }
              />
            <ReactiveList
              componentId="results"
              dataField="name"
              size={7}
              showResultStats={true}
              pagination={true}
              react={{
                and: "searchbox"
              }}
              defaultQuery={()=> {
                if(this.state.searchText !== null){
                  return {
                    query: {
                      match: {
                        name: this.state.searchText
                      }
                    }
                  }
                } else {
                  return {
                    query: {
                      match_none: {}
                    }
                  }

                }
              }}
              onData={(res) => {
              return (
                <View style={styles2.result}>
                  <Image source={{ uri: res.image.replace('http', 'https') }} style={styles2.image} />
                  <View style={styles2.item}>
                    <Text style={styles2.title}>{res.name}</Text>
                  </View>
                </View>
              )}
              }
            />
          </View>
        </ScrollView>

我知道这是一个老问题,但我偶然发现了同样的问题。

我的解决方案看起来有点不同 - 使用 onQueryChange 属性。

onQueryChange={
  function(prevQuery, nextQuery) {
    if ('match_all' in nextQuery['query']) {
      nextQuery['query'] = { match_none: {} }
    }
  }
}

这将禁用显示所有结果的结果列表,并且仅在您选择任何过滤器或输入搜索词后显示结果。

所选答案非常有用,但我更改了 defaultQuery 以在出现搜索词时使用原始查询:

defaultQuery={()=> {
  if(this.state.searchText !== null){
    return {
    }
  } else {
    return {
      query: {
        match_none: {}
      }
    }

  }
}}