Advanced Search via API

Advanced search is an important function that can be done via API. To review, there are three types of searches supported (you can see them also in the API specification. Example – Accounts: http://zurmo.org/wiki/rest-api-specification-accounts). We will explain more about the three search methods that our API supports:

1. Basic search – This allows you to search a model (such as Accounts) by its basic field. For example, with Accounts we can search by Owner, Name, Type and a few other variables, but this is very basic since we can only search by fields that belong to Accounts that are an exact match. The search must match all fields (using AND operator)

The search parameters can look like this:

$searchFilter = Array
(
  [pagination] => Array
  (
    [page] => 1
    [pageSize] => 3
  )
  [search] => Array
  (
    [industry] => Array
    (
      [value] => Financial Services
    )
    [type] => Array
    (
      [value] => Vendor
    )
    [owner] => Array
    (
      [id] => 1
    )
  )
  [sort] => name.desc
)

2. Dynamic search – This allows you to structure searching clauses. For example, you can search for Accounts where the name is “IBM” OR “Lenovo”. Also you can search by related models, such as finding Accounts whose owner user ID is 5.

Here is an example:

$data = Array
(
  [dynamicSearch] => Array
  (
    [dynamicClauses] => Array
    (
      [0] => Array
      (
        [attributeIndexOrDerivedType] => owner
        [structurePosition] => 1
        [owner] => Array
        (
          [id] => 3
        )
      )
      [1] => Array
      (
        [attributeIndexOrDerivedType] => name
        [structurePosition] => 2
        [name] => Fi
      )
      [2] => Array
      (
        [attributeIndexOrDerivedType] => name
        [structurePosition] => 3
        [name] => Se
      )
    )
    [dynamicStructure] => 1 AND (2 OR 3)
  )
  [pagination] => Array
  (
    [page] => 1
    [pageSize] => 2
  )
  [sort] => name.asc
)

3. Advanced Search – This is the one you have been waiting for. It allows you more advanced searching of fields that belong to Accounts, which means you can search Accounts whose name starts with some text or where some number value is greater then a specified number. Also Advanced Search supports the ability to specify operators and group them in the same way that dynamic search supports:

Here is an example:

$filter = Array
(
  [search] => Array
  (
    [modelClassName] => Account
    [searchAttributeData] => Array
    (
      [clauses] => Array
      (
        [1] => Array
        (
          [attributeName] => owner
          [relatedAttributeName] => id
          [operatorType] => equals
          [value] => 1
        )
        [2] => Array
        (
          [attributeName] => name
          [operatorType] => startsWith
          [value] => Fi
        )
        [3] => Array
        (
          [attributeName] => name
          [operatorType] => startsWith
          [value] => Se
        )
      )
      [structure] => 1 AND (2 OR 3)
    )
  )
  [pagination] => Array
  (
    [page] => 1
    [pageSize] => 2
  )
  [sort] => name asc
)

Here is a list of allowed operators for Advanced Search:

OperatorRules::TYPE_EQUALS
OperatorRules::TYPE_DOES_NOT_EQUAL
OperatorRules::TYPE_STARTS_WITH
OperatorRules::TYPE_DOES_NOT_STARTS_WITH
OperatorRules::TYPE_ENDS_WITH
OperatorRules::TYPE_DOES_NOT_ENDS_WITH
OperatorRules::TYPE_CONTAINS
OperatorRules::TYPE_DOES_NOT_CONTAINS
OperatorRules::TYPE_GREATER_THAN_OR_EQUAL_TO
OperatorRules::TYPE_LESS_THAN_OR_EQUAL_TO
OperatorRules::TYPE_GREATER_THAN
OperatorRules::TYPE_LESS_THAN
OperatorRules::TYPE_ONE_OF
OperatorRules::TYPE_BETWEEN
OperatorRules::TYPE_IS_NULL
OperatorRules::TYPE_IS_NOT_NULL
OperatorRules::TYPE_BECOMES
OperatorRules::TYPE_WAS
OperatorRules::TYPE_BECOMES_ONE_OF
OperatorRules::TYPE_WAS_ONE_OF
OperatorRules::TYPE_CHANGES
OperatorRules::TYPE_DOES_NOT_CHANGE
OperatorRules::TYPE_IS_EMPTY
OperatorRules::TYPE_IS_NOT_EMPTY

Leave a Comment