简体   繁体   English

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

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

My data is structured like this我的数据结构是这样的

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

****EDIT: This is my front end code if that helps, maybe the problem lies here. ****编辑:如果有帮助,这是我的前端代码,也许问题出在这里。

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

When I index this, I only want to get results with the firstName, lastName, and username fields.当我对此进行索引时,我只想获得带有 firstName、lastName 和 username 字段的结果。 I DO NOT want algolia to look through the userID and school fields for this particular search我不希望 algolia 为这个特定的搜索查看用户 ID 和学校字段

I've tried changing the facets and the searchable attributes but nothing seems to change.我已经尝试更改方面和可搜索属性,但似乎没有任何变化。

My results have been searching through every single field, not just the ones I specified.我的结果一直在搜索每个字段,而不仅仅是我指定的字段。 Maybe I just don't have a great understanding of how algolia queries work.也许我只是不太了解 algolia 查询的工作原理。

While indexing you should set which attributes are searchable.在编制索引时,您应该设置哪些属性是可搜索的。

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

See this link for more details.有关更多详细信息,请参阅此链接

Although setting the searchableAttributes should work, you can also restrict your attributes尽管设置searchableAttributes应该有效,但您也可以限制您的属性

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

See this link for restricting your attributes for a query请参阅此链接以限制查询的属性

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

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