public class SearchAbility extends Object
The fusion search service provides the index and search capabilities. Applications can customize index attributes and index data structures.
This class provides methods for connecting to the fusion search service, setting the index attributes, adding, deleting, updating, and clearing indexes, listening to index changes, and obtaining search sessions.
Before using the fusion search service, an application must create a
SearchAbility instance and call connect(ohos.data.search.connect.ServiceConnectCallback)
to connect to the service. The application must call disconnect() to
disconnect from the service to release resources.
SystemCapability.Data#DATA_SEARCH| 构造器 | 说明 |
|---|---|
SearchAbility(Context context) |
A parameterized constructor used to create a
SearchAbility instance. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
SearchSession |
beginSearch(String groupId,
String bundleName) |
Starts a search session in the index library with a specified group ID and bundle name.
|
int |
clearIndex(String groupId,
String bundleName,
Map<String,List<String>> deviceIds) |
Clears indexes that match a device ID list from the index library
with a specified bundle name and group ID.
|
int |
clearIndexForm(String bundleName) |
Clears the index attributes configured for a specified bundle name.
|
boolean |
connect(ServiceConnectCallback callback) |
Asynchronously connects to the fusion search service.
|
List<IndexData> |
delete(String groupId,
String bundleName,
List<IndexData> indexDataList) |
Deletes a list of indexes from the index library with a specified bundle name and group ID.
|
int |
deleteByQuery(String groupId,
String bundleName,
String queryJsonStr) |
Deletes indexes that match a specified query string from the index library
with a specified bundle name and group ID.
|
List<String> |
deleteByTerm(String groupId,
String bundleName,
String indexFieldName,
List<String> indexFieldValueList) |
Deletes indexes that match any given field value in a specified field
from the index library with a specified bundle name and group ID.
|
boolean |
disconnect() |
Disconnects from the fusion search service.
|
void |
endSearch(String groupId,
String bundleName,
SearchSession searchSession) |
Ends a search session to release resources.
|
List<IndexForm> |
getIndexForm(String bundleName) |
Obtains the index attributes configured for a specified bundle name.
|
int |
getIndexFormVersion(String bundleName) |
Obtains the version of the index attributes configured for a specified bundle name.
|
SearchableEntity |
getSearchableEntity(String bundleName) |
Obtains the
SearchableEntity of a specified search-source application. |
List<SearchableEntity> |
getSearchableEntityList() |
Obtains the list of
SearchableEntity objects that have been set. |
boolean |
hasConnected() |
Checks whether the fusion search service is connected.
|
List<IndexData> |
insert(String groupId,
String bundleName,
List<IndexData> indexDataList) |
Inserts a list of indexes into the index library with a specified bundle name and group ID.
|
void |
registerIndexChangeListener(String groupId,
String bundleName,
IIndexChangeListener listener) |
Registers an index change listener.
|
int |
setIndexForm(String bundleName,
int version,
List<IndexForm> indexFormList) |
Sets index field attributes.
|
int |
setIndexForm(String bundleName,
int version,
List<IndexForm> indexFormList,
IndexSchemaType schemaType) |
Sets index field attributes.
|
SearchResult |
setSearchableEntity(SearchableEntity searchableEntity) |
Sets a
SearchableEntity. |
void |
unRegisterIndexChangeListener(String groupId,
String bundleName,
IIndexChangeListener listener) |
Deregisters an index change listener.
|
List<IndexData> |
update(String groupId,
String bundleName,
List<IndexData> indexDataList) |
Updates a list of indexes in the index library with a specified bundle name and group ID.
|
public SearchAbility(Context context)
SearchAbility instance.
To use the fusion search service, an application must instantiate the class and connect to the service.
context - Indicates the application context.public boolean connect(ServiceConnectCallback callback)
Before using the fusion search service, an application must call this method.
After the connection is set up, the application receives a callback through
the passed callback.
callback - Indicates the callback for receiving the connection status.true if the connection is successful; returns false otherwise.public boolean disconnect()
When an application does not need to use the fusion search service any longer, the application should call this method to disconnect from the service to release resources.
true if the disconnection is successful; returns false otherwise.public boolean hasConnected()
true if the service is connected; returns false otherwise.public int setIndexForm(String bundleName, int version, List<IndexForm> indexFormList)
Index attributes must be set before an application performs indexing and search.
bundleName - Indicates the bundle name of the index library.version - Indicates the version of the index attribute. The initial value is 1.
The value should be incremented each time an attribute is set.indexFormList - Indicates the index attribute list of index fields.SearchErrorCode#SUCCESS if the operation is successful;
returns SearchErrorCode#FAIL otherwise.public List<IndexForm> getIndexForm(String bundleName)
bundleName - Indicates the bundle name of the index library.public int getIndexFormVersion(String bundleName)
bundleName - Indicates the bundle name of the index library.public int clearIndexForm(String bundleName)
bundleName - Indicates the bundle name of the index library.SearchErrorCode#SUCCESS if the operation is successful;
returns SearchErrorCode#FAIL otherwise.public List<IndexData> insert(String groupId, String bundleName, List<IndexData> indexDataList)
Use SearchParameter#DEFAULT_GROUP as the group ID when inserting local indexes.
The list of indexes that fail to insert is returned. If all indexes are successfully inserted,
an empty list is returned.
groupId - Indicates the group ID.bundleName - Indicates the bundle name of the index library.indexDataList - Indicates the indexes to insert.public List<IndexData> update(String groupId, String bundleName, List<IndexData> indexDataList)
Use SearchParameter#DEFAULT_GROUP as the group ID when updating local indexes.
The list of indexes that fail to update is returned. If all indexes are successfully updated,
an empty list is returned.
groupId - Indicates the group ID.bundleName - Indicates the bundle name of the index library.indexDataList - Indicates the indexes to update.public List<IndexData> delete(String groupId, String bundleName, List<IndexData> indexDataList)
Use SearchParameter#DEFAULT_GROUP as the group ID when deleting local indexes.
The list of indexes that fail to delete is returned. If all indexes are successfully deleted,
an empty list is returned.
groupId - Indicates the group ID.bundleName - Indicates the bundle name of the index library.indexDataList - Indicates the indexes to delete.public List<String> deleteByTerm(String groupId, String bundleName, String indexFieldName, List<String> indexFieldValueList)
Use SearchParameter#DEFAULT_GROUP as the group ID when deleting local indexes.
If any matched index fails to delete, the passed indexFieldValueList is returned.
If all matched indexes are successfully deleted, an empty list is returned.
groupId - Indicates the group ID.bundleName - Indicates the bundle name of the index library.indexFieldName - Indicates the index field name.indexFieldValueList - Indicates the index field value list.indexFieldValueList if the deletion fails; returns an empty list otherwise.public int deleteByQuery(String groupId, String bundleName, String queryJsonStr)
Use SearchParameter#DEFAULT_GROUP as the group ID when deleting local indexes.
groupId - Indicates the group ID.bundleName - Indicates the bundle name of the index library.queryJsonStr - Indicates the query string.SearchErrorCode#SUCCESS if the operation is successful;
returns SearchErrorCode#FAIL otherwise.public int clearIndex(String groupId, String bundleName, Map<String,List<String>> deviceIds)
deviceIds is
null, all indexes in the specified index library are cleared.groupId - Indicates the group ID.bundleName - Indicates the bundle name of the index library.deviceIds - Indicates the list of devices. For each key-value pair in deviceIds,
the key is the field name of the device ID and the value is the list of device IDs
for which indexes are to delete.SearchErrorCode#SUCCESS if the operation is successful;
returns SearchErrorCode#FAIL otherwise.public SearchSession beginSearch(String groupId, String bundleName)
An application can use the returned search session to perform a search.
Search sessions are thread-safe. If no index exists in the specified index library,
null is returned. Therefore, the application should check whether the
returned search session is null before using it. If the returned search session
is not null, the application should call the endSearch method to
release resources when the session is no longer needed.
groupId - Indicates the group ID.bundleName - Indicates the bundle name of the index library.null otherwise.public void endSearch(String groupId, String bundleName, SearchSession searchSession)
When an application does not need to use a search session any longer, the application should call this method to release resources.
Note: This method must be used each time the SearchAbility#beginSearch(String, String)
method is called. Generally, the try-finally block is used.
groupId - Indicates the group ID.bundleName - Indicates the bundle name of the index library.searchSession - Indicates the search session to end.public void registerIndexChangeListener(String groupId, String bundleName, IIndexChangeListener listener)
Note: An application can listen for only index changes in its own bundle.
groupId - Indicates the group ID.bundleName - Indicates the bundle name of the index library.listener - Indicates the index change callback.public void unRegisterIndexChangeListener(String groupId, String bundleName, IIndexChangeListener listener)
Note: An application can listen for only index changes in its own bundle.
groupId - Indicates the group ID.bundleName - Indicates the bundle name of the index library.listener - Indicates the index change callback.public SearchResult setSearchableEntity(SearchableEntity searchableEntity)
SearchableEntity.
This method is called by the search-source application.
The search application can use the SearchableEntity to search indexes
of the search-source application and display the search result for users.
searchableEntity - Indicates the SearchableEntity to set.SearchResult#SUCCESS if the setting is successful;
returns SearchResult#FILE_RW_EXCEPTION if a file exception occurs;
returns SearchResult#ILLEGAL_ARGUMENT if searchableEntity is invalid;
returns SearchResult#PERMISSION_DENIED if the search-source application has no permission
to set the SearchableEntity;
returns SearchResult#IPC_EXCEPTION if an IPC exception occurs.public SearchableEntity getSearchableEntity(String bundleName)
SearchableEntity of a specified search-source application.bundleName - Indicates the bundle name of the search-source application.SearchableEntity if obtained; returns null otherwise.public List<SearchableEntity> getSearchableEntityList()
SearchableEntity objects that have been set.SearchableEntity objects.public int setIndexForm(String bundleName, int version, List<IndexForm> indexFormList, IndexSchemaType schemaType)
You must set index field attributes prior to indexing and search operations.
Call this method when you need to set an index field attribute for the first time or update
an index field attribute.
Before calling this method, you can use the SearchAbility#getIndexFormVersion(String)
method to check whether an index field attribute has been set for the application
and whether the version of the index field attribute to set is incremented based on that of
the previous index field attribute.
bundleName - Indicates the name of the index library, which is the application bundle name.version - Indicates the version of the index field attribute. The initial value
is 1. The value should be incremented each time an attribute is set.indexFormList - Indicates the list of index field attributes. This parameter works with
schemaType and its value could be any of the following:
schemaType) are used.schemaType).
IndexForm.isPrimaryKey cannot be set to true in any of the custom index
attributes.schemaType is set
to IndexSchemaType#CUSTOM.schemaType - Indicates the index schema type, as described in schema.IndexSchemaType.SearchErrorCode#SUCCESS if the operation is successful;
returns SearchErrorCode#FAIL otherwise.