Rescore Usageedit

Rescoring can help to improve precision by reordering just the top (eg 100 - 500) documents returned by the query and post_filter phases, using a secondary (usually more costly) algorithm, instead of applying the costly algorithm to all documents in the index.

See the Elasticsearch documentation on Rescoring for more detail.

Single rescore queryedit

Fluent DSL Exampleedit

s => s
.From(10)
.Size(20)
.Query(q => q
    .MatchAll()
)
.Rescore(r => r
    .WindowSize(20)
    .RescoreQuery(rq => rq
        .ScoreMode(ScoreMode.Multiply)
        .Query(q => q
            .ConstantScore(cs => cs
                .Filter(f => f
                    .Terms(t => t
                        .Field(p => p.Tags.First())
                        .Terms("eos", "sit", "sed")
                    )
                )
            )
        )
    )
)

Object Initializer Syntax Exampleedit

new SearchRequest<Project>
{
    From = 10,
    Size = 20,
    Query = new QueryContainer(new MatchAllQuery()),
    Rescore = new Rescore
    {
        WindowSize = 20,
        Query = new RescoreQuery
        {
            ScoreMode = ScoreMode.Multiply,
            Query = new ConstantScoreQuery
            {
                Filter = new TermsQuery
                {
                    Field = Infer.Field<Project>(p => p.Tags.First()),
                    Terms = new[] { "eos", "sit", "sed" }
                }
            }
        }
    }

}

Example json output. 

{
  "from": 10,
  "size": 20,
  "query": {
    "match_all": {}
  },
  "rescore": {
    "window_size": 20,
    "query": {
      "score_mode": "multiply",
      "rescore_query": {
        "constant_score": {
          "filter": {
            "terms": {
              "tags": [
                "eos",
                "sit",
                "sed"
              ]
            }
          }
        }
      }
    }
  }
}

Multiple rescore queriesedit

Fluent DSL Exampleedit

s => s
.From(10)
.Size(20)
.Query(q => q
    .MatchAll()
)
.Rescore(r => r
    .WindowSize(20)
    .RescoreQuery(rq => rq
        .ScoreMode(ScoreMode.Multiply)
        .Query(q => q
            .ConstantScore(cs => cs
                .Filter(f => f
                    .Terms(t => t
                        .Field(p => p.Tags.First())
                        .Terms("eos", "sit", "sed")
                    )
                )
            )
        )
    )
)
.Rescore(rr => rr
    .RescoreQuery(rq => rq
        .ScoreMode(ScoreMode.Total)
        .Query(q => q
            .FunctionScore(fs => fs
                .Functions(f => f
                    .RandomScore(1337)
                )
            )
        )
    )
)

Object Initializer Syntax Exampleedit

new SearchRequest<Project>
{
    From = 10,
    Size = 20,
    Query = new QueryContainer(new MatchAllQuery()),
    Rescore = new MultiRescore
    {
        new Rescore
        {
            WindowSize = 20,
            Query = new RescoreQuery
            {
                ScoreMode = ScoreMode.Multiply,
                Query = new ConstantScoreQuery
                {
                    Filter = new TermsQuery
                    {
                        Field = Infer.Field<Project>(p => p.Tags.First()),
                        Terms = new[] { "eos", "sit", "sed" }
                    }
                }
            }
        },
        new Rescore
        {
            Query = new RescoreQuery
            {
                ScoreMode = ScoreMode.Total,
                Query = new FunctionScoreQuery
                {
                    Functions = new List<IScoreFunction>
                    {
                        new RandomScoreFunction
                        {
                            Seed = 1337
                        }
                    }
                }
            }
        }
    }
}

Example json output. 

{
  "from": 10,
  "size": 20,
  "query": {
    "match_all": {}
  },
  "rescore": [
    {
      "window_size": 20,
      "query": {
        "score_mode": "multiply",
        "rescore_query": {
          "constant_score": {
            "filter": {
              "terms": {
                "tags": [
                  "eos",
                  "sit",
                  "sed"
                ]
              }
            }
          }
        }
      }
    },
    {
      "query": {
        "score_mode": "total",
        "rescore_query": {
          "function_score": {
            "functions": [
              {
                "random_score": {
                  "seed": 1337
                }
              }
            ]
          }
        }
      }
    }
  ]
}