RediSearch 命令¶
这些是与 RediSearch 模块 交互的命令。下面是一个简短的示例,以及命令本身的文档。在下面的示例中,正在创建一个名为 my_index 的索引。当未指定索引名称时,将创建一个名为 idx 的索引。
创建搜索索引并显示其信息
import redis
from redis.commands.search.field import TextField
r = redis.Redis()
index_name = "my_index"
schema = (
TextField("play", weight=5.0),
TextField("ball"),
)
r.ft(index_name).create_index(schema)
print(r.ft(index_name).info())
- class redis.commands.search.commands.SearchCommands[源代码]¶
Search commands.
- add_document(doc_id, nosave=False, score=1.0, payload=None, replace=False, partial=False, language=None, no_create=False, **fields)[源代码]¶
Add a single document to the index.
- 参数:
doc_id (str) -- the id of the saved document.
nosave (bool) -- if set to true, we just index the document, and don't save a copy of it. This means that searches will just return ids.
score (float) -- the document ranking, between 0.0 and 1.0
payload (bool) -- optional inner-index payload we can save for fast access in scoring functions
replace (bool) -- if True, and the document already is in the index, we perform an update and reindex the document
partial (bool) -- if True, the fields specified will be added to the existing document. This has the added benefit that any fields specified with no_index will not be reindexed again. Implies replace
language (str | None) -- Specify the language used for document tokenization.
no_create (str) -- if True, the document is only updated and reindexed if it already exists. If the document does not exist, an error will be returned. Implies replace
fields (List[str]) -- kwargs dictionary of the document fields to be saved and/or indexed. NOTE: Geo points shoule be encoded as strings of "lon,lat"
- add_document_hash(doc_id, score=1.0, language=None, replace=False)[源代码]¶
Add a hash document to the index.
### Parameters
- doc_id: the document's id. This has to be an existing HASH key
in Redis that will hold the fields the index needs.
score: the document ranking, between 0.0 and 1.0
- replace: if True, and the document already is in the index, we
perform an update and reindex the document
language: Specify the language used for document tokenization.
- aggregate(query, query_params=None)[源代码]¶
Issue an aggregation query.
### Parameters
query: This can be either an AggregateRequest, or a Cursor
An AggregateResult object is returned. You can access the rows from its rows property, which will always yield the rows of the result.
For more information see FT.AGGREGATE.
- 参数:
query (str | Query)
query_params (Dict[str, str | int | float] | None)
- aliasadd(alias)[源代码]¶
Alias a search index - will fail if alias already exists
### Parameters
alias: Name of the alias to create
For more information see FT.ALIASADD.
- 参数:
alias (str)
- aliasdel(alias)[源代码]¶
Removes an alias to a search index
### Parameters
alias: Name of the alias to delete
For more information see FT.ALIASDEL.
- 参数:
alias (str)
- aliasupdate(alias)[源代码]¶
Updates an alias - will fail if alias does not already exist
### Parameters
alias: Name of the alias to create
For more information see FT.ALIASUPDATE.
- 参数:
alias (str)
- alter_schema_add(fields)[源代码]¶
Alter the existing search index by adding new fields. The index must already exist.
### Parameters:
fields: a list of Field objects to add for the index
For more information see FT.ALTER.
- 参数:
fields (List[str])
- batch_indexer(chunk_size=100)[源代码]¶
Create a new batch indexer from the client with a given chunk size
- config_get(option)[源代码]¶
Get runtime configuration option value.
### Parameters
option: the name of the configuration option.
For more information see FT.CONFIG GET.
- 参数:
option (str)
- 返回类型:
str
- config_set(option, value)[源代码]¶
Set runtime configuration option.
### Parameters
option: the name of the configuration option.
value: a value for the configuration option.
For more information see FT.CONFIG SET.
- 参数:
option (str)
value (str)
- 返回类型:
bool
- create_index(fields, no_term_offsets=False, no_field_flags=False, stopwords=None, definition=None, max_text_fields=False, temporary=None, no_highlight=False, no_term_frequencies=False, skip_initial_scan=False)[源代码]¶
Creates the search index. The index must not already exist.
For more information, see https://redis.io/commands/ft.create/
- 参数:
fields (List[Field]) -- A list of Field objects.
no_term_offsets (bool) -- If true, term offsets will not be saved in the index.
no_field_flags (bool) -- If true, field flags that allow searching in specific fields will not be saved.
stopwords (List[str] | None) -- If provided, the index will be created with this custom stopword list. The list can be empty.
definition (IndexDefinition | None) -- If provided, the index will be created with this custom index definition.
max_text_fields -- If true, indexes will be encoded as if there were more than 32 text fields, allowing for additional fields beyond 32.
temporary -- Creates a lightweight temporary index which will expire after the specified period of inactivity. The internal idle timer is reset whenever the index is searched or added to.
no_highlight (bool) -- If true, disables highlighting support. Also implied by no_term_offsets.
no_term_frequencies (bool) -- If true, term frequencies will not be saved in the index.
skip_initial_scan (bool) -- If true, the initial scan and indexing will be skipped.
- delete_document(doc_id, conn=None, delete_actual_document=False)[源代码]¶
Delete a document from index Returns 1 if the document was deleted, 0 if not
### Parameters
- delete_actual_document: if set to True, RediSearch also delete
the actual document if it is in the index
- dict_add(name, *terms)[源代码]¶
Adds terms to a dictionary.
### Parameters
name: Dictionary name.
terms: List of items for adding to the dictionary.
For more information see FT.DICTADD.
- 参数:
name (str)
terms (List[str])
- dict_del(name, *terms)[源代码]¶
Deletes terms from a dictionary.
### Parameters
name: Dictionary name.
terms: List of items for removing from the dictionary.
For more information see FT.DICTDEL.
- 参数:
name (str)
terms (List[str])
- dict_dump(name)[源代码]¶
Dumps all terms in the given dictionary.
### Parameters
name: Dictionary name.
For more information see FT.DICTDUMP.
- 参数:
name (str)
- dropindex(delete_documents=False)[源代码]¶
Drop the index if it exists. Replaced drop_index in RediSearch 2.0. Default behavior was changed to not delete the indexed documents.
### Parameters:
delete_documents: If True, all documents will be deleted.
For more information see FT.DROPINDEX.
- 参数:
delete_documents (bool)
- explain(query, query_params=None)[源代码]¶
Returns the execution plan for a complex query.
For more information see FT.EXPLAIN.
- 参数:
query (str | Query)
query_params (Dict[str, str | int | float] | None)
- get(*ids)[源代码]¶
Returns the full contents of multiple documents.
### Parameters
ids: the ids of the saved documents.
- info()[源代码]¶
Get info an stats about the the current index, including the number of documents, memory consumption, etc
For more information see FT.INFO.
- profile(query, limited=False, query_params=None)[源代码]¶
Performs a search or aggregate command and collects performance information.
### Parameters
query: This can be either an AggregateRequest, Query or string. limited: If set to True, removes details of reader iterator. query_params: Define one or more value parameters. Each parameter has a name and a value.
- 参数:
query (str | Query | AggregateRequest)
limited (bool)
query_params (Dict[str, str | int | float] | None)
- search(query, query_params=None)[源代码]¶
Search the index for a given query, and return a result of documents
### Parameters
- query: the search query. Either a text for simple queries with
default parameters, or a Query object for complex queries. See RediSearch's documentation on query format
For more information see FT.SEARCH.
- 参数:
query (str | Query)
query_params (Dict[str, str | int | float | bytes] | None)
- spellcheck(query, distance=None, include=None, exclude=None)[源代码]¶
Issue a spellcheck query
- 参数:
query -- search query.
distance -- the maximal Levenshtein distance for spelling suggestions (default: 1, max: 4).
include -- specifies an inclusion custom dictionary.
exclude -- specifies an exclusion custom dictionary.
For more information see FT.SPELLCHECK.
- sugadd(key, *suggestions, **kwargs)[源代码]¶
Add suggestion terms to the AutoCompleter engine. Each suggestion has a score and string. If kwargs["increment"] is true and the terms are already in the server's dictionary, we increment their scores.
For more information see FT.SUGADD.
- sugdel(key, string)[源代码]¶
Delete a string from the AutoCompleter index. Returns 1 if the string was found and deleted, 0 otherwise.
For more information see FT.SUGDEL.
- 参数:
key (str)
string (str)
- 返回类型:
int
- sugget(key, prefix, fuzzy=False, num=10, with_scores=False, with_payloads=False)[源代码]¶
Get a list of suggestions from the AutoCompleter, for a given prefix.
Parameters:
- prefixstr
The prefix we are searching. Must be valid ascii or utf-8
- fuzzybool
If set to true, the prefix search is done in fuzzy mode. NOTE: Running fuzzy searches on short (<3 letters) prefixes can be very slow, and even scan the entire index.
- with_scoresbool
If set to true, we also return the (refactored) score of each suggestion. This is normally not needed, and is NOT the original score inserted into the index.
- with_payloadsbool
Return suggestion payloads
- numint
The maximum number of results we return. Note that we might return less. The algorithm trims irrelevant suggestions.
Returns:
- list:
A list of Suggestion objects. If with_scores was False, the score of all suggestions is 1.
For more information see FT.SUGGET.
- 参数:
key (str)
prefix (str)
fuzzy (bool)
num (int)
with_scores (bool)
with_payloads (bool)
- 返回类型:
List[SuggestionParser]
- suglen(key)[源代码]¶
Return the number of entries in the AutoCompleter index.
For more information see FT.SUGLEN.
- 参数:
key (str)
- 返回类型:
int
- syndump()[源代码]¶
Dumps the contents of a synonym group.
The command is used to dump the synonyms data structure. Returns a list of synonym terms and their synonym group ids.
For more information see FT.SYNDUMP.
- synupdate(groupid, skipinitial=False, *terms)[源代码]¶
Updates a synonym group. The command is used to create or update a synonym group with additional terms. Only documents which were indexed after the update will be affected.
Parameters:
- groupid :
Synonym group id.
- skipinitialbool
If set to true, we do not scan and index.
- terms :
The terms.
For more information see FT.SYNUPDATE.
- 参数:
groupid (str)
skipinitial (bool)
terms (List[str])
- tagvals(tagfield)[源代码]¶
Return a list of all possible tag values
### Parameters
tagfield: Tag field name
For more information see FT.TAGVALS.
- 参数:
tagfield (str)