Percolate Query Usage

edit

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 example

edit
q
.Percolate(p => p
    .Document(Project.Instance)
    .Field(f => f.Query)
)

Object Initializer syntax example

edit
new PercolateQuery
{
    Document = Project.Instance,
    Field = Infer.Field<ProjectPercolation>(f => f.Query)
}

Example json output.

{
  "percolate": {
    "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 Responses

edit
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 document

edit

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 example

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

Object Initializer syntax example

edit
new PercolateQuery
{
    Index = IndexName.From<Project>(),
    Id = Project.Instance.Name,
    Routing = Project.Instance.Name,
    Field = Infer.Field<ProjectPercolation>(f => f.Query)
}

Example json output.

{
  "percolate": {
    "type": "doc",
    "index": "project",
    "id": "Durgan LLC",
    "routing": "Durgan LLC",
    "field": "query"
  }
}

Handling Responses

edit
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 multiple documents

edit

The percolate query can match multiple documents simultaneously with the indexed percolator queries. Percolating multiple documents in a single request can improve performance as queries only need to be parsed and matched once instead of multiple times.

See the Elasticsearch documentation on percolate query for more details.

Fluent DSL example

edit
q
.Percolate(p => p
    .Documents(Project.Instance, Project.Instance, Project.Instance)
    .Field(f => f.Query)
)

Object Initializer syntax example

edit
new PercolateQuery
{
    Documents = new[] { Project.Instance, Project.Instance, Project.Instance },
    Field = Infer.Field<ProjectPercolation>(f => f.Query)
}

Example json output.

{
  "percolate": {
    "documents": [
      {
        "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
        }
      },
      {
        "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
        }
      },
      {
        "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 Responses

edit
response.Total.Should().Be(1);
response.Hits.Should().NotBeNull();
response.Hits.Count.Should().Be(1);
response.Fields.Count.Should().Be(1);

var field = response.Fields.ElementAt(0);
var values = field.ValuesOf<int>("_percolator_document_slot");
values.Should().Contain(new[] { 0, 1, 2 });

var match = response.Documents.First();
match.Id.Should().Be(PercolatorId);
((IQueryContainer)match.Query).Match.Should().NotBeNull();