Full-text search is a common feature used by a lot of modern applications.
You might already know that it is possible to implement full-text search with $regex queries,
but this approach will not work efficiently when there are massive objects or fields in a class.
To solve the problem, LeanCloud provides dedicated REST APIs for full-text search powered by the Elasticsearch engine.
Overview
URL
HTTP Method
Functionality
/search/select
GET
full-text search
/search/mlt
GET
"more like this" query; used to find similar documents
Before using search APIs, you must first enable search indexes for classes.
To do so, go to your app's Dashboard > LeanStorage > In-app searching, and click on the Add Class button.
Be aware of the following limitations:
An application with a Developer/Business Plan can enable search indexes for at most 5/10 classes.
An application with a Developer/Business Plan can enable search indexes for at most 5/10 columns for each search-enabled class.
However, objectId, createdAt, and updatedAt are always enabled for a search-enabled class, and they do not count into the column limit.
If LeanCloud has not received any full-text search API requests within two weeks after you enabled search indexes for a class, the search indexes of this class will be disabled.
Full-Text Search
To search for objects, you need to send a GET request to /search/select.
For example, to search for dennis in GameScore class:
Keywords. You need to specify either this parameter or the likeObjectIds parameter.
likeObjectIds
Optional
Comma-seperated objectId list. You need to specify either this parameter or the like parameter.
min_term_freq
Optional
The minimum term frequency below which the terms will be ignored from the input document. Defaults to 2.
min_doc_freq
Optional
The minimum document frequency below which the terms will be ignored from the input document. Defaults to 5.
max_doc_freq
Optional
The maximum document frequency above which the terms will be ignored from the input document. This could be useful to ignore highly frequent words such as stop words. Defaults to 0.
skip
Optional
Skipped results. Used for pagination.
limit
Optional
The number of returned objects. The default value is 100 and the maximum value is 1000.
fields
Optional
Comma-seperated column list.
highlights
Optional
Highlighted keywords. It can be a comma-separated string or wildcard *.
include
Optional
Include objects referenced by Pointers. Example: user,comment.
Full-Text Search REST API Guide
Full-text search is a common feature used by a lot of modern applications. You might already know that it is possible to implement full-text search with
$regex
queries, but this approach will not work efficiently when there are massive objects or fields in a class. To solve the problem, LeanCloud provides dedicated REST APIs for full-text search powered by the Elasticsearch engine.Overview
The current API version is
1.1
. For request format and response format, please refer to the Request Format section and Response Format section of REST API Guide.Enable Search Indexes
Before using search APIs, you must first enable search indexes for classes. To do so, go to your app's Dashboard > LeanStorage > In-app searching, and click on the
Add Class
button.Be aware of the following limitations:
objectId
,createdAt
, andupdatedAt
are always enabled for a search-enabled class, and they do not count into the column limit.Full-Text Search
To search for objects, you need to send a
GET
request to/search/select
. For example, to search fordennis
inGameScore
class:The following query parameters are available:
q
skip
limit
sid
fields
highlights
*
.clazz
include
user,comment
.order
-
for descending. Example:-score,createdAt
.sort
The response will be something like:
In the above response body:
hits
: Total number of matched results._highlight
: Highlighted search results with keywords wrapped inem
tags. If you did not pass thehighlights
query parameter,null
will be returned.GeoPoints Sorting
GeoPoints can be sorted by their distances to a given GeoPoint. For example, to sort players near a GeoPoint (
[39.9, 116.4]
):More Like This Query
To search for similar objects, you can send a
GET
request to/search/mlt
. This may be used in a recommendation system.For example, to search for posts with tags similar to
clojure
:The response body will be something like:
You can use the
likeObjectIds
parameter instead oflike
to search for posts with similar tags:Below are all the query parameters available:
clazz
like
likeObjectIds
parameter.likeObjectIds
like
parameter.min_term_freq
min_doc_freq
max_doc_freq
skip
limit
fields
highlights
*
.include
user,comment
.You can also refer to the Elasticsearch documentation for more information.