Mongoid and extended JSON - ruby-on-rails

I'm trying to bypass a JSON aggregation query to aggregation pipeline of MongoDB.
For doing that I have this endpoint on my RoR app
def index
Lead.collection.aggregate(JSON.parse(params[:query]))
end
And I send this JSON from my frontend
[
{
"$match": {
"statuses.created_at": {
"$gte": {
"$date": "1539369174"
}
}
}
}
]
The problem is that I'm not getting no result because "$date" filter is not working properly.
In mongo shell I'm getting results.

Related

How can I run a Graphql query in a Rails console?

I have a Graphql query working in Graphiql:
query MyConfigurationType {
myConfiguration {
number
expirationDate
}
}
Returns
{
"data": {
"myConfiguration": {
"number": 1,
"expirationDate": "2022/10/04"
}
}
}
But I need to actually use that result in my app therefore I want to be able to run it in my rails console. There doesn't seem to be much info about this.
How would one go about executing a Graphql query in the Rails console?
After looking at some documentation the best I could manage to do was, in the Rails console do:
query_string = "query MyConfigurationType {
myConfiguration {
number
expirationDate
}
}"
and the run
result = MySchema.execute(query_string)
Which has a result of
=> #<GraphQL::Query::Result #query=... #to_h={"data"=>{"myConfiguration"=>{"number"=>1, "expirationDate"=>"2022/10/04"}}}>
Therefore one can now do
[1] pry(main)> result['data']
=> {"myConfiguration"=>{"number"=1, "expirationDate"=>"2022/10/04"}}

How to fix graphql parse error on $ using rails shopify_app gem?

I'm using the shopify_app gem and trying to get multiple products using the graphql admin API. I can hardcode product Id's and get a valid response. However, when using dynamic variables I get the error below in my terminal:
GraphQL::ParseError (Parse error on "$" (VAR_SIGN) at [2, 27]):
Here is the code block
get_order_products = ShopifyAPI::GraphQL.client.parse <<-'GRAPHQL'
{
query getProducts($ids: [ID!]!) {
nodes(ids: $ids) {
... on Product {
id
title
metafields(first: 5) {
edges {
node {
namespace
key
value
}
}
}
}
}
}
}
GRAPHQL
{
"ids": ["gid://shopify/Product/abc123", "gid://shopify/Product/abc456"]
}
#result = ShopifyAPI::GraphQL.client.query(get_order_products)
I'm new to graphql but would have expected this to work based on this shopify community forum post][1]

Issue with Graphql query using graphlient

I am using graphlient gem to make a graphql query call
I have to make this call in controller so thats why I am using graphlient ta make call
client = Graphlient::Client.new("http://localhost:3000/graphql", headers: {
'Authorization': "API #{api_key}"
})
response = client.query do
query{
search(id: 1){
edges{
node{
id
name
timing{
start
end
}
}
}
}
end
I have timing field which has two attribute start and end but Rails is giving me syntax error as
end is a keyword in Rails
How can I run this query without syntax error? Is there any way to send the query in string format to run this?
I am assuming that you are using ashkan18/graphlient since this is the first result when searching for graphlient.
And according to its documentation, you can use a string for making queries. So instead of using blocks in your example, you can do this instead:
response = client.query <<~GRAPHQL
query {
search(id: 1) {
edges {
node {
id
name
timing {
start
end
}
}
}
}
}
GRAPHQL

elasticsearch filter aggregation - multiple filters

Using ElasticSearch aggregation to aggregate data on my documents.
so the aggregation request looks something like this
{
"query":{
"bool":{
"filter":[
{
"terms":{
"status":[
"active",
"deleted"
]
}
},
{
"terms":{
"address_ids":[
4078,
4080
]
}
}
]
}
},
"aggregations":{
"neighbourhoods":{
"terms":{
"field":"neighbourhoods",
"size":100
}
},
"budgets_stats":{
"filter":{
"range":{
"budget":{
"lt":100000
}
}
},
"aggregations":{
"budget":{
"stats":{
"field":"budget"
}
}
}
}
}
}
i want to filter out documents that match ** budget lower than 100000 ** and add more filters to that specific aggregation.
I cant add it to the query clause, because i have other aggregation in which i dont want these budget filters to apply.
I cant use the filters aggregation either because it creates a different bucket for each filter - instead of 1 bucket with all filters applied to it.
How can create a filter aggregation with more than 1 filter? lets say the other condition should be
"budgets_stats":{
"filter":
{
"range":{
"vip_budget":{
"gt":500
}
}
}
}
i have tried using Array but its not working.
filter: [{:range=>{:budget=>{:lt=>100000}}}, {:range=>{:vip_budget=>{:gt=>500}}}]
getting this error (using rails gems)
Elasticsearch::Transport::Transport::Errors::BadRequest Exception: [400] {"error":{"root_cause":[{"type":"parsing_exception","reason":"Expected [START_OBJECT] under [filter], but got a [START_ARRAY] in [budgets_stats]","line":1,"col":233}],"type":"parsing_exception","reason":"Expected [START_OBJECT] under [filter], but got a [START_ARRAY] in [budgets_stats]","line":1,"col":233},"status":400}
Is this even possible? couldn't find a reference in the documentation of elastic / rails gems for elastic
Will appreciate any help,
Thanks

Neo4j http API NullPointerException

I am using the HTTP API to query the Neo4j server. The exact same queries with slightly different values do not work consistently. Infact the entire system breaks because of the NullPointer exception that is being thrown. Cannot figure out the root of this problem
{
"query":"START n=node( { current_user_node } ), n1 = node( { contact_node } ) CREATE UNIQUE n-[:has_contact {device: {device_id}, name: {name} }]->(n1)",
"params":{"current_user_node":2,"contact_node":5941,"device_id":"F1485935-48F8-4624-AF5D-67529AE91227","name":"Samir Coll "}
}
The above query returns
{
"exception": "NullPointerException",
"fullname": "java.lang.NullPointerException",
"stacktrace": []
}
I tried the above query in the neo4j-shell from the command line and the query returned a null.
While
{
"query":"START n=node( { current_user_node } ), n1 = node( { contact_node } ) CREATE UNIQUE n-[:has_contact {device: {device_id}, name: {name} }]->(n1)",
"params":{"current_user_node":1,"contact_node":5658,"device_id":"FA2C589A-6AB5-4D78-ADED-7146CA71D0FC","name":"Jayesh New"}
}
The above returns
{ "columns": [], "data": [] }
The data is empty as the relationship already exists.
I am running neo4j 2.0.0 stable. All the nodes mentioned in the above queries are valid. I am very unsure as to how to proceed with this. Would appreciate it if someone can help with the problem.

Resources