Text search
Text search is an experimental
feature introduced in Memgraph
2.15.1. To use it, start Memgraph with the --experimental-enabled=text-search
flag.
Text search allows you to look up nodes with properties that contain specific content. For a node to be searchable, you first need to create a text index that applies to it.
Text indices and search are powered by the Tantivy full-text search engine.
Create text indices
Text indices are created with the CREATE TEXT INDEX
command. You need to give
a name to the new index and specify which labels it should apply to.
Index all properties
This statement creates a text index named complianceDocuments
for nodes with
the Report
label, indexing all text-indexable properties:
CREATE TEXT INDEX complianceDocuments ON :Report;
Index specific properties
You can also create a text index on a subset of properties by specifying them explicitly:
CREATE TEXT INDEX index_name ON :Label(prop1, prop2, prop3);
For example, to create an index only on the title
and content
properties of Report
nodes:
CREATE TEXT INDEX complianceDocuments ON :Report(title, content);
If you attempt to create an index with an existing name, the statement will fail.
What is indexed
For any given node, if a text index applies to it:
- When no specific properties are listed, all properties with text-indexable types (
String
,Integer
,Float
, orBoolean
) are stored. - When specific properties are listed, only those properties (if they have text-indexable types) are stored.
Changes made within the same transaction are not visible to the index. To see your changes in text search results, you need to commit the transaction first.
Show text indices
To list all text indices in Memgraph, use the SHOW INDEX INFO
statement.
Query text indices
Within a single transaction, repeated text searches may return different results if other transactions have committed their changes to the same index. For consistent results, avoid performing multiple identical searches within the same transaction when concurrent modifications are expected.
Querying text indices is done through query procedures.
Unlike other index types, text indices are not used by the query planner.
Search in specific properties
The text_search.search
procedure finds text-indexed nodes matching the given query.
Input:
index_name: String
- The text index to be searched.search_query: String
- The query applied to the text-indexed nodes.
Output:
node: Node
- A node inindex_name
matching the givensearch_query
.
Usage:
The syntax for the search_query
parameter is available
here.
If the query contains property names, attach the data.
prefix to them.
The following query searches the complianceDocuments
index for nodes with the
value of title
property containing Rules2024
:
CALL text_search.search("complianceDocuments", "data.title:Rules2024")
YIELD node
RETURN node;
Example
CREATE TEXT INDEX complianceDocuments ON :Document;
CREATE (:Document {title: 'Rules2024', version: 1});
CREATE (:Document {title: 'Rules2024', version: 2});
CREATE (:Document {title: 'Other', version: 2});
// Search for documents with title containing 'Rules2024'
CALL text_search.search('complianceDocuments', 'data.title:Rules2024')
YIELD node
RETURN node.title AS title, node.version AS version
ORDER BY version ASC;
Result:
+-------------+-------------+
| title | version |
+-------------+-------------+
| "Rules2024" | 1 |
| "Rules2024" | 2 |
+-------------+-------------+
Boolean expressions
You can use boolean logic in your search queries to create more complex search conditions. Boolean operators include AND
, OR
, and NOT
, and you can use parentheses to group conditions.
Usage:
Boolean expressions allow you to combine multiple search conditions:
AND
- both conditions must be trueOR
- at least one condition must be trueNOT
- condition must be false- Use parentheses
()
to group conditions and control precedence
Example
CREATE TEXT INDEX complianceDocuments ON :Document;
CREATE (:Document {title: 'Rules2023', fulltext: 'nothing'});
CREATE (:Document {title: 'Rules2024', fulltext: 'words', version: 2});
// Search with boolean logic: (title is Rules2023 OR Rules2024) AND fulltext contains 'words'
CALL text_search.search('complianceDocuments', '(data.title:Rules2023 OR data.title:Rules2024) AND data.fulltext:words')
YIELD node
RETURN node.title AS title, node.version AS version
ORDER BY version ASC, title ASC;
Result:
+-------------+-------------+
| title | version |
+-------------+-------------+
| "Rules2024" | 2 |
+-------------+-------------+
Search over all indexed properties
The text_search.search_all
procedure looks for text-indexed nodes where at
least one property value matches the given query.
Unlike text_search.search
, this procedure searches over all properties, and
there is no need to specify property names in the query.
Input:
index_name: String
- The text index to be searched.search_query: String
- The query applied to the text-indexed nodes.
Output:
node: Node
- A node inindex_name
matching the givensearch_query
.
Usage:
The following query searches the complianceDocuments
index for nodes where at
least one property value contains Rules2024
:
CALL text_search.search_all("complianceDocuments", "Rules2024")
YIELD node
RETURN node;
Example
CREATE TEXT INDEX complianceDocuments ON :Document;
CREATE (:Document {title: 'Rules2024', fulltext: 'text words', version: 1});
CREATE (:Document {title: 'Other', fulltext: 'Rules2024 here', version: 3});
// Search for 'Rules2024' across all properties
CALL text_search.search_all('complianceDocuments', 'Rules2024')
YIELD node
RETURN node
ORDER BY node.version ASC;
Result:
+----------------------------------------------------------------------+
| node |
+----------------------------------------------------------------------+
| (:Document {fulltext: "text words", title: "Rules2024", version: 1}) |
| (:Document {fulltext: "Rules2024 here", title: "Other", version: 3}) |
+----------------------------------------------------------------------+
Regex search
The text_search.regex_search
procedure looks for text-indexed nodes where at
least one property value matches the given regular expression (regex).
Input:
index_name: String
- The text index to be searched.search_query: String
- The regex applied to the text-indexed nodes.
Output:
node: Node
- A node inindex_name
matching the givensearch_query
.
Usage:
Regex searches apply to all properties; do not include property names in the search query.
The following query searches the complianceDocuments
index for nodes where at
least one property value satisfies the wor.*s
regex, e.g. “works” and “words”:
CALL text_search.regex_search("complianceDocuments", "wor.*s")
YIELD node
RETURN node;
Example
CREATE TEXT INDEX complianceDocuments ON :Document;
CREATE (:Document {fulltext: 'words and things'});
CREATE (:Document {fulltext: 'more words'});
// Search using regex pattern 'wor.*s'
CALL text_search.regex_search('complianceDocuments', 'wor.*s')
YIELD node
RETURN node
ORDER BY node.fulltext ASC;
Result:
+--------------------------------------------+
| node |
+--------------------------------------------+
| (:Document {fulltext: "more words"}) |
| (:Document {fulltext: "words and things"}) |
+--------------------------------------------+
Aggregations
Aggregations allow you to perform calculations on text search results. By using them, you can efficiently summarize the results, calculate averages or totals, identify min/max values, and count indexed nodes that meet specific criteria.
The text_search.aggregate
procedure lets you define an aggregation and apply
it to the results of a search query.
Input:
index_name: String
- The text index to be searched.search_query: String
- The query applied to the text-indexed nodes.aggregation_query: String
- The aggregation (JSON-formatted) to be applied to the output ofsearch_query
.
Output:
aggregation: String
- JSON-formatted string with the output of aggregation.
Usage:
Aggregation queries and results are strings with Elasticsearch-compatible JSON
format, where "field"
corresponds to node properties. If the search or
aggregation queries contain property names, attach the data.
prefix to them.
The following query counts all nodes in the complianceDocuments
index:
CALL text_search.aggregate(
"complianceDocuments",
"data.title:Rules2024",
'{"count": {"value_count": {"field": "data.version"}}}'
)
YIELD aggregation
RETURN aggregation;
Example
CREATE TEXT INDEX complianceDocuments ON :Document;
CREATE (:Document {title: 'Rules2024', version: 1});
CREATE (:Document {title: 'Rules2024', version: 2});
// Count documents matching the search query
CALL text_search.aggregate(
'complianceDocuments',
'data.title:Rules2024',
'{"count":{"value_count":{"field":"data.version"}}}'
)
YIELD aggregation
RETURN aggregation;
Result:
+-------------------------------+
| aggregation |
+-------------------------------+
| "{\"count\":{\"value\":2.0}}" |
+-------------------------------+
Drop text indices
Text indices are dropped with the DROP TEXT INDEX
command. You need to give
the name of the index to be deleted.
This statement drops the text index named complianceDocuments
:
DROP TEXT INDEX complianceDocuments;
Compatibility
Even though text search is an experimental feature, it supports most usage modalities that are available in Memgraph from version 3.5. Refer to the table below for an overview:
Feature | Support |
---|---|
Multitenancy | ✅ Yes |
Durability | ✅ Yes |
Replication | ✅ Yes (from version 3.5) |
Concurrent transactions | ⚠️ Yes, but search results may vary within transactions |
Storage modes | ❌ No (doesn’t work in IN_MEMORY_ANALYTICAL) |