2

Using the Search REST API to get all users where their last name begins with a certain letter to make a custom user directory. For example, I hoped that the following would return all users that have a last name that starts with A:

/_api/search/query?querytext=%27lastname:A*%27&sourceid=%27B09A7990-05EA-4AF9-81EF-EDFAB16C4E31%27

However, that A* is really looking for words that start with A, not last names that start with A, so the above will return users with hyphenated last names where the second part starts with A. So the above will return "Carter-Adams" as part of the result set, which is not what I want.

How do I search to get values where the entire value starts with a certain string, rather than where any word in the value starts with that string?

2 Answers 2

0

If you are looking to grab the first letter of each last name you could use startsWith in your $filter of the query.

https://msdn.microsoft.com/en-us/library/office/fp142385(v=office.15).aspx#bk_supported

1
  • 1
    The following did not work: &$filter=startswith(lastname,'K'). This is using the REST Search api which seems to have different options than the other SP Rest services, so I'm using the querytext to specify the query.
    – Mike2500
    Commented Apr 16, 2015 at 17:28
0

Try using lastname= instead of lastname:

Typically in the Search API a : means "Contains" and an = means "Equals" which will anchor the query to the beginning of the line. I'm not 100% certain it works in this specific case but give it a try and let me know.

Edit:

For reference, here's the KQL guide.

Depending on the architecture of your application you could always filter the results yourself, just holding them in a list until you prune the ones containing a hyphen.

3
  • lastname=A* yields no results. (perhaps it's like SQL and = means that the * is not a wildcard)
    – Mike2500
    Commented Apr 16, 2015 at 15:46
  • According to the guide I just linked you can combine the two, they just don't recommend it for exact matching (for obvious reasons). The only other thing I can think of would be adding another parameter for something like lastname<>- to exclude hyphens, but that's just a guess.
    – thanby
    Commented Apr 17, 2015 at 11:42
  • Or put the A* in quotes to make sure it's not combining the wildcard with the next operation.
    – thanby
    Commented Apr 17, 2015 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.