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.

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)
}

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"
  }
}

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:

  • index in which the document resides
  • type of the document
  • field that contains the query
  • id of the document
  • document_type type / mapping of the document

See the Elasticsearch documentation on percolate query for more details.

Fluent DSL exampleedit

q
.Percolate(p => p
        .Type<Project>()
        .Index<Project>()
        .Id(Project.Instance.Name)
        .Field(f => f.Query)
        .DocumentType<Project>() 
)

specify the type, index, id, field, document_type of the document to fetch, to percolate.

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();