Giter VIP home page Giter VIP logo

Comments (5)

jyrijh avatar jyrijh commented on June 23, 2024 1

When I build the query I don't the .Index("indexname") as I wan't to use the DefaultIndex that is set when I create the client

string defaultIndex = "articles_query_tests.";

var settings = new ElasticsearchClientSettings(uri: new Uri(uri))
    .DefaultIndex($"{defaultIndex}*")
private SearchRequestDescriptor<Article> CreateSeachRequestDescriptor(int page, int size, int offset, Metadata.Models.Query.Search query)
{
    return CreateQuery(query)
        .TrackTotalHits(track)
        .From(page * offset)
        .Size(size)
        .Sort(CreateSortOptions(query));
}

from elasticsearch-net.

flobernd avatar flobernd commented on June 23, 2024 1

Thanks @jyrijh, this actually helped me to spot the bug in the generator. I'll fix this in the next patch release.

from elasticsearch-net.

flobernd avatar flobernd commented on June 23, 2024

Hi @jyrijh, could you please share your CreateQuery method?

from elasticsearch-net.

jyrijh avatar jyrijh commented on June 23, 2024

Sure, but it wont tell much

private SearchRequestDescriptor<Article> CreateQuery(Metadata.Models.Query.Search searchQuery)
{
    if (searchQuery.Filters.OfType<Metadata.Models.Query.Filter.IddocListFilter>().Any())
    {
        return CreateIdsQuery(searchQuery);
    }

    BoolQuery boolQuery = new()
    {
        Must = CreateMustQueries(searchQuery),
        Filter = CreateFilters(searchQuery),
        MustNot = CreateNotFilters(searchQuery)
    };

    SearchRequestDescriptor<Article> search = new();
    return search.Query(boolQuery);
}

from elasticsearch-net.

jyrijh avatar jyrijh commented on June 23, 2024

and CreateMustQueries

private List<Query> CreateMustQueries(Metadata.Models.Query.Search searchQuery)
{
    List<Query> mustQueries = [];

    foreach (var filter in searchQuery.OrFilters.OfType<IHasValueOrDoesNotExistsFilter>())
    {
        if(filter is DateExistsFilter dateExistsFilter)
        {
            mustQueries.Add(new BoolQuery()
            {
                Should =
                [
                    CreateDateComparisonFilter(dateExistsFilter),
                    new BoolQuery()
                    {
                        MustNot = [new ExistsQuery() { Field = dateExistsFilter.FieldName }]
                    }
                ]
            });
        }

        if(filter is ServiceTypeFilter IHasTermValueOrDoesNotExistsFilter)
        {
            mustQueries.Add(new BoolQuery()
            {
                Should =
                [
                    new TermQuery(((BaseFilter)filter).FieldName) { Value = ((IHasTermValueOrDoesNotExistsFilter)filter).Value },
                    new BoolQuery()
                    {
                        MustNot = [new ExistsQuery() { Field = ((BaseFilter)filter).FieldName }]
                    }
                ]
            });
        }
    }

    foreach (var filter in searchQuery.Filters.OfType<IMustExistsFilter>())
    {
        mustQueries.Add(new ExistsQuery() { Field = ((BaseFilter)filter).FieldName });
    }

    return mustQueries.Count != 0 ? mustQueries: null!;

    Query CreateDateComparisonFilter(DateExistsFilter dateExistsFilter) => dateExistsFilter.Comparison switch
    {
        DateExistsFilter.ComparisonType.Gt => new DateRangeQuery(dateExistsFilter.FieldName) { Gt = _timeProvider.GetUtcNow().DateTime },
        DateExistsFilter.ComparisonType.Lt => new DateRangeQuery(dateExistsFilter.FieldName) { Lt = _timeProvider.GetUtcNow().DateTime },
        DateExistsFilter.ComparisonType.Gte => new DateRangeQuery(dateExistsFilter.FieldName) { Gte = _timeProvider.GetUtcNow().DateTime },
        DateExistsFilter.ComparisonType.Lte => new DateRangeQuery(dateExistsFilter.FieldName) { Lte = _timeProvider.GetUtcNow().DateTime },
        _ => throw new NotImplementedException()
    };
}

from elasticsearch-net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.