Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch.net - Range Query

I'm trying to query an Elasticsearch index from C# via Elasticsearch.net (not NEST). Specifically, I need to get all documents with a status of "success" that have been created since a specific date. In an attempt to do this, I have:

var query = new {
  query = new {
    match = new {
      field="status",
      query="success"
    }
  },

  range = new {
    ?
  }
};

I'm not sure what to use for the range part. In fact, I'm not even sure if my syntax for the query is correct. I don't really understand how the C# syntax maps to the Query DSL in Elasticsearch. Any help is appreciated.

Thank you!

like image 402
Some User Avatar asked Dec 19 '18 19:12

Some User


People also ask

What is range query in Elasticsearch?

Range Queries in Elasticsearch Combining the greater than ( gt ) and less than ( lt ) range parameters is an effective way to search for documents that contain a certain field value within a range where you know the upper and lower bounds. In this example, we can find all cars that were made in 2016, 2017, and 2018: 1.

How do I retrieve more than 10000 results events in Elasticsearch?

By default, you cannot use from and size to page through more than 10,000 hits. This limit is a safeguard set by the index. max_result_window index setting. If you need to page through more than 10,000 hits, use the search_after parameter instead.

What is field range?

An Field Range Index accelerates queries for comparisons within a specified type. Each Field Range Index keeps track of the values appearing in a field. Field Range Indexes also allow you to use the cts:field-values family of lexicon APIs and to use the cts:field-range-query constructor in searches.


2 Answers

Something like this should do:

var query = new {
  bool = new {
    must = new {
      match = new {
        field = "status",
        query = "success"
      }
    },
    filter = new {
      range = new {
        createDate = new {
          gt = "2018-12-01T00:00:00.000Z"
        }
      }
    }
  }
};
like image 68
Val Avatar answered Sep 20 '22 09:09

Val


I don't really understand how the C# syntax maps to the Query DSL in Elasticsearch.

By looking at provided example I assume you want to use PosData.Serializable(query). In this case the query object (no matter what type it is) will be JSON serialized and posted to elasticsearch cluster without any modifications. When you create object using new {} C# syntax it is serialized by default to JSON with the same keys as properties of this object. That is, the object

new {
    query = new {
        bool = new {
            must = new {
                term = new {
                    status = "success"
                }
            },
            filter = new {
                range = new {
                    date = new { gte = "2018-12-22T00:00:00.000Z" }
                }
            }
        }
    }
}

will be serialized and passed to elasticsearch as

"query": {
    "bool": {
        "must": {
            "term": {
                  "status": "success"
            }
        },
        "filter": {
            "range": {
               "date": { "gte": "2018-12-22T00:00:00.000Z" }
            }
        }
    }
}

So, by using low level Elasticsearch client you create objects which has almost 1:1 mapping to Query DSL syntax. You can copy examples from elastic.co, replace ":" with " = new", remove quotes from property names and, basically, thats it.

like image 36
Maxim Kosov Avatar answered Sep 21 '22 09:09

Maxim Kosov