Firestore REST API starting query - json

I am struggling with Firestore REST API. Database have a collection with Users. Every user is separate document. I need to get all users with, for example, category equal 1. Here is my attempt (request body):
{
"structuredQuery": {
"where" : {
"fieldFilter" : {
"field": { "fieldPath" : "category" } ,
"op":"EQUAL",
"value": { "integerValue" : 1 }
}
}
}
}
and response error:
"error": {
"code": 400,
"message": "kind is required for filter: category",
"status": "INVALID_ARGUMENT"
}
I have totaly no idea, how "field" inside "fieldFilter" should looks like. Thanks in advance.

I believe if you include the "from" property of the structured query, this will work. If the collection you're querying from is called "users" it would look like this, based on what you posted above:
{
"structuredQuery": {
"where" : {
"fieldFilter" : {
"field": {"fieldPath": "category"},
"op":"EQUAL",
"value": {"integerValue": 1}
}
},
"from": [{"collectionId": "users"}]
}
}

Related

How does Elasticsearch implement the following SQL statement

I have the following SQL statement, I want to use ES7.13 implementation, please ask how I should write the code.
SELECT created_at from customer WHERE DATE_FORMAT(created_at,'%m') = '06'
Your question is about querying specific month in date. So the solution is to use script
POST /customer/_search
{
"query": {
"filter" : {
"script" : {
"script" : {
"source": "doc['created_at'].value.getMonthValue() == 6",
"lang": "painless"
}
}
}
}
}
Reference: https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-datetime.html
Es7.13 uses the following code
POST /customer/_search
{
"query": {
"bool": {
"filter" : {
"script" : {
"script" : {
"source": "doc['created_at'].value.getMonthValue() == 6",
"lang": "painless"
}
}
}
}
}
}

How to check in elasticsearch if a JSON object has a key using the DSL?

If I have two documents within an index of the following format, I just want to weed out the ones which have an empty JSON instead of my expected key.
A
{
"search": {
"gold": [1,2,3,4]
}
B
{
"search":{}
}
I should just get A json and not B json.
I've tried the exists query to search for "gold" but it just checks for non null values and returns the list.
Note: The following doesn't do what I want.
GET test/_search
{
"query": {
"bool": {
"must": [
{
"exists": { "field": "search.gold" }}
]
}
}
}
This is a simple question but I'm unable to find a way to do it even after searching through their docs.
If someone can help me do this it would be really great.
The simplified mapping of the index is :
"test": {
"mappings": {
"carts": {
"dynamic": "true",
"_all": {
"enabled": false
},
"properties": {
"line_items": {
"properties": {
"line_items_dyn_arr": {
"type": "nested",
"properties": {
"dynamic_key": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
Are you storing complete json in search field?
If this is not the case then please share the mapping of your index and sample data.
Update: Query for nested field:
{
"query": {
"nested": {
"path": "search",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "search.gold"
}
}
]
}
}
}
}
}
For nested type fields we need to specify the path and query to be executed on nested fields since nested fields are indexed as child documents.
Elastic documentation: Nested Query
UPDATE based on the mapping added in question asked:
{
"query": {
"nested": {
"path": "line_items.line_items_dyn_arr",
"query": {
"exists": {
"field": "line_items.line_items_dyn_arr"
}
}
}
}
}
Notice that we used "path": "line_items.line_items_dyn_arr". The reason we require to provide full path is because nested field line_items_dyn_arr is itself under line_items object. Had line_items_dyn_arr be a property of mapping and not the property of object or nested field the previous query would work fine.
Nishant's answer is right but for some reason I could get it working only if the path and field are the whole paths.
The following works for me.
{
"nested": {
"path": "search.gold",
"query": {
"exists": {
"field": "search.gold"
}
}
}
}

Not able to fetch the value of a key from JSON data

I am trying to fetch value if "id" from JSON response I got from a POST request.
{
"callId": "87e90efd-eefb-456a-b77e-9cce2ed6e837",
"commandId": "NONE",
"content": [
{
"scenarioId": "SCENARIO-1",
"Channel": "Channel1-1",
"data": {
"section": {
"class": {
"repository": [
{
"export": "export-1",
"modules": "module-1",
"index": "23",
"period": {
"axis": {
"new_channel": "channel-1.1"
},
"points": [
{
"id": "6a5474cf-1a24-4e28-b9c7-6b570443df9c",
"duration": "150",
"v": 1.01,
"isNegligible": false
}
]
}
}
]
}
}
}
}
]
}
I am able to display the entire response json and am also able to get the value of "callId" using below code. Getting error at last line:
Cannot read property '0' of undefined
Code snippet:
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
let responseData = JSON.stringify(body);
//Display the entire response
console.log(responseData);
//Display the callId
console.log(body['callId']);
//Getting error here
console.log(body.content[0].repository[0].points[0].id);
}
}
Any solution to get value of "id" ?
Right now you are trying to log body.content[0].repository[0].points[0].id).
If you look at it carefully the repository[0] is an object and it has no immediate child points.
So repository[0].points would evaluate to undefined and by specifying repository[0].points[0], you are trying to access property 0 of undefined as the error states.
The correct way of accessing id would be as follows :
body.content[0].data.section.class.repository[0].period.points[0].id
If you have confusion regarding parsing JSON try breaking it down by consoling body and then by expanding it in the console or in JSONeditor
PS: It is also recommended that you check at each level if the value exists before trying to access a deeper child elements, since in some cases your body may not contain one of the values, say content for example. In that case trying to access body.content[0] will cause an error. So it is recommended to do checks at each level like follows
if(body){
if(body.content){
if(body.content[0]){
/* and so on*/
}
}
}
Try with
console.log(body.content[0].data.section.class.repository[0].period.points[0].id)

ElasticSearch - Combining query match with wildcard

I'm fairly new to ElasticSearch still, but I'm currently trying to wrap my head around why I am not able to mix a wildcard query with a match as well.
Take this JSON body for example
{
"size":"10",
"from":0,
"index":"example",
"type":"logs",
"body":{
"query":{
"match":{
"account":"1234"
},
"wildcard":{
"_all":"*test*"
}
},
"sort":{
"timestamp":{
"order":"desc"
}
}
}
}
It returns with the error "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;"
(Full dump: http://pastebin.com/uJJZm8fQ)
However, if I remove either the wildcard or match key from the request body - it returns results as expected.
I've been going through the documentation and I'm not really able to find any relevant content at all.
At first I thought it was to do with the _all parameter, but even if I explicitly specify a key, the same result occurs.
Before I assume that I should be using the 'bool' operator, or something alike to mix my query types, is there any explanation for this?
The exception says that it does not understand the field "index". When querying Elasticsearch you include the index name and type in the URL. There is no wildcard search in a match query. There is a wildcard search in the query_string query.
Your query should be something like this with match:
POST /example/logs/_search
{
"size": 10,
"from": 0,
"query" : {
"match": {
"account": "1234"
}
},
"sort": {
"timestamp" : {
"order": "desc"
}
}
Or something like this with query_string:
POST /example/logs/_search
{
"size": 10,
"from": 0,
"query" : {
"query_string": {
"default_field": "account",
"query": "*1234*"
}
},
"sort": {
"timestamp" : {
"order": "desc"
}
}
EDIT: Adding an example of a wildcard query:
POST /example/logs/_search
{
"size": 10,
"from": 0,
"query" : {
"wildcard": "*test*"
},
"sort": {
"timestamp" : {
"order": "desc"
}
}

ElasticSearch not returning expected results

I am attempting to run an ElasticSearch search using the following query. Please pardon my ignorance, as I'm new to ES, and I've sorta cobbled this together by trial and error trying to follow the documentation. Basically, the only parts that are working as expected are the from, size, sort, and the match on severity. Thank you in advance for the assist!
{
"from":0,
"size":50,
"sort":{"timestamp":{"order":"desc"}},
"query":[
{
"range":{
"timestamp":{"gte":"2013-11-18T05:00:00+00:00","lte":"2013-12-02T05:00:00+00:00"}
}
},
{
"query":{
"match":{"severity":{"query":"medium","operator":"or"}}
}
},
{
"query":{
"constantScore":{
"filter":{
"query":{
"query_string":{"default_field":"_all","query":"10.1.10.22"}
}
}
}
}
}
]
}
I think you need to read more about Query DSL. Here's the correct query based on your input:
{
"query": {
"query_string": {
"default_field": "_all",
"query": "10.1.10.22"
}
},
"filter": {
"bool": {
"must": [
{
"range": {
"timestamp": {
"gte": "2013-11-18T05:00:00+00:00",
"lte": "2013-12-02T05:00:00+00:00"
}
}
},
{
"term": {
"severity": "medium"
}
}
]
}
}
}
The above query can be explained as:
- filter the data first using bool filter, "must" here can be understood as "AND". So the data will be filter by "timestamp in range..." AND "serverity=medium"
- then search the filtered data using "query_string"
That will make your searching much more faster.
In any case, your query is not formatted correctly. If you want to combine multiple queries you can use the bool query. See the docs: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html