Elasticsearch.Net - Changes after upgrading from v

2019-08-21 10:11发布

问题:

I have noticed a lot of changes in ES after upgrading it from v1.9 to v5.4.

I am still having doubts about the querying techniques in ES. In v1.9 I have noticed that the Filter option differs.

I am wondering how I can replicate the below code in v5.4

searchDescriptor.Query(q => q.Filtered(m => m.Query(n => matchQuery).Filter(o => o.And(filterContainer.ToArray()))))

Here I see Filter(o => o.And(filterContainer.ToArray()) how is it possible to do an And or an Or operation with v5.4?

Does Filter(o => o.And(filterContainer.ToArray()) indicate that each item in the array are bound with an And operaton?

回答1:

.And() and .Or() were deprecated in Elasticsearch 2.0 and removed in 5.0. You can replace them with a bool query

  • for .And(), if you require scoring, then used .Must(). If you don't require scoring, use .Filter().
  • for .Or(), use .Should().

bool queries can be nested, so it's possible to create complex compound queries.

searchDescriptor.Query(q => q.Filtered(m => m.Query(n => matchQuery).Filter(o => o.And(filterContainer.ToArray()))))

becomes something like

var matchQuery = new MatchQuery { Field = "field", Query = "query" };

var filterContainer = new QueryContainer[] {
    new TermQuery { Field = "field", Value = "value" }
};

client.Search<Message>(s => s
    .Query(q => q
        .Bool(b => b
            .Must(matchQuery)
            .Filter(filterContainer)
        )
    )
);

filtered queries were deprecated in 2.0 and removed in 5.0.

Here I see Filter(o => o.And(filterContainer.ToArray()) how is it possible to do an And or an Or operation with v5.4?

Does Filter(o => o.And(filterContainer.ToArray()) indicate that each item in the array are bound with an And operaton?

For these, pass them to a bool query filter clauses, which are conjunctive i.e. and'ed. If you need to or clauses, you can nest bool queries with filter clauses within a bool query should clauses.