SearchQueryBuilder

Class

SearchQueryBuilder can be used to construct the q param for searchItems or searchGroups. By chaining methods, it helps build complex search queries.

const startDate = new Date("2020-01-01");
const endDate = new Date("2020-09-01");
const query = new SearchQueryBuilder()
 .match("Patrick")
 .in("owner")
 .and()
 .from(startDate)
 .to(endDate)
 .in("created")
 .and()
 .startGroup()
   .match("Web Mapping Application")
   .in("type")
   .or()
   .match("Mobile Application")
   .in("type")
   .or()
   .match("Application")
   .in("type")
 .endGroup()
 .and()
 .match("Demo App");

searchItems(query).then((res) => {
  console.log(res.results);
});

Will search for items matching

"owner: Patrick AND created:[1577836800000 TO 1598918400000] AND (type:"Web Mapping Application" OR type:"Mobile Application" OR type:Application) AND Demo App"

Implements

Constructors

Constructor Parameters

Parameter Type Default Notes
q Optional string ""

An existing query string to start building from.

Methods

Method Returns Notes
SearchQueryBuilder

Joins two sets of queries with an AND clause.

const query = new SearchQueryBuilder()
  .match("Lakes")
  .in("title")
  .and()
  .match("Rivers")
  .in("title")
SearchQueryBuilder

Boosts the previous term to increase its rank in the results.

const query = new SearchQueryBuilder()
  .match("Lakes")
  .in("title")
  .or()
  .match("Rivers")
  .in("title")
  .boost(3)
SearchQueryBuilder

Returns a new instance of SearchQueryBuilder based on the current instance.

SearchQueryBuilder

Ends a search group.

const query = new SearchQueryBuilder()
  .startGroup()
    .match("Lakes")
    .in("title")
  .endGroup()
  .or()
  .startGroup()
    .match("Rivers")
    .in("title")
  .endGroup()
  • from(term: number | string | Date)
SearchQueryBuilder

Begins a new range query.


const NEWYEARS = new Date("2020-01-01")
const TODAY = new Date()

const query = new SearchQueryBuilder()
  .from(NEWYEARS)
  .to(TODAY)
  .in("created")
  • in(field: string)
SearchQueryBuilder

Defines fields to search in. You can pass "*" or call this method without arguments to search a default set of fields

const query = new SearchQueryBuilder()
  .match("My Layer")
  .in("title")
  • match(terms: string[] | string)
SearchQueryBuilder

Defines strings to search for.

const query = new SearchQueryBuilder()
  .match("My Layer")
SearchQueryBuilder

Joins two sets of queries with a NOT clause. Another option for filtering results is the prohibit operator '-'.

// omit results with "Rivers" in their title
const query = new SearchQueryBuilder()
  .not()
  .match("Rivers")
  .in("title")

// equivalent
const query = new SearchQueryBuilder()
  .match("Rivers")
  .in("-title")
SearchQueryBuilder

Joins two sets of queries with an OR clause.

const query = new SearchQueryBuilder()
  .match("Lakes")
  .in("title")
  .or()
  .match("Rivers")
  .in("title")
SearchQueryBuilder

Starts a new search group.

const query = new SearchQueryBuilder()
  .startGroup()
    .match("Lakes")
    .in("title")
  .endGroup()
  .or()
  .startGroup()
    .match("Rivers")
    .in("title")
  .endGroup()
  • to(term: any)
SearchQueryBuilder

Ends a range query.

const query = new SearchQueryBuilder()
  .from(yesterdaysDate)
  .to(todaysDate)
  .in("created")
string

Returns the current query string. Called internally when the request is made.

and

Class Method

Joins two sets of queries with an AND clause.

Returns


const query = new SearchQueryBuilder()
  .match("Lakes")
  .in("title")
  .and()
  .match("Rivers")
  .in("title")

boost

Class Method

Boosts the previous term to increase its rank in the results.

Parameters

Parameter Type Default Notes
num Required number

Returns


const query = new SearchQueryBuilder()
  .match("Lakes")
  .in("title")
  .or()
  .match("Rivers")
  .in("title")
  .boost(3)

clone

Class Method

Returns a new instance of SearchQueryBuilder based on the current instance.

Returns

endGroup

Class Method

Ends a search group.

Returns


const query = new SearchQueryBuilder()
  .startGroup()
    .match("Lakes")
    .in("title")
  .endGroup()
  .or()
  .startGroup()
    .match("Rivers")
    .in("title")
  .endGroup()

from

Class Method

Begins a new range query.

Parameters

Parameter Type Default Notes
term Required number | string | Date

Returns



const NEWYEARS = new Date("2020-01-01")
const TODAY = new Date()

const query = new SearchQueryBuilder()
  .from(NEWYEARS)
  .to(TODAY)
  .in("created")

in

Class Method

Defines fields to search in. You can pass "*" or call this method without arguments to search a default set of fields

Parameters

Parameter Type Default Notes
field Optional string

The field to search for the previous match in.

Returns


const query = new SearchQueryBuilder()
  .match("My Layer")
  .in("title")

match

Class Method

Defines strings to search for.

Parameters

Parameter Type Default Notes
terms Required string[] | string

strings to search for.

Returns


const query = new SearchQueryBuilder()
  .match("My Layer")

not

Class Method

Joins two sets of queries with a NOT clause. Another option for filtering results is the prohibit operator '-'.

Returns


// omit results with "Rivers" in their title
const query = new SearchQueryBuilder()
  .not()
  .match("Rivers")
  .in("title")

// equivalent
const query = new SearchQueryBuilder()
  .match("Rivers")
  .in("-title")

or

Class Method

Joins two sets of queries with an OR clause.

Returns


const query = new SearchQueryBuilder()
  .match("Lakes")
  .in("title")
  .or()
  .match("Rivers")
  .in("title")

startGroup

Class Method

Starts a new search group.

Returns


const query = new SearchQueryBuilder()
  .startGroup()
    .match("Lakes")
    .in("title")
  .endGroup()
  .or()
  .startGroup()
    .match("Rivers")
    .in("title")
  .endGroup()

to

Class Method

Ends a range query.

Parameters

Parameter Type Default Notes
term Required any

Returns


const query = new SearchQueryBuilder()
  .from(yesterdaysDate)
  .to(todaysDate)
  .in("created")

toParam

Class Method

Returns the current query string. Called internally when the request is made.

  • toParam() : string

Returns

string

Class defined in packages/arcgis-rest-portal/src/util/SearchQueryBuilder.ts:46