elasticsearch,reactivesearch,React Native,elasticsearch,Reactivesearch" /> elasticsearch,reactivesearch,React Native,elasticsearch,Reactivesearch" />

React native 反应搜索如何在加载时停止初始查询

React native 反应搜索如何在加载时停止初始查询,react-native,elasticsearch,reactivesearch,React Native,elasticsearch,Reactivesearch,所以我已经让ReactiveSearch正常工作了,但我注意到在初始加载时它会执行一个查询来获取项目-考虑到我的索引中可能有一百万个项目,我理想情况下希望关闭它,只从autosuggest返回结果 <ReactiveBase app="tths-shop-items" url="my-es-cluster" credentials="user:password" > <ScrollVi

所以我已经让ReactiveSearch正常工作了,但我注意到在初始加载时它会执行一个查询来获取项目-考虑到我的索引中可能有一百万个项目,我理想情况下希望关闭它,只从autosuggest返回结果

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

编辑2:

我还尝试了以下格式的defaultQuery,并将其添加到reactiveList和dataSearch组件中。这会给我一个未定义的错误,它不是对象“this.defaultQuery.sort”-如果我将sort添加到两个查询中,则没有区别:

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

这里有一个答案,您将通过搜索框单击的值存储在state中,然后在那里处理defaultQuery。注意:如果没有搜索文本,默认查询不匹配_none:{}

这有点低效,因为您仍然执行一个不返回任何结果的查询,但它是有效的-我将保留这个问题,以便在接下来的时间给出更好的答案

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


{ 
如果(值==''){
this.setState({searchText:null})
}
}}
onValueSelected={(值、原因、来源)=>{
this.setState({searchText:value.value})
}
}
/>
{
if(this.state.searchText!==null){
返回{
查询:{
匹配:{
名称:this.state.searchText
}
}
}
}否则{
返回{
查询:{
匹配_none:{}
}
}
}
}}
onData={(res)=>{
返回(
{res.name}
)}
}
/>

这里有一个答案,您将通过搜索框单击的值存储在state中,然后在那里处理defaultQuery。注意:如果没有搜索文本,默认查询不匹配_none:{}

这有点低效,因为您仍然执行一个不返回任何结果的查询,但它是有效的-我将保留这个问题,以便在接下来的时间给出更好的答案

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


{ 
如果(值==''){
this.setState({searchText:null})
}
}}
onValueSelected={(值、原因、来源)=>{
this.setState({searchText:value.value})
}
}
/>
{
if(this.state.searchText!==null){
返回{
查询:{
匹配:{
名称:this.state.searchText
}
}
}
}否则{
返回{
查询:{
匹配_none:{}
}
}
}
}}
onData={(res)=>{
返回(
{res.name}
)}
}
/>

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

我的解决方案看起来有点不同——使用onQueryChange道具

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

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

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

我的解决方案看起来有点不同——使用onQueryChange道具

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

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

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

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

  }
}}

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

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

  }
}}

哦,太好了,我要试一试!欢迎来到StackOverflow-如果这样做有效,我会将此标记为正确答案,因为这是一个更好的解决方案谢谢:)你试过了吗?太好了,我试过了!欢迎使用StackOverflow-如果这样做有效,我会将此标记为正确答案,因为这是一个更好的解决方案谢谢:)您尝试过了吗?