Range search in elastic search

How do i use the range search in elastic?

I tried to use the code from price and news, but can’t get it to work.

I have tried alot of different ways, but i never get any result back.
Tried to have the field Nested, and not Nested. I tried it on the regular Tags, and i have saved all numeric values in a seprate property and tried that. I have tried with and without keyword on key and value.


        [Nested]
        public List<NumericTagItem> NumericTags { get; set; } = new List<NumericTagItem>();
        public class NumericTagItem
        {
            [Keyword]
            public string Key { get; set; }
            [Keyword(EagerGlobalOrdinals = true)]
            public double Value { get; set; }
        }

These are the queries i have tried:

allQueries.Add(qc.Bool(b =>
                                b.Filter(bf =>
                                    bf.Range(r =>
                                        r.Field(x => x.NumericTags.Find(t => t.Key == tag.Key).Value)
                                            .GreaterThanOrEquals(minValue)
                                            .LessThanOrEquals(maxValue)))));

                            allQueries.Add(qc.Range(b =>
                                        b.Field(x => x.NumericTags.Find(t => t.Key == tag.Key).Value)
                                            .GreaterThanOrEquals(minValue)
                                            .LessThanOrEquals(maxValue)));

                            allQueries.Add(qc.Nested(n => n
                                .Path(x => x.NumericTags)
                                .Query(nq
                                    => nq.Range(t => t.Field(f => f.NumericTags.Find(tt => tt.Key == tag.Key).Value)
                                        .GreaterThanOrEquals(minValue)
                                        .LessThanOrEquals(maxValue)))));


                            allQueries.Add(qc.Bool(b => b.Filter(bf => bf.Bool(bb => bb.Should(q => q.Nested(n => n
                                    .Path(x => x.NumericTags)
                                    .Query(nq => nq.Range(t => t.Field(f => f.NumericTags.FirstOrDefault(tt => tt.Key == tag.Key).Value)
                                               .GreaterThanOrEquals(minValue)
                                               .LessThanOrEquals(maxValue))
                                    )))))));

Litium version: 8.2

If i change

t.Field(f => f.NumericTags.FirstOrDefault(tt => tt.Key == tag.Key).Value)

to

t.Field(f => f.NumericTags[0].Value)

It seems to work… But is that correct? It feels like now it will only look on the first value in the List of NumericTags?

This seems to work. Any thoughts?

allQueries.Add(qc.Bool(b => b.Filter(bf => bf.Bool(bb => bb.Should(q => q.Nested(n => n
                                    .Path(x => x.NumericTags)
                                    .Query(nq => nq.Term(t => t.Field(f => f.NumericTags[0].Key).Value(tag.Key)) 
                                                 && nq.Range(t => t.Field(f => f.NumericTags[0].Value)
                                               .GreaterThanOrEquals(minValue)
                                               .LessThanOrEquals(maxValue)))))))));

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.