Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

Elasticsearch conditional query with count and multiple conditions using NEST

I'm struggling with an elasticsearch nest query. I would like to display a list of WebPage documents. WebPage has a path, version, and isactive flag. There can be multiple WebPage documents with the same path, but only 0 or 1 documents for a path can have isactive = true. The result should be a list of all distinct paths and should use the document with isactive = true, but if there are no documents with isactive = true, then it should use the document with the highest version.

Here is my starting point with some other fields I am searching, but I am assuming this is all wrong and I will need to change this to add aggregations:

    var filter = new List<Func<QueryContainerDescriptor<WebPage>, QueryContainer>>();
    if (path != null)
    {
        filter.Add(fq => fq.Terms(t => t
            .Field(f => f.path.Suffix("keyword")).Terms(path)
        ));
    }

    var should = new List<Func<QueryContainerDescriptor<WebPage>, QueryContainer>>();
    if (search != null && search != string.Empty)
    {
        should.Add(fq => fq.Wildcard(c => c
            .Field(f => f.pageTitle).Value(search.ToLower() + "*")
        ));
    }

    var response = await _elasticClient.SearchAsync<WebPage>(x => x
        .Query(q => q
            .Bool(bq => bq
                .Filter(filter)
                .Should(should)
            )
        )
    );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...