简体   繁体   English

React Algolia 搜索不返回任何匹配项

[英]React Algolia search returns no hits

I am trying to implement algolia in react hooks.我正在尝试在反应钩子中实现 algolia。 I have created my index and assigned searchable attributes.我已经创建了索引并分配了可搜索的属性。

My searchable attributes are index.customerFirstName and index.customerLastName.我的可搜索属性是 index.customerFirstName 和 index.customerLastName。 I don't have a custom search ranking set.我没有自定义搜索排名集。

An index record looks like this:索引记录如下所示:

    {
  "indexData": {
    "customerFirstName": "Ivor",
    "customerLastName": "Cutler"
  },
  "objectID": "xxxxxxxxxxxxxxxxxxxxxx"
}

With useState I initialise variables:使用 useState 我初始化变量:

const [ searchClient, setSearchClient ] = useState(null);
const [ search, setSearch ] = useState('');
const [ searchIndex, setSearchIndex ] = useState();

My useEffect waits to sign into firebase to get the records, then sets the search client to access the algolia index.我的 useEffect 等待登录 firebase 以获取记录,然后设置搜索客户端以访问 algolia 索引。 If it is successful, it should populate the variable searchClient and this seems to work because when useEffect detects the change it logs the searchClient.如果成功,它应该填充变量 searchClient,这似乎有效,因为当 useEffect 检测到更改时,它会记录 searchClient。

useEffect (() => {

        firebase.auth().onAuthStateChanged((user) => {

            if (user && !searchClient) { 

                setSearchClient(

                    algoliasearch(

                        process.env.REACT_APP_ALGOLIA_APP_ID,

                        process.env.REACT_APP_ALGOLIA_SEARCH_KEY

                    )

                )

            }

          });

          if (searchClient !== null && !searchIndex) {

              setSearchIndex(

                  searchClient.initIndex('Sessions')

              );

              console.log("SEARCH CLIENT", searchClient)

          }

          if (searchIndex && search) {

              console.log('SEARCH INDEX: ', searchIndex, search);

              getHits();

          }

    

    }, [search, searchClient, searchIndex, sessions, sessionData, showSessionData])

The function gethits() is called when the value of search (the query) changes:当搜索(查询)的值改变时调用函数 gethits():

const getHits = () => {

        searchIndex.search(

            search

        ).then(console.log)

    }

Yet my hits array is always empty.然而我的 hits 数组总是空的。 I have data in the index with the correct fields?我在索引中有正确字段的数据? What gives?是什么赋予了?

It is searching something because the search query appears in the hits JSON?它正在搜索某些内容,因为搜索查询出现在点击 JSON 中?

Ok so once I regenerated the API SEARCH KEY, this worked fine!好的,一旦我重新生成了 API 搜索密钥,就可以正常工作! For anybody who has already managed to index Algolia correctly and is using React Hooks, this method might offer more immediate flexibility than their InstantSearch React component, and I couldn't see anyone that had posted a basic React Hooks way of searching an algolia index.对于已经设法正确索引 Algolia 并使用 React Hooks 的任何人来说,这种方法可能比他们的 InstantSearch React 组件提供更直接的灵活性,而且我看不到任何人发布了搜索 algolia 索引的基本 React Hooks 方法。

There was no need to wait for firebase so this will work in the useEffect:无需等待 firebase,因此这将在 useEffect 中起作用:

   if (!searchClient) { 

                setSearchClient(

                    algoliasearch(

                        process.env.REACT_APP_ALGOLIA_APP_ID,

                        process.env.REACT_APP_ALGOLIA_SEARCH_KEY

                    )

                )

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM