12

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!

1
  • have you tried messaging the github maintainer? Commented Dec 21, 2018 at 20:37

3 Answers 3

3
+100

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"
        }
      }
    }
  }
};
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

The query would be like:

var query = new {
  bool = new {
    must = new {
      match = new {
        field = "status",
        query = "success"
      }
    },
    filter = new {
      range = new {
        createDate = new {
          gte = "2018-01-01T00:00:00.000Z",
          lt = "2019-01-01T00:00:00.000Z"
        }
      }
    }
  }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.