如何限制在 Swift 的 Algolia 索引中可搜索的字段?

How to Limit what fields are searchable in Algolia index in Swift?

我的数据结构如下

firstName: String
lastName: String
username: String
userID: String
school: String

****编辑:如果有帮助,这是我的前端代码,也许问题出在这里。

 let index = client.index(withName: "Users")
       index.setSettings([
            "searchableAttributes": [
                "firstName",
                "lastName",
                "username"
            ]
            ])
        let query = Query(query: searchBar.text)
      //  query.facets = ["firstName", "username", "lastName"]
        index.search(query, completionHandler: { (content, error) -> Void in
         // completion handler//print results
        })

当我索引这个时,我只想获得包含 firstName、lastName 和 username 字段的结果。我不想让 algolia 为这个特定的搜索查看用户 ID 和学校字段

我试过更改构面和可搜索属性,但似乎没有任何改变。

我的结果一直在搜索每个字段,而不仅仅是我指定的字段。也许我只是不太了解 algolia 查询的工作原理。

在编制索引时,您应该设置哪些属性是可搜索的。

index.setSettings([
  "searchableAttributes": [
    "firstName",
    "lastName",
    "username"
  ]
])

查看此link了解更多详情。

虽然设置 searchableAttributes 应该有效,但您也可以限制您的属性

query.restrictSearchableAttributes = [
  "firstName",
  "lastName",
  "username"
]

查看此link以限制您的查询属性