Skip to content

[FEATURE] Unable to use Term Query for Enums #8009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DR9885 opened this issue Jan 10, 2024 · 1 comment
Closed

[FEATURE] Unable to use Term Query for Enums #8009

DR9885 opened this issue Jan 10, 2024 · 1 comment

Comments

@DR9885
Copy link

DR9885 commented Jan 10, 2024

Is your feature request related to a problem? Please describe.

Would like to be able to this with client, like in NEST client.

    public class TestModel
    {
        public string Id { get; set; }
        public TestEnum Enum { get; set; }
    }

    public enum TestEnum
    {
        Unknown,
        First,
        Second
    }

    var res = await _elasticsearchClient.SearchAsync<TestModel>(s => s
        .Index("test")
        .Query(q => q
            .Bool(b => b
                .Must(m => m
                    .Terms(ts => ts.Field(f => f.TestEnum).Terms(new [] { TestEnum.Unknown }))
                )
            )
        )
    ); 

Ref: (similar)
#7716

Describe the solution you'd like
filter on enums

Describe alternatives you've considered
Event this didn't work...

    var enumFieldValues = models.Select(x => x.Enum).Select(x => FieldValue.String(x.ToString())).ToArray();
    var res = await _repo.FindAllAsync<TestModel>(s => s
        .Index(_index)
        .Size(ids.Length)
        .Query(q =>
            q.Bool(b =>
                b.Filter(
                    f => f.Terms(x => x.Field(ff => ff.Enum).Terms(new TermsQueryField(enumFieldValues)))
                )
        )),
    CancellationToken.None);
@DR9885 DR9885 added the Feature label Jan 10, 2024
@DR9885
Copy link
Author

DR9885 commented Jan 10, 2024

found workaround:

    var enumFieldValues = models.Select(x => x.Enum).Select(x => FieldValue.String(x.ToString())).ToArray();
    var res = await _repo.FindAllAsync<TestModel>(s => s
        .Index(_index)
        .Size(ids.Length)
        .Query(q =>
            q.Bool(b =>
                b.Filter(
                    f => f.Terms(x => x.Field("enum.keyword").Terms(new TermsQueryField(enumFieldValues)))
                )
        )),
    CancellationToken.None);

@DR9885 DR9885 closed this as completed Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant