API – Query Object (Strings)

This article assumes that you have read API Query Objects.

A lot of data in LoanPro is stored in strings, and often times you don’t care about an exact string match. Rather, you want to know that a string contains another string, such as a customer name contains “bob” but doesn’t have to be “bob” (it can be “bobby” as well). This functionality is done by the query string feature of ElasticSearch.

Format

Query Strings are formatted as follows:

    "query_string": {
      "fields": [<fields list here>],
      "query": <query string here>
    }
  • <fields list here> – replace this with the lowercase list of field names to perform the search against.
    • Ex. To search against custom fields and titles it would be ["title", "customFields"]
  • <query string here> – replace this with the string for the query

Query String

Query strings are more heavily documented on ElasticSearch’s site, but we will do a basic overview of them here.

Wildcards

Wildcards allow you to specify that you know that there should/can be a set of characters, but you either don’t know or don’t care what they are. There are two wildcard characters, ? and *.

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

This means that we can do searches for phrases in a string, without knowing what the other characters are. For example, to find all three letter names that end with “ob”, we can do:

?ob

which will match “Bob”, “Cob”, “Rob”, “Sob”, etc.

To find all strings that contain the phrase “due” we can do:

*due*

Please note that the above doesn’t say “Find strings with the word due”, it just says “find strings that contain due”. So, it will match “The loan was due yesterday.”, “overdue”, “past due”, “due date”, “amount due”, “dueling”, etc.

Fuzziness

At times we want to match items that are close to another word (based on the number of changes). The ElasticSearch documentation states that they use the Damerau-Levenshtein distance algorithm. Basically, a change counts as an insertion, deletion, substitution, or swap of two adjacent characters needed to transform strings into another. For example, to transform “quick” to “quikc” we would swap the “c” and “k”, making 1 change. To transform “quick” to “qukc” we would need to delete the “i” and swap the “c” and “k”, making two changes. To transform “too” to “two” we would substitute “w” for the first “o”, making one change.

The algorithm that ElasticSearch uses finds the least amount of changes possible. ElasticSearch also uses 2 changes as the default (meaning that “quick” to “qukc” would match, but not “quick” to “qkc”).

To use a fuzziness string, you would state your string and then follow it with the tilde (~) as follows:

quick~

Please refer to the ElasticSearch documentation for examples on how to use it further.

Other Areas of Interest

Other of areas of interest in the documentation are:

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

To find out more about how to use the Query String, please read the full documentation.

Example

Let’s now 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*"
               }
            }
         ]
      }
   }
}

 

Related Articles

Leave A Comment?