Percolate Query Usageedit
The percolate query can be used to match queries stored in an index. The percolate query itself contains the document that will be used as query to match with the stored queries.
In order for the percolate query to work, the index in which your stored queries reside must contain a mapping for documents that you wish to percolate, so that they are parsed correctly at query time.
See the Elasticsearch documentation on percolate query for more details.
In this example, we have a document stored with a query field that is mapped as a percolator type. This field
contains a match query.
foreach (var index in values.Values)
{
this.Client.CreateIndex(index, c => c
.Mappings(m => m
.Map<Project>(mm => mm.AutoMap()
.Properties(Seeder.ProjectProperties)
)
.Map<PercolatedQuery>(mm => mm.AutoMap()
.Properties(Seeder.PercolatedQueryProperties)
)
)
);
this.Client.Index(new PercolatedQuery
{
Id = PercolatorId,
Query = new QueryContainer(new MatchQuery
{
Field = Infer.Field<Project>(f => f.LeadDeveloper.FirstName),
Query = "Martijn"
})
}, d => d.Index(index));
this.Client.Refresh(index);
}Fluent DSL Exampleedit
f => f.Query(QueryFluent).Index(CallIsolatedValue).AllTypes()
Object Initializer Syntax Exampleedit
new SearchRequest<PercolatedQuery>(CallIsolatedValue, Types.All)
{
Query = this.QueryInitializer
}Example json output.
{
"percolate": {
"document_type": "project",
"document": {
"name": "Koch, Collier and Mohr",
"state": "BellyUp",
"startedOn": "2015-01-01T00:00:00",
"lastActivity": "0001-01-01T00:00:00",
"leadDeveloper": {
"gender": "Male",
"id": 0,
"firstName": "Martijn",
"lastName": "Laarman"
},
"location": {
"lat": 42.1523,
"lon": -80.321
}
},
"field": "query"
}
}
Fluent DSL Exampleedit
q
.Percolate(p => p
.DocumentType(typeof(Project))
.Document(Project.Instance)
.Field(f => f.Query)
)Object Initializer Syntax Exampleedit
new PercolateQuery
{
DocumentType = typeof(Project),
Document = Project.Instance,
Field = Infer.Field<PercolatedQuery>(f => f.Query)
}Handling Responsesedit
response.Total.Should().BeGreaterThan(0); response.Hits.Should().NotBeNull(); response.Hits.Count().Should().BeGreaterThan(0); var match = response.Documents.First(); match.Id.Should().Be(PercolatorId); ((IQueryContainer)match.Query).Match.Should().NotBeNull();
Percolate an existing documentedit
Instead of specifying the source of the document being percolated, the source can also be retrieved from an already stored document. The percolate query will then internally execute a get request to fetch that document.
The required fields to percolate an existing document are:
-
indexin which the document resides -
typeof the document -
fieldthat contains the query -
idof the document -
document_typetype / mapping of the document
See the Elasticsearch documentation on percolate query for more details.
Fluent DSL Exampleedit
Object Initializer Syntax Exampleedit
new PercolateQuery
{
Type = typeof(Project),
Index = IndexName.From<Project>(),
Id = Project.Instance.Name,
DocumentType = typeof(Project),
Field = Infer.Field<PercolatedQuery>(f => f.Query)
}Example json output.
{
"percolate": {
"type": "project",
"index": "project",
"id": "Durgan LLC",
"document_type": "project",
"field": "query"
}
}
Handling Responsesedit
response.Total.Should().BeGreaterThan(0); response.Hits.Should().NotBeNull(); response.Hits.Count().Should().BeGreaterThan(0); var match = response.Documents.First(); match.Id.Should().Be(PercolatorId); ((IQueryContainer)match.Query).Match.Should().NotBeNull();