Utilisez les fonctionnalités de recherche de texte dans Cloud Firestore pour rechercher des chaînes spécifiques dans les documents.
Édition requise
La recherche de texte nécessite une base de données Firestore édition Enterprise.
Avant de commencer
Pour effectuer une recherche textuelle, vous devez d'abord créer des index de texte pour les champs dans lesquels vous devez effectuer la recherche.
Effectuer une recherche textuelle
Pour effectuer une recherche de texte, utilisez l'expression documentMatches dans le paramètre query de l'étape search(...).
L'opération ne recherche que dans les champs indexés avec un index de texte. Si plusieurs index sont disponibles, Cloud Firestore en sélectionne un à utiliser pour l'opération.
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles') }));
iOS
let snapshot = try await db.pipeline().collection("restaurants") .search(query: DocumentMatches("waffles")) .execute()
Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles")))
Java
Pipeline pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles")));
Node.js
await db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles') }) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import DocumentMatches results = ( client.pipeline() .collection("restaurants") .search(DocumentMatches("waffles")) .execute() )
Java
Pipeline.Snapshot results1 = firestore.pipeline().collection("restaurants") .search(Search.withQuery(documentMatches("waffles"))) .execute().get();
Accéder
snapshot := client.Pipeline(). Collection("restaurants"). Search(firestore.WithSearchQuery(firestore.DocumentMatches("waffles"))). Execute(ctx)
Rechercher un terme exact
Pour rechercher un terme exact, placez-le entre guillemets (") :
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: documentMatches('"belgian waffles"') }));
iOS
let snapshot = try await db.pipeline().collection("restaurants") .search(query: DocumentMatches("\"belgian waffles\"")) .execute()
Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("\"belgian waffles\"")))
Java
Pipeline pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("\"belgian waffles\"")));
Node.js
await db.pipeline().collection('restaurants') .search({ query: documentMatches('"belgian waffles"') }) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import DocumentMatches results = ( client.pipeline() .collection("restaurants") .search(DocumentMatches('"belgian waffles"')) .execute() )
Java
Pipeline.Snapshot results2 = firestore.pipeline().collection("restaurants") .search(Search.withQuery(documentMatches("\"belgian waffles\""))) .execute().get();
Accéder
snapshot := client.Pipeline(). Collection("restaurants"). Search(firestore.WithSearchQuery(firestore.DocumentMatches("\"belgian waffles\""))). Execute(ctx)
Rechercher une combinaison de termes
Pour rechercher une combinaison de termes (AND logique), séparez les termes par des espaces :
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles eggs') }));
iOS
let snapshot = try await db.pipeline().collection("restaurants") .search(query: DocumentMatches("waffles eggs")) .execute()
Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles eggs")))
Java
Pipeline pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles eggs")));
Node.js
await db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles eggs') }) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import DocumentMatches results = ( client.pipeline() .collection("restaurants") .search(DocumentMatches("waffles eggs")) .execute() )
Java
firestore.collection("restaurants").add(new HashMap<String, Object>() {{ put("name", "Morning Diner"); put("description", "Start your day with waffles and eggs."); }}); Pipeline.Snapshot results3 = firestore.pipeline().collection("restaurants") .search(Search.withQuery(documentMatches("waffles eggs"))) .execute().get();
Accéder
snapshot := client.Pipeline(). Collection("restaurants"). Search(firestore.WithSearchQuery(firestore.DocumentMatches("waffles eggs"))). Execute(ctx)
Exclure un terme
Pour exclure un terme, ajoutez un trait d'union (-) devant :
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: documentMatches('coffee -waffles') }));
iOS
let snapshot = try await db.pipeline().collection("restaurants") .search(query: DocumentMatches("coffee -waffles")) .execute()
Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles eggs")))
Java
Pipeline pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("coffee -waffles")));
Node.js
await db.pipeline().collection('restaurants') .search({ query: documentMatches('-waffles') }) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import DocumentMatches results = ( client.pipeline() .collection("restaurants") .search(DocumentMatches("-waffles")) .execute() )
Java
firestore.collection("restaurants").add(new HashMap<String, Object>() {{ put("name", "City Coffee"); put("description", "Premium coffee and pastries."); }}); Pipeline.Snapshot results4 = firestore.pipeline().collection("restaurants") .search(Search.withQuery(documentMatches("-waffles"))) .execute().get();
Accéder
snapshot := client.Pipeline(). Collection("restaurants"). Search(firestore.WithSearchQuery(firestore.DocumentMatches("-waffles"))). Execute(ctx)
Vous pouvez également exclure une expression, par exemple pizza -"New York".
Trier les résultats
Par défaut, Cloud Firestore trie les résultats par date de création des documents, du plus récent au plus ancien. Vous pouvez également trier les résultats par score de recherche, mais cela nécessite plus de calculs pour déterminer et comparer le score exact de chaque document :
Pour trier les résultats par score de recherche :
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles'), sort: score().descending() }));
iOS
let snapshot = try await db.pipeline().collection("restaurants")
.search(
query: DocumentMatches("waffles"),
sort: [Score().descending()]
)
.execute()Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage .withQuery(documentMatches("waffles")) .withSort(score().descending()) )
Node.js
await db.pipeline().collection('restaurants') .search({ query: documentMatches('waffles'), sort: score().descending() }) .execute();
Ajouter des champs aux documents renvoyés par l'étape de recherche
Vous pouvez utiliser addFields pour ajouter des champs aux documents renvoyés par l'étape de recherche. Les expressions renvoyant des valeurs calculées par l'étape de recherche, comme score(), peuvent être utilisées dans addFields lors de l'étape de recherche pour écrire ces valeurs dans les documents de sortie.
L'exemple suivant ajoute le champ "score" aux documents renvoyés par l'étape de recherche :
Web
const result = await execute(db.pipeline().collection('restaurants') .search({ query: 'menu:waffles', addFields: [ score().as('score'), ] }));
iOS
let snapshot = try await db.pipeline().collection("restaurants") .search( query: DocumentMatches("waffles"), addFields: [ Score().as("score") ] ) .execute()
Kotlin
val pipeline = db.pipeline().collection("restaurants") .search(SearchStage.withQuery(documentMatches("waffles eggs")))
Java
Pipeline pipeline = db.pipeline().collection("restaurants") .search( SearchStage.withQuery(documentMatches("menu:waffles")) .withAddFields(score().alias("score")));
Node.js
await db.pipeline().collection('restaurants') .search({ query: field('menu').matches('waffles'), addFields: [ score().as('score'), ] }).execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import DocumentMatches, Score from google.cloud.firestore_v1.pipeline_stages import SearchOptions results = ( client.pipeline() .collection("restaurants") .search( SearchOptions( query=DocumentMatches("menu:waffles"), add_fields=[Score().as_("score")], ) ) .execute() )
Java
Pipeline.Snapshot results5 = firestore.pipeline().collection("restaurants") .search(Search.withQuery(field("menu").regexMatch("waffles")) .withAddFields(score().as("score"))) .execute().get();
Go
snapshot := client.Pipeline(). Collection("restaurants"). Search( firestore.WithSearchQuery(firestore.FieldOf("menu").RegexMatch("waffles")), firestore.WithSearchAddFields(firestore.Score().As("score")), ). Execute(ctx)