API – Query Object (Strings)

Introduction

This article expands upon the topics taught in our API Query Objects article. Thus, we recommend you read that article first if you haven't already. Here, we explain how to query string objects via the API. Query strings are covered in greater detail on Elasticsearch’s site, so we will cover only the basics here.

Much of the data in LoanPro is stored in strings; and, in most cases, searching for an exact string match is unnecessary. Instead, searching for near matches instead of exact matches may be more beneficial. For example, searching a customer name that contains “alex” can return customers named Alex, Alexis, or Alexander. This can be achieved by using Elasticsearch's query string feature.

Format

Query Strings are formatted as follows.

    "query_string": {
"fields": [<fields list here>],
"query": <query string here>
}

Here's a breakdown of the parameters used in this query:

Parameter

Description

Example

fields

Replace <fields list here> with the lowercase list of field names to perform the search against

For example, to search against custom fields and titles, include: ["title", "customFields"]

query

Replace <query string here> with the string for the query

"due"

Wildcards

Wildcards allow you to specify that there should be a set of characters if you don’t know (or don’t care) what they are. There are two wildcard characters that you can use: ?  and *.

  • ? – Represents a single character
  • * – Represents zero or more characters

This means that we can search for phrases in a string, without knowing what the other characters are. For example, to find all three letter names that end with “ob”, you can use ?ob which will match “Bob”, “Cob”, “Rob”, “Sob”, etc.

On the other hand, to find all strings that contain the phrase “due” we can use *due*.

Remember that searching with *due* doesn’t tell the query to find strings with the exact word "due". Instead, it tells the query to find any strings that contain the word "due”. Therefore, it will match “The loan was due yesterday.”, “overdue”, “past due”, “due date”, “amount due”, “dueling”, etc.

Fuzziness

Fuzziness allows you to search for phrases that are similar to each other. This is useful if you are unsure of the spelling in a string or if you'd like to search for mistakes. Elasticsearch uses the Damerau-Levenshtein distance algorithm to determine the similarity between phrases. The algorithm counts the smallest possible number of changes in characters needed to make both two phrases match. Changes such as insertions, deletions, or substitutions are included in the count. 

For example, to change the phrase “quick” to “qukc”, the algorithm would choose to delete the “k” and then substitute the "i" for a "k". This would result in 2 changes needed. As another example, to change the phrase “too” to “two”, we would substitute “w” for the first “o”, resulting in 1 change.

Elasticsearch uses 2 changes as the default for the max number of changes that it will match for. To use a fuzziness string, state your string and then follow it with the tilde (~), like quick~.

Other Search Tools

There are a few more tools that you can use with Elasticsearch. While we won't cover them in detail here, we'll provide links for you to look into the details on the Elasticsearch documentation website.

  • Ranges – allows ranges of dates, numbers, etc
  • Boolean Operators – allowing the use of “AND”, “OR”, “NOT”
  • Grouping – allows grouping of terms

Example

Now, let's go over how the query could look for finding loans with a first name holding “ob”. Our query would look something like the following:

{
"query":
  {
"bool":
{
"must":
[
{
"match":
{
"customers.firstName": "*ob*"
}
}
]
}
}
}


How did we do?


Powered by HelpDocs (opens in a new tab)