Find Geopoint with radius which are overlapping geopoint - sql

As you can see below I have
3 GeoPoint A,B,C With Some Radius
1 GeoPoint K,
I want to find all the GeoPoint with the radius overlapping K Geo
So answer should be B, C.
So how can achieve this?
Currently I am using Mongodb. But any other database is also fine.

This question is opinion-based as is the statement "any other db is fine".
But for the record, the way to do it in ES is as follows:
PUT circles
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape",
"strategy": "recursive"
}
}
}
}
PUT circles/_doc/A
{
"location": {
"type": "circle",
"coordinates": [
16.34817123413086,
48.20968893477074
],
"radius": "2km"
}
}
PUT circles/_doc/B
{
"location": {
"type": "circle",
"coordinates": [
16.374435424804688,
48.20122291334052
],
"radius": "3km"
}
}
PUT circles/_doc/C
{
"location": {
"type": "circle",
"coordinates": [
16.386451721191406,
48.21586595914765
],
"radius": "4km"
}
}
GET circles/_search
{
"query": {
"geo_shape": {
"location": {
"shape": {
"type": "point",
"coordinates": [
16.386795043945312,
48.208773756674425
]
},
"relation": "intersects"
}
}
}
}
yielding
[
{
"_index":"circles",
"_type":"_doc",
"_id":"B",
"_score":1.0,
"_source":{
}
},
{
"_index":"circles",
"_type":"_doc",
"_id":"C",
"_score":1.0,
"_source":{
}
}
]

Related

MongoDB get fieldvalue based on fieldname

I'm new to MongoDB, and I'm not sure if what I'm trying to do is possible at all. I've exhausted my Google skills, so I'm hoping someone here can give me a push in the right direction.
My data is structured like this:
db={
"people": [
{
"id": 1,
"name": "Pete",
"occupation": "baker"
},
{
"id": 2,
"name": "Mike",
"occupation": "painter"
}
],
"activity": [
{
"baker": "bakes",
"painter": "paints"
}
]
}
Although this is just sample data, my "activity" is a single large document with unique keys that I'm trying to get the value from, based on key name (when it matches value from the "people" document).
What I'm trying to achieve is this output:
[
{
"id": 1,
"name": "Pete",
"occupation": "baker”,
“activity:” “bakes”
},
{
"id": 2,
"name": "Mike",
"occupation": "painter”,
“activity”: “paints”
}
]
Is this possible at all?
If I understand your database and data model, it seems like a poor model.
If the data model was different (see below), the query would be quite simple. Given that, here's one way you could generate your desired output with your model.
db.people.aggregate([
{ "$lookup": {
"from": "activity",
"let": { occ: "$occupation" },
"pipeline": [
{
"$replaceWith": {
"$arrayToObject": {
"$filter": {
"input": { "$objectToArray": "$$ROOT" },
"as": "kv",
"cond": { "$eq": [ "$$kv.k", "$$occ" ] }
}
}
}
}
],
"as": "activity"
}
},
{ "$set": {
"activity": {
"$getField": {
"field": "v",
"input": {
"$first": {
"$objectToArray": { "$first": "$activity" }
}
}
}
}
}
},
{ "$unset": "_id" }
])
Try it on mongoplayground.net.
If your activity collection has this model...
[
{
"occupation": "baker",
"activity": "bakes"
},
{
"occupation": "painter",
"activity": "paints"
}
]
... then the query could be:
db.people.aggregate([
{ "$lookup": {
"from": "activity",
"localField": "occupation",
"foreignField": "occupation",
"as": "activity"
}
},
{ "$set": { "activity": { "$first": "$activity.activity" } } },
{ "$unset": "_id" }
])
Try it on mongoplayground.net.

Querying an Array in Graphql

I have the following query on graphql
query articles {
articles {
news_link
article_details {
description
title
brands {
name
}
featured_image {
url
}
published_at
}
}
}
Which will give me the following sample response
{
"data": {
"articles": [
{
"news_link": "http://loanstreet.com.my",
"article_details": [
{
"description": "My first article",
"title": "First Article",
"brands": [
{
"name": "Fendi"
}
],
"featured_image": {
"url": "/uploads/e5c00dbd3a3648dd8c0e1c2a44f50c0d.png"
},
"published_at": "2020-03-31T04:00:00.000Z"
},
{
"description": "Lighter ff article",
"title": "First Article LP",
"brands": [
{
"name": "Lighter"
}
],
"featured_image": {
"url": "/uploads/ac6c5f26050f46eebd37fa7319ab531b.png"
},
"published_at": "2020-03-31T04:00:00.000Z"
}
]
}
]
}
}
I am trying to query and only get the response where brand name is "Fendi" but cannot seem to get it right using Graphql playground.
I tried this
query articles {
articles {
news_link
article_details {
description
title
brands(where: {name: "Fendi"}) {
name
}
featured_image {
url
}
published_at
}
}
}
but it gave me this response which is not really what i want
{
"data": {
"articles": [
{
"news_link": "http://loanstreet.com.my",
"article_details": [
{
"description": "My first article",
"title": "First Article",
"brands": [
{
"name": "Fendi"
}
],
"featured_image": {
"url": "/uploads/e5c00dbd3a3648dd8c0e1c2a44f50c0d.png"
},
"published_at": "2020-03-31T04:00:00.000Z"
},
{
"description": "Lighter ff article",
"title": "First Article LP",
"brands": [],
"featured_image": {
"url": "/uploads/ac6c5f26050f46eebd37fa7319ab531b.png"
},
"published_at": "2020-03-31T04:00:00.000Z"
}
]
}
]
}
}
I am expecting a response like this
{
"data": {
"articles": [
{
"news_link": "http://loanstreet.com.my",
"article_details": [
{
"description": "My first article",
"title": "First Article",
"brands": [
{
"name": "Fendi"
}
],
"featured_image": {
"url": "/uploads/e5c00dbd3a3648dd8c0e1c2a44f50c0d.png"
},
"published_at": "2020-03-31T04:00:00.000Z"
}
]
}
]
}
}
Any help is appreciated. Thanks

How to specify Rule Line with a single value in VegaLite?

It's possible to specify Rule as encoding over the data. But sometimes it's too verbose. Is there a shorter way to specify it with just a single number?
In example below I want to draw a horizontal line with y=1 and it requires me to specify a calculate transform. Wonder if it's possible with something more compact, like:
"layer": [
...,
{ "mark": { "type": "rule", "y": 1 }, // Specify ruler with just a single line
...
]
Playground
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"transform":[
{ "calculate": "1", "as": "one" }
],
"layer": [
{
"mark": { "type": "bar" },
"encoding": {
"x": { "field": "diff", "type": "quantitative" },
"y": { "field": "diff", "type": "quantitative" }
}
},
{
"mark": { "type": "rule" },
"encoding": {
"y": { "field": "one", "type": "quantitative" }
}
}
],
"data": {
"values": [
{ "diff": 1 },
{ "diff": 2 },
{ "diff": 3 }
]
}
}
You can specify a single value using the "value" field in the encoding; e.g.
{
"mark": { "type": "rule" },
"encoding": {
"y": { "value": 133 }
}
}
(Playground)
But be aware this value is a value in the axis domain, rather than in the data domain; i.e. it represents pixels from the top-left of the chart.
If you want to specify a value in the data domain, there is no such shorthand. You have to either use a transform (as you did in your question) or define a new dataset for your layer. The most compact way of doing this, I think, is something like this:
{
"mark": { "type": "rule" },
"data": {"values": {"y": 1}},
"encoding": {
"y": { "field": "y" }
}
}

Create a new Google Sheet with row or column groups

I'm trying to create a new spreadsheet using spreadsheets#create, with specified row groups.
In the API Explorer, I am entering in the JSON below. which corresponds to the following appearance:
No errors are flagged or returned when I execute the call, but when the sheet is created, the specified grouping is not created - only the values are set.
{ "properties": {
"title": "Test Spreadsheet",
"locale": "en"
},
"sheets": [
{ "properties": {"title": "Test1"},
"data": [
{
"startRow": 0,
"startColumn": 0,
"rowData": [
{ "values": [
{ "userEnteredValue": { "stringValue": "Top1" } }
]
},
{ "values": [
{ "userEnteredValue": { "stringValue": "Top2" } }
]
},
{ "values": [
{ "userEnteredValue": { "stringValue": "" } },
{ "userEnteredValue": { "stringValue": "Top2A" } }
]
},
{ "values": [
{ "userEnteredValue": { "stringValue": "" } },
{ "userEnteredValue": { "stringValue": "Top2B" } }
]
},
{ "values": [
{ "userEnteredValue": { "stringValue": "" } },
{ "userEnteredValue": { "stringValue": "Top2C" } }
]
},
{ "values": [
{ "userEnteredValue": { "stringValue": "Top3" } }
]
}
]
}
],
"rowGroups": [
{ "range": {
"dimension": "ROWS",
"startIndex": 2,
"endIndex": 5
}
}
]
}
]
}
Even when I create the rowGroups JSON directly on the page, with its structured editor to make sure it is properly defined, the created spreadsheet still doesn't group the specified rows. I have triple-checked all my objects from the top down, and can't see what I am doing wrong.

ElasticSearch combine match and function score queries

I have a complicated query to run in elasticsearch that spans across multiple fields (nested and non-nested). I am using a bool should query across a multi-field match and nested field match.
Additionally I want a composite scoring which takes into account several other parameters such as location, rating etc.
I tried to run a simplified proof of concept combined query which looks for a matching term and tries to use function score for the other fields but I am running into an error from es.
GET init/restaurant/_search/
{
"query": {
"match": {
"cuisine_categories": "Oriental"
},
"function_score": {
"functions": [
{
"gauss": {
"coordinates": {
"origin": { "lat": 74.20, "lon": 31.23 },
"offset": "1km",
"scale": "3km"
}
}
},
{
"gauss": {
"nomnom_rating": {
"origin": "4.5",
"offset": "0.5",
"scale": "1"
}
},
"weight": 2
},
{
"gauss": {
"estimated_preparation_time": {
"origin": "30",
"offset": "10",
"scale": "20"
}
},
"weight": 5
}
]
}
}
}
The query is not a valid. The match clause should be within the query object of function score as shown below
Example:
POST init/restaurant/_search/
{
"query": {
"function_score": {
"functions": [
{
"gauss": {
"coordinates": {
"origin": {
"lat": 74.2,
"lon": 31.23
},
"offset": "1km",
"scale": "3km"
}
}
},
{
"gauss": {
"nomnom_rating": {
"origin": "4.5",
"offset": "0.5",
"scale": "1"
}
},
"weight": 2
},
{
"gauss": {
"estimated_preparation_time": {
"origin": "30",
"offset": "10",
"scale": "20"
}
},
"weight": 5
}
],
"query": {
"match": {
"cuisine_categories": "Oriental"
}
}
}
}
}