Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search: Add other options #5

Closed
ElPicador opened this issue Nov 30, 2015 · 0 comments
Closed

Search: Add other options #5

ElPicador opened this issue Nov 30, 2015 · 0 comments
Milestone

Comments

@ElPicador
Copy link
Contributor

Search

Notes: If you are building a web application, you may be more interested in using our JavaScript client to perform queries. It brings two benefits:

  • Your users get a better response time by not going through your servers
  • It will offload unnecessary tasks from your servers.

To perform a search, you only need to initialize the index and perform a call to the search function.

The search query allows only to retrieve 1000 hits, if you need to retrieve more than 1000 hits for seo, you can use Backup / Retrieve all index content

You can use the following optional arguments:

Query Parameters

Full Text Search Parameters

  • setQueryString: (string) The instant search query string. All words of the query are interpreted as prefixes (for example "John Mc" will match "John Mccamey" and "Johnathan Mccamey"). If no query parameter is set all objects are retrieved.
  • setQueryType: Selects how the query words are interpreted. It can be one of the following values:
    • PREFIX_ALL: All query words are interpreted as prefixes.
    • PREFIX_ALL: Only the last word is interpreted as a prefix (default behavior).
    • PREFIX_NONE: No query word is interpreted as a prefix. This option is not recommended.
  • removeWordsIfNoResults: This option is used to select a strategy in order to avoid having an empty result page. There are three different options:
    • REMOVE_LAST_WORDS: When a query does not return any results, the last word will be added as optional. The process is repeated with n-1 word, n-2 word, ... until there are results.
    • REMOVE_FIRST_WORDS: When a query does not return any results, the first word will be added as optional. The process is repeated with second word, third word, ... until there are results.
    • REMOVE_ALL_OPTIONAL: When a query does not return any results, a second trial will be made with all words as optional. This is equivalent to transforming the AND operand between query terms to an OR operand.
    • REMOVE_NONE: No specific processing is done when a query does not return any results (default behavior).
  • setMinWordSizeToAllowOneTypo: The minimum number of characters in a query word to accept one typo in this word.
    Defaults to 4.
  • setMinWordSizeToAllowTwoTypos: The minimum number of characters in a query word to accept two typos in this word.
    Defaults to 8.
  • setTypoTolerance: This option allows you to control the number of typos in the result set:
    • TYPO_TRUE: The typo tolerance is enabled and all matching hits are retrieved (default behavior).
    • TYPO_FALSE: The typo tolerance is disabled. For example, if one result matches without typos, then all results with typos will be hidden.
    • TYPO_MIN: Only keep results with the minimum number of typos.
    • TYPO_STRICT: Hits matching with 2 typos are not retrieved if there are some matching without typos. This option is useful if you want to avoid false positives as much as possible.
  • enableTyposOnNumericTokens: If set to false, disables typo tolerance on numeric tokens (numbers). Defaults to true.
  • ignorePlural: If set to true, plural won't be considered as a typo. For example, car and cars will be considered as equals. Defaults to false.
  • disableTypoToleranceOnAttributes List of attributes on which you want to disable typo tolerance (must be a subset of the attributesToIndex index setting). Attributes are separated with a comma such as "name,address". You can also use JSON string array encoding such as encodeURIComponent("[\"name\",\"address\"]"). By default, this list is empty.
  • restrictSearchableAttributes List of attributes you want to use for textual search (must be a subset of the attributesToIndex index setting). Attributes are separated with a comma such as "name,address". You can also use JSON string array encoding such as encodeURIComponent("[\"name\",\"address\"]"). By default, all attributes specified in attributesToIndex settings are used to search.
  • enableRemoveStopWords: Remove stop words from query before executing it. Defaults to false. Contains stop words for 41 languages (Arabic, Armenian, Basque, Bengali, Brazilian, Bulgarian, Catalan, Chinese, Czech, Danish, Dutch, English, Finnish, French, Galician, German, Greek, Hindi, Hungarian, Indonesian, Irish, Italian, Japanese, Korean, Kurdish, Latvian, Lithuanian, Marathi, Norwegian, Persian, Polish, Portugese, Romanian, Russian, Slovak, Spanish, Swedish, Thai, Turkish, Ukranian, Urdu).
  • enableAdvancedSyntax: Enables the advanced query syntax. Defaults to 0 (false).
    • Phrase query: A phrase query defines a particular sequence of terms. A phrase query is built by Algolia's query parser for words surrounded by ". For example, "search engine" will retrieve records having search next to engine only. Typo tolerance is disabled on phrase queries.
    • Prohibit operator: The prohibit operator excludes records that contain the term after the - symbol. For example, search -engine will retrieve records containing search but not engine.
  • enableAnalytics: If set to false, this query will not be taken into account in the analytics feature. Defaults to true.
  • enableSynonyms: If set to false, this query will not use synonyms defined in the configuration. Defaults to true.
  • enableReplaceSynonymsInHighlight: If set to false, words matched via synonym expansion will not be replaced by the matched synonym in the highlight results. Defaults to true.
  • setOptionalWords: A string that contains the comma separated list of words that should be considered as optional when found in the query.

Pagination Parameters

  • setPage: (integer) Pagination parameter used to select the page to retrieve.
    Page is zero based and defaults to 0. Thus, to retrieve the 10th page you need to set page=9.
  • setHitsPerPage: (integer) Pagination parameter used to select the number of hits per page. Defaults to 20.

Geo-search Parameters

  • aroundLatitudeLongitude(float, float): Search for entries around a given latitude/longitude.
    The maximum distance is automatically guessed depending of the density of the area but you also manually specify the maximum distance in meters with the radius parameter.
    At indexing, you should specify the geo location of an object with the _geoloc attribute in the form {"_geoloc":{"lat":48.853409, "lng":2.348800}}.
  • aroundLatitudeLongitude(float, float, int, int): Search for entries around a given latitude/longitude with a given precision for ranking. For example, if you set aroundPrecision=100, the distances will be considered by ranges of 100m, for example all distances 0 and 100m will be considered as identical for the "geo" ranking parameter.
  • aroundLatitudeLongitudeViaIP(): Search for entries around the latitude/longitude automatically computed from user IP address.
    The radius is automatically guessed based on density but you can also specify it manually with the radius parameter (optional).
    At indexing, you should specify the geo location of an object with the _geoloc attribute in the form {"_geoloc":{"lat":48.853409, "lng":2.348800}}.
  • aroundLatitudeLongitudeViaIP(int, int): Search for entries around a latitude/longitude automatically computed from user IP address with a given precision for ranking. For example if you set precision=100, two objects that are a distance of less than 100 meters will be considered as identical for the "geo" ranking parameter.
  • insideBoundingBox: Search entries inside a given area defined by the two extreme points of a rectangle (defined by 4 floats: p1Lat,p1Lng,p2Lat,p2Lng).
    For example, insideBoundingBox=47.3165,4.9665,47.3424,5.0201).
    At indexing, you should specify geoloc of an object with the _geoloc attribute (in the form "_geoloc":{"lat":48.853409, "lng":2.348800} or "_geoloc":[{"lat":48.853409, "lng":2.348800},{"lat":48.547456, "lng":2.972075}] if you have several geo-locations in your record). You can use several bounding boxes (OR) by passing more than 4 values. For example instead of having 4 values you can pass 8 to use or OR between two bounding boxes.
  • insidePolygon: Search entries inside a given area defined by a set of points (defined by a minimum of 6 floats: p1Lat,p1Lng,p2Lat,p2Lng,p3Lat,p3Long).
    For example, insideBoundingBox=47.3165,4.9665,47.3424,5.0201).
    At indexing, you should specify geoloc of an object with the _geoloc attribute (in the form "_geoloc":{"lat":48.853409, "lng":2.348800} or "_geoloc":[{"lat":48.853409, "lng":2.348800},{"lat":48.547456, "lng":2.972075}] if you have several geo-locations in your record).

Parameters to Control Results Content

  • setAttributesToRetrieve: The list of object attributes you want to retrieve in order to minimize the answer size. By default, all attributes are retrieved. You can also use * to retrieve all values when an attributesToRetrieve setting is specified for your index.
  • setAttributesToHighlight: The list of attributes you want to highlight according to the query. If an attribute has no match for the query, the raw value is returned. By default, all indexed text attributes are highlighted. You can use * if you want to highlight all textual attributes. Numerical attributes are not highlighted. A matchLevel is returned for each highlighted attribute and can contain:
    • full: If all the query terms were found in the attribute.
    • partial: If only some of the query terms were found.
    • none: If none of the query terms were found.
  • setAttributesToSnippet: The list of attributes to snippet alongside the number of words to return (syntax is attributeName:nbWords). By default, no snippet is computed.
  • getRankingInfo: If set to true, the result hits will contain ranking information in the _rankingInfo attribute.
  • setHighlightingTags: (string, string) Specify the string that is inserted before the highlighted parts in the query result (defaults to "<em>") and the string that is inserted after the highlighted parts in the query result (defaults to "</em>")..
  • setSnippetEllipsisText: (string) String used as an ellipsis indicator when a snippet is truncated (defaults to empty).

Numeric Search Parameters

  • setNumericFilters: A string that contains the comma separated list of numeric filters you want to apply. The filter syntax is attributeName followed by operand followed by value. Supported operands are <, <=, =, > and >=.

You can easily perform range queries via the : operator. This is equivalent to combining a >= and <= operand. For example, numericFilters=price:10 to 1000.

You can also mix OR and AND operators. The OR operator is defined with a parenthesis syntax. For example, (code=1 AND (price:[0-100] OR price:[1000-2000])) translates to encodeURIComponent("code=1,(price:0 to 10,price:1000 to 2000)").

You can also use a string array encoding (for example numericFilters: ["price>100","price<1000"]).

Category Search Parameters

  • setTagFilters: Filter the query by a set of tags. You can AND tags by separating them with commas. To OR tags, you must add parentheses. For example, tags=tag1,(tag2,tag3) means tag1 AND (tag2 OR tag3). You can also use a string array encoding. For example, tagFilters: ["tag1",["tag2","tag3"]] means tag1 AND (tag2 OR tag3).
    At indexing, tags should be added in the _tags attribute of objects. For example {"_tags":["tag1","tag2"]}.

Faceting Parameters

  • setFacetFilters: Filter the query with a list of facets. Facets are separated by commas and is encoded as attributeName:value. To OR facets, you must add parentheses. For example: facetFilters=(category:Book,category:Movie),author:John%20Doe. You can also use a string array encoding. For example, [["category:Book","category:Movie"],"author:John%20Doe"].
  • setFacets: List of object attributes that you want to use for faceting.
    Attributes are separated with a comma. For example, "category,author". You can also use JSON string array encoding. For example, ["category","author"]. Only the attributes that have been added in attributesForFaceting index setting can be used in this parameter. You can also use * to perform faceting on all attributes specified in attributesForFaceting. If the number of results is important, the count can be approximate, the attribute exhaustiveFacetsCount in the response is true when the count is exact.
  • setMaxValuesPerFacet: Limit the number of facet values returned for each facet. For example, maxValuesPerFacet=10 will retrieve a maximum of 10 values per facet.

UNIFIED FILTER PARAMETER (SQL LIKE)

  • setFilters: Filter the query with numeric, facet or/and tag filters. The syntax is a SQL like syntax, you can use the OR and AND keywords. The syntax for the underlying numeric, facet and tag filters is the same than in the other filters:
    available=1 AND (category:Book OR NOT category:Ebook) AND public
    date: 1441745506 TO 1441755506 AND inStock > 0 AND author:"John Doe"
    The list of keywords is:
    OR: create a disjunctive filter between two filters.
    AND: create a conjunctive filter between two filters.
    TO: used to specify a range for a numeric filter.
    NOT: used to negate a filter. The syntax with the ‘-‘ isn’t allowed.

    Note: To specify a value with spaces or with a value equal to a keyword, it's possible to add quotes.

    Warning:

    • Like for the other filter for performance reason, it's not possible to have FILTER1 OR (FILTER2 AND FILTER3).
    • It's not possible to mix different category of filter inside a OR like num=3 OR tag1 OR facet:value
    • It's not possible to negate an group, it's only possible to negate a filters: NOT(FILTER1 OR (FILTER2) is not allowed.

Distinct Parameter

  • setDistinct: If set to true, enables the distinct feature, disabled by default, if the attributeForDistinct index setting is set. This feature is similar to the SQL "distinct" keyword. When enabled in a query with the distinct=1 parameter, all hits containing a duplicate value for the attributeForDistinct attribute are removed from results. For example, if the chosen attribute is show_name and several hits have the same value for show_name, then only the best one is kept and the others are removed.
Index index = client.initIndex("contacts");
System.out.println(index.search(new Query("query string")));
System.out.println(index.search(new Query("query string").
             setAttributesToRetrieve(Arrays.asList("firstname", "lastname")).
             setNbHitsPerPage(50)));

The server response will look like:

{
  "hits": [
    {
      "firstname": "Jimmie",
      "lastname": "Barninger",
      "objectID": "433",
      "_highlightResult": {
        "firstname": {
          "value": "<em>Jimmie</em>",
          "matchLevel": "partial"
        },
        "lastname": {
          "value": "Barninger",
          "matchLevel": "none"
        },
        "company": {
          "value": "California <em>Paint</em> & Wlpaper Str",
          "matchLevel": "partial"
        }
      }
    }
  ],
  "page": 0,
  "nbHits": 1,
  "nbPages": 1,
  "hitsPerPage": 20,
  "processingTimeMS": 1,
  "query": "jimmie paint",
  "params": "query=jimmie+paint&attributesToRetrieve=firstname,lastname&hitsPerPage=50"
}

Multiple queries

You can send multiple queries with a single API call using a batch of queries:

// perform 3 queries in a single API call:
//  - 1st query targets index `categories`
//  - 2nd and 3rd queries target index `products`

List<APIClient.IndexQuery> queries = new ArrayList<APIClient.IndexQuery>();

queries.add(new APIClient.IndexQuery("categories", new Query(myQueryString).setHitsPerPage(3)));
queries.add(new APIClient.IndexQuery("products", new Query(myQueryString).setHitsPerPage(3).setTagFilters("promotion"));
queries.add(new APIClient.IndexQuery("products", new Query(myQueryString).setHitsPerPage(10)));

JSONObject res = client.multipleQueries(queries);

System.out.println(res.getJSONArray("results").toString())

The resulting JSON answer contains a results array storing the underlying queries answers. The answers order is the same than the requests order.

You can specify a strategy to optimize your multiple queries:

  • none: Execute the sequence of queries until the end.
  • stopIfEnoughMatches: Execute the sequence of queries until the number of hits is reached by the sum of hits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant