Austin R.
Austin R.

Reputation: 25

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. I DO NOT want algolia to look through the userID and school fields for this particular search

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.

Upvotes: 1

Views: 331

Answers (1)

May Rest in Peace
May Rest in Peace

Reputation: 2207

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

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

See this link for restricting your attributes for a query

Upvotes: 1

Related Questions