Skip to content

Latest commit

 

History

History
242 lines (189 loc) · 5.04 KB

search.md

File metadata and controls

242 lines (189 loc) · 5.04 KB

Search

You can search in your index by passing a query object to the search function like so:

  q.query = {
     AND: [{'*': ['usa']}] //search for string 'usa' in all ('*') fields
  }
  si.search(q, function (err, searchResults) {
    //do something with searchResults
  })

Search types

Search index supports several types of search:

Simple Search

Use the wildcard token ('*') to do simple searches across all fields

q.query = {
  AND: {'*': ['reagan']}
}

Fielded Search

You can search on specified fields

Every <fieldName>: [<searchterms>] in the query must return true in order for the corresponding document to be included in the search results. In the example below, only documents containing the word reagan in the title field AND the word usa in the body field will be returned.

q.query = 
  {
    AND: [
      {'title': ['reagan']},
      {'body':['usa']}
    ]
  }
}

Boolean Search (AND, OR, NOT)

You can construct boolean AND, OR and NOT queries:

q.query = [                    // Each array element is an OR condition
  {
    AND: [             
      {'title': ['reagan']},   // 'reagan' AND 'ussr'   
      {'body':['ussr']}
    ],
    NOT: [
      {'body':['usa']}         // but NOT 'usa' in the body field
    ]
  },
  {                            // OR this condition
    AND: [                  
      {'title': ['gorbachev']},// 'gorbachev' AND 'ussr'
      {'body':['ussr']}
    ],
    NOT: [
      {'body':['usa']}         // NOT 'usa' in the body field
    ]
  }
}

Multi-word Search

To search for more than one term, the search string must be tokenised into an array

A search on <fieldName> for [<searchterms>] returns true if and only if all of [<searchterms>] appear in <fieldName>

q.query = {
  AND: {'*': ['usa', 'reagan']}
}

Phrase Search

Use one array element per phrase. You can search for combinations of phrases if desired.

q.query = {
  AND: {'*': ['ronald reagan']}
}

##Categories, Buckets and Filters

You can add categories and buckets onto any given search. You can use the results to then create filters.

Categories display totals for each category in the resultset

Buckets display totals for ranges defined in the query

Filters are a way of limiting the resultset. Think of them as the query that must be called when you select buckets/categories.

Simple categories

To use categories, you must define filters when you index the data (TODO: describe indexing more here)

The following query will display a count for every single place in the resultset

q.query = {
  AND: {'*': ['reagan']}
}
q.categories = {
  name: 'places'    
}

Limiting facet length

The following query will limit facet length

q.query = {
  AND: {'*': ['reagan']}
}
q.categories = {
  name: 'places',
  limit: 10
}

Sorting facets

The following query will sort a facet by its keys in a descending order. Available sorts are keyAsc,keyDesc, valueAsc, and valueDesc

q.query = {
  AND: {'*': ['reagan']}
}
q.categories = {
  name: 'places',
  limit: 10,
  sort: keyDesc
}

Buckets

The following query will display a count for every range of values within the given buckets

q.query = {'*': ['africa', 'bank']};
q.buckets = [
  {
    field: 'totalamt',
    gte: '000000000000000',
    lte: '000000050000000'
  },
  {
    field: 'totalamt',
    gte: '000000050000001',
    lte: '100000000000000'
  },
  {
    field: 'mjtheme',
    gte: 'A',
    lte: 'J'
  },
  {
    field: 'mjtheme',
    gte: 'K',
    lte: 'Z'
  }      
]

Some other query stuff

Paging

You can handle paging by setting an offset and a pageSize. For example, the following would give you page 3 of a resultset, where the page size is 50:

q.query = {
  AND:{'*': ['usa']}
}
q.offset = 100;
q.pageSize = 50;

Teasers and Search Term Highlighting

Search-index can generate a simple content preview in the search results (teasers). The teaser will contain highlighted search terms. Simply specify the field to generate the teaser on like so:

q.query = {
  AND:{'*': ['usa']}
};
q.teaser = 'body';

Common error when test-querying your search solution

When you have indexed some documents, you want to check if your search is working, and do a couple of test queries. Remember then, to not use any of the stopwords. These are words that hold little meaning and are therefore left out of the index. This is the search-index stopword list for English, and here are all the available stopword languages.

q.query = {
  AND: {'*': ['and']}
}

And since there iss an inherent logical AND when having multiple words in your query, it only takes one word from the stopword-list to get zero results back.

q.query = {
  AND: {'*': ['and', 'meaningful']}
};