Related
I would like to use Google Maps API to find all cities contains entered name.
I tried to use:
https://maps.googleapis.com/maps/api/place/autocomplete/json?&input=Poznań&types=(cities)&language=pl&key=[MyKey]
This request returned 5 cities:
"Poznań, Polska"
"Poznanovec, Chorwacja",
"Poznań, Obwód rówieński, Ukraina"
"Poznanovci, Słowenia"
"Sady, Polska"
Two problems:
Query don't return one of city Poznań in Poland (https://goo.gl/maps/B3LjyRdVn7S3DEuu7)
I don't know why query return: "Sady, Polska"
Could you tell me how force API to return all cities contains some name?
Full query return:
{
"predictions" : [
{
"description" : "Poznań, Polska",
"matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"place_id" : "ChIJtwrh7NJEBEcR0b80A5gx6qQ",
"reference" : "ChIJtwrh7NJEBEcR0b80A5gx6qQ",
"structured_formatting" : {
"main_text" : "Poznań",
"main_text_matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"secondary_text" : "Polska"
},
"terms" : [
{
"offset" : 0,
"value" : "Poznań"
},
{
"offset" : 8,
"value" : "Polska"
}
],
"types" : [ "locality", "political", "geocode" ]
},
{
"description" : "Poznanovec, Chorwacja",
"matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"place_id" : "ChIJMduuD-nmZUcRKLHqfF4Ty0w",
"reference" : "ChIJMduuD-nmZUcRKLHqfF4Ty0w",
"structured_formatting" : {
"main_text" : "Poznanovec",
"main_text_matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"secondary_text" : "Chorwacja"
},
"terms" : [
{
"offset" : 0,
"value" : "Poznanovec"
},
{
"offset" : 12,
"value" : "Chorwacja"
}
],
"types" : [ "locality", "political", "geocode" ]
},
{
"description" : "Poznań, Obwód rówieński, Ukraina",
"matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"place_id" : "ChIJM8yYp2JZKEcRROf2JFonEiA",
"reference" : "ChIJM8yYp2JZKEcRROf2JFonEiA",
"structured_formatting" : {
"main_text" : "Poznań",
"main_text_matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"secondary_text" : "Obwód rówieński, Ukraina"
},
"terms" : [
{
"offset" : 0,
"value" : "Poznań"
},
{
"offset" : 8,
"value" : "Obwód rówieński"
},
{
"offset" : 25,
"value" : "Ukraina"
}
],
"types" : [ "locality", "political", "geocode" ]
},
{
"description" : "Poznanovci, Słowenia",
"matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"place_id" : "ChIJs5Z1s_IXb0cRGcz67oYV9SA",
"reference" : "ChIJs5Z1s_IXb0cRGcz67oYV9SA",
"structured_formatting" : {
"main_text" : "Poznanovci",
"main_text_matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"secondary_text" : "Słowenia"
},
"terms" : [
{
"offset" : 0,
"value" : "Poznanovci"
},
{
"offset" : 12,
"value" : "Słowenia"
}
],
"types" : [ "locality", "political", "geocode" ]
},
{
"description" : "Sady, Polska",
"matched_substrings" : [
{
"length" : 4,
"offset" : 0
}
],
"place_id" : "ChIJ-TKnSCVHBEcRGR5C_UOhHhY",
"reference" : "ChIJ-TKnSCVHBEcRGR5C_UOhHhY",
"structured_formatting" : {
"main_text" : "Sady",
"main_text_matched_substrings" : [
{
"length" : 4,
"offset" : 0
}
],
"secondary_text" : "Polska"
},
"terms" : [
{
"offset" : 0,
"value" : "Sady"
},
{
"offset" : 6,
"value" : "Polska"
}
],
"types" : [ "locality", "political", "geocode" ]
}
],
"status" : "OK"
}
You can add components parameter to guarantee that the results of the Places Autocomplete API will be restricted to a certain country.
Here's a working example:
https://maps.googleapis.com/maps/api/place/autocomplete/json?&input=Poznań&types=(cities)&components=country:PL&key=YOUR_API_KEY
There,Poznań, Poland is in the result, and no Sady, Polska.
I am trying to filter based on the cluster field and whether the podName field has a value.
Then I want to filter out some fields with specific values but I get the values for other cluster fields than the one specified.
So the following query will also return values for cluster2 and cluster3.
I can't figure out what the correct syntax is.
{
"size":50,
"query":{
"bool":{
"must":[
{
"range":{
"timestamp":{
"gte":"now-1h"
}
}
},
{
"query_string":{
"query":"(podstatus.podName:* AND cluster:cluster1) AND NOT podstatus.containerStatus:true AND NOT podstatus.phase:Running AND NOT podstatus.phase:Succeeded AND NOT podstatus.started: true"
}
}
]
}
}
}
Sample document
{
"timestamp": "2020-07-09T17:30:04",
"cluster": "cluster1",
"namespace": "kube-system",
"podstatus.podName": "cronjob-kubernetes-resource-monitor-1594233600-4frbc",
"podstatus.containerStatus": "false",
"podstatus.restartCount": 0,
"podstatus.started": "false",
"podstatus.phase": "Succeeded"
}
Mapping
{
"cluster-resources-cluster1-2020.07.08-000001" : {
"mappings" : {
"properties" : {
"allocated" : {
"properties" : {
"pods-percent" : {
"type" : "float"
}
}
},
"capacity" : {
"properties" : {
"cpu" : {
"type" : "long"
},
"mem" : {
"type" : "long"
},
"pods" : {
"type" : "long"
}
}
},
"cluster" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"depstatus" : {
"properties" : {
"availableReplicas" : {
"type" : "long"
},
"deploymentName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"readyReplicas" : {
"type" : "long"
},
"replicas" : {
"type" : "long"
},
"unavailableReplicas" : {
"type" : "long"
},
"updatedReplicas" : {
"type" : "long"
}
}
},
"namespace" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"podstatus" : {
"properties" : {
"containerStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"phase" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"podName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"restartCount" : {
"type" : "long"
},
"started" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"requests" : {
"properties" : {
"cpu" : {
"type" : "long"
},
"cpu-percent" : {
"type" : "float"
},
"mem" : {
"type" : "long"
},
"mem-percent" : {
"type" : "float"
},
"pods" : {
"type" : "long"
}
}
},
"timestamp" : {
"type" : "date"
}
}
}
}
}
Your query seems to be working correctly. However I'm posting the below steps and you let me know if you can find any observations in similar manner.
I've taken the mapping, created sample documents, the query you've shared and response I get.
Mapping:
PUT cluster_index_001
{
"mappings" : {
"properties" : {
"allocated" : {
"properties" : {
"pods-percent" : {
"type" : "float"
}
}
},
"capacity" : {
"properties" : {
"cpu" : {
"type" : "long"
},
"mem" : {
"type" : "long"
},
"pods" : {
"type" : "long"
}
}
},
"cluster" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"depstatus" : {
"properties" : {
"availableReplicas" : {
"type" : "long"
},
"deploymentName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"readyReplicas" : {
"type" : "long"
},
"replicas" : {
"type" : "long"
},
"unavailableReplicas" : {
"type" : "long"
},
"updatedReplicas" : {
"type" : "long"
}
}
},
"namespace" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"podstatus" : {
"properties" : {
"containerStatus" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"phase" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"podName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"restartCount" : {
"type" : "long"
},
"started" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"requests" : {
"properties" : {
"cpu" : {
"type" : "long"
},
"cpu-percent" : {
"type" : "float"
},
"mem" : {
"type" : "long"
},
"mem-percent" : {
"type" : "float"
},
"pods" : {
"type" : "long"
}
}
},
"timestamp" : {
"type" : "date"
}
}
}
}
Sample Documents:
POST cluster_index_001/_doc/1
{
"timestamp": "2020-07-09T17:30:04",
"cluster": "cluster1",
"namespace": "kube-system",
"podstatus.podName": "cronjob-kubernetes-resource-monitor-1594233600-4frbc",
"podstatus.containerStatus": "false",
"podstatus.restartCount": 0,
"podstatus.started": "false",
"podstatus.phase": "Failed"
}
POST cluster_index_001/_doc/2
{
"timestamp": "2020-07-10T17:30:04",
"cluster": "cluster1",
"namespace": "kube-system",
"podstatus.podName": "cronjob-kubernetes-resource-monitor-1594233600-4frbc",
"podstatus.containerStatus": "false",
"podstatus.restartCount": 0,
"podstatus.started": "false",
"podstatus.phase": "Failed"
}
POST cluster_index_001/_doc/3
{
"timestamp": "2020-07-10T17:30:04",
"cluster": "cluster2",
"namespace": "kube-system",
"podstatus.podName": "cronjob-kubernetes-resource-monitor-1594233600-4frbc",
"podstatus.containerStatus": "false",
"podstatus.restartCount": 0,
"podstatus.started": "false",
"podstatus.phase": "Failed"
}
Sample Query:
POST cluster_index_001/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"timestamp": {
"gte": "now-2d"
}
}
},
{
"query_string": {
"query":"(podstatus.podName:* AND cluster:cluster1) AND NOT podstatus.containerStatus:true AND NOT podstatus.phase:Running AND NOT podstatus.phase:Succeeded AND NOT podstatus.started:true"
}
}
]
}
}
}
You could also make use of cluster.keyword in the above like this cluster.keyword:cluster1 for exact matches.
Response:
{
"took" : 86,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.4700036,
"hits" : [
{
"_index" : "cluster_index_001",
"_type" : "_doc",
"_id" : "1",
"_score" : 2.4700036,
"_source" : {
"timestamp" : "2020-07-09T17:30:04",
"cluster" : "cluster1",
"namespace" : "kube-system",
"podstatus.podName" : "cronjob-kubernetes-resource-monitor-1594233600-4frbc",
"podstatus.containerStatus" : "false",
"podstatus.restartCount" : 0,
"podstatus.started" : "false",
"podstatus.phase" : "Failed"
}
},
{
"_index" : "cluster_index_001",
"_type" : "_doc",
"_id" : "2",
"_score" : 2.4700036,
"_source" : {
"timestamp" : "2020-07-10T17:30:04",
"cluster" : "cluster1",
"namespace" : "kube-system",
"podstatus.podName" : "cronjob-kubernetes-resource-monitor-1594233600-4frbc",
"podstatus.containerStatus" : "false",
"podstatus.restartCount" : 0,
"podstatus.started" : "false",
"podstatus.phase" : "Failed"
}
}
]
}
}
Note that query works correctly and returns correct set of documents
Additional Debugging and Further Info:
This steps would help you verify and let you know why the document that was not supposed to be returned is returning.
For e.g the 3rd Document in the sample was not showing up in the response for me, and the way to figure out that is to make use of Explain API.
GET cluster_index_001/_explain/3 <----- Note this
{
"query": {
"bool": {
"must": [
{
"range": {
"timestamp": {
"gte": "now-2d"
}
}
},
{
"query_string": {
"query":"podstatus.podName:* AND cluster:cluster1 AND NOT podstatus.containerStatus:true AND NOT podstatus.phase:Running AND NOT podstatus.started: true"
}
}
]
}
}
}
The response for which I see is the below:
{
"_index" : "cluster_index_001",
"_type" : "_doc",
"_id" : "3",
"matched" : false,
"explanation" : {
"value" : 0.0,
"description" : "Failure to meet condition(s) of required/prohibited clause(s)",
"details" : [
{
"value" : 1.0,
"description" : "ConstantScore(DocValuesFieldExistsQuery [field=timestamp])",
"details" : [ ]
},
{
"value" : 0.0,
"description" : "no match on required clause (+ConstantScore(NormsFieldExistsQuery [field=podstatus.podName]) +cluster:cluster1 -podstatus.containerStatus:true -podstatus.phase:running -podstatus.started:true)",
"details" : [
{
"value" : 0.0,
"description" : "Failure to meet condition(s) of required/prohibited clause(s)",
"details" : [
{
"value" : 1.0,
"description" : "ConstantScore(NormsFieldExistsQuery [field=podstatus.podName])",
"details" : [ ]
},
{
"value" : 0.0,
"description" : "no match on required clause (cluster:cluster1)",
"details" : [
{
"value" : 0.0,
"description" : "no matching term",
"details" : [ ]
}
]
}
]
}
]
}
]
}
}
Note how the description in the above response clearly states this:
"description" : "Failure to meet condition(s) of required/prohibited clause(s)"
Also note the below:
"description" : "no match on required clause (+ConstantScore(NormsFieldExistsQuery [field=podstatus.podName]) +cluster:cluster1 -podstatus.containerStatus:true -podstatus.phase:running -podstatus.started:true)",
As a result, you know now why document 3 is not returning in the response.
Further if you still are not able to figure out the issue, make sure of the below points:
Make sure you are not using any alias and that you focus on single index at a time. Narrow down the index which can be causing this issue if you are using alias.
Also make sure that your that document in question does not have multiple values for e.g. "cluster": "cluster2, cluster1"
If the above two points are clear, go to your browser and type http://<your_host_name>:<port>/cluster-resources-cluster1-2020.07.08-000001/_settings and observe if there are any custom analyzers that has been implemented for e.g. Edge Ngrams or Ngrams and if your standard analyzer has been over-ridden.
Execute this http://<your_host_name>:<port>/cluster-resources-cluster1-2020.07.08-000001/_stats?pretty and notice if you find anything peculiar.
One thing at a time, please do share your observations and we can see what is the issue.
i'm developing an app that use google API to obtain a transport solution to reach a location at a desired time.
My code that call google API work perfectly with "driving" and "walking" travel mode, but with "transit" and "bicycling" the response is always marked with the status "ZERO_RESULTS" and no routes is calculated.
This, by the google documentation, means that transit and bicycling are not available in the country where the solution must be calculated.
I tried with several country and I always had the same result, I tried also with the Google example (for transit) in the overview and it doesn't work.
The parameters that I pass with the HTTPS request are: origin, destination, travel mode, arrival_time.
The first thing that I tried is to change arrival_time, in case the value is referred to the past or to a too distant future, but it doesn't work.
In case I don't pass "arrival_time" the response is perfect, but I don't understand because in the Google Documentation is clear that arrival_time is a possible argument of the https request also with the transit travel mode. Without this argument the request is useless for my goal.
The curious thing is that if i pass "transit" (with arrival or departure time) the response is this:
{
"available_travel_modes" : [ "DRIVING", "WALKING" ],
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJu46S-ZZhLxMROG5lkwZ3D7k",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ53USP0nBhkcRjQ50xhPN_zw",
"types" : [ "locality", "political" ]
}
],
"routes" : [],
"status" : "ZERO_RESULTS"
}
And if I pass "bicycling" (with all the other parameters unchanged):
{
"available_travel_modes" : [ "DRIVING", "WALKING", "TRANSIT" ],
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJu46S-ZZhLxMROG5lkwZ3D7k",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ53USP0nBhkcRjQ50xhPN_zw",
"types" : [ "locality", "political" ]
}
],
"routes" : [],
"status" : "ZERO_RESULTS"
}
So in the second case the API said that TRANSIT is available and this doesn't make sense at all!
The format of the location that I use is geographical coordinates to avoid cases of ambiguity.
Hope that I give enough information to make you able to help me.
EDIT: sorry, i was thinking that my post was clear enough.
I use Google Directions API: https://developers.google.com/maps/documentation/directions/
My problem is with "transit" and "bicycling" as travel mode and is not a code problem, but the problem appears at the level of https response, even with the google example for "transit" that can be found on the overview i obtain a "ZERO_RESULTS" response.
This is the URL of the example:
https://maps.googleapis.com/maps/api/directions/json?origin=75+9th+Ave+New+York,+NY&destination=MetLife+Stadium+1+MetLife+Stadium+Dr+East+Rutherford,+NJ+07073&mode=transit&arrival_time=1391374800&key=YOUR_API_KEY
And here is the two URL that get me the two response that I copied above:
with transit:
https://maps.googleapis.com/maps/api/directions/json?origin=Rome&destination=Milan&mode=transit&arrival_time=1513162800000&key=++API_KEY++
and with bicycling:
https://maps.googleapis.com/maps/api/directions/json?origin=Rome&destination=Milan&mode=transit&arrival_time=1513162800000&key=++API_KEY++
in the code I use for origin and destination geographical coordinates as here:
https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835,12.496365500000024&destination=45.4642035,9.189981999999986&mode=transit&arrival_time=1513162800000&key=++API_KEY++
the only difference is that google do not recognize geocoded waypoints:
{
"available_travel_modes" : [ "DRIVING", "WALKING" ],
"geocoded_waypoints" : [ {}, {} ],
"routes" : [],
"status" : "ZERO_RESULTS"
}
If I use "driving" as travel mode this worked:
https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835,12.496365500000024&destination=45.4642035,9.189981999999986&mode=driving&arrival_time=1513162800000&key=++API_KEY++
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "EjJQaWF6emEgZGVsbGEgUmVwdWJibGljYSwgMTAsIDAwMTg1IFJvbWEgUk0sIEl0YWxpYQ",
"types" : [ "street_address" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJC5u9LazGhkcRXAZQFNDTpKc",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 45.4731019,
"lng" : 12.6177051
},
"southwest" : {
"lat" : 41.9028523,
"lng" : 9.1839517
}
},
"copyrights" : "Dati mappa ©2017 Google",
"legs" : [
{
"distance" : {
"text" : "573 km",
"value" : 572600
},
"duration" : {
"text" : "5 ore 51 min",
"value" : 21069
},
"end_address" : "Via Silvio Pellico, 2, 20121 Milano MI, Italia",
"end_location" : {
"lat" : 45.46495119999999,
"lng" : 9.1892874
},
"start_address" : "Piazza della Repubblica, 10, 00185 Roma RM, Italia",
"start_location" : {
"lat" : 41.9028523,
"lng" : 12.4964704
},
"steps" : [
{
"distance" : {
"text" : "93 m",
"value" : 93
},
"duration" : {
"text" : "1 min",
"value" : 19
},
"end_location" : {
"lat" : 41.9035781,
"lng" : 12.4959908
},
"html_instructions" : "Procedi in direzione \u003cb\u003enord\u003c/b\u003e da \u003cb\u003ePiazza della Repubblica\u003c/b\u003e verso \u003cb\u003eVia Giuseppe Romita\u003c/b\u003e",
"polyline" : {
"points" : "ycw~F}ugkAU?I#E#E#a#RG#EDED_#d#OP"
},
"start_location" : {
"lat" : 41.9028523,
"lng" : 12.4964704
},
"travel_mode" : "DRIVING"
},
// cut
{
"distance" : {
"text" : "52 m",
"value" : 52
},
"duration" : {
"text" : "1 min",
"value" : 14
},
"end_location" : {
"lat" : 45.4665738,
"lng" : 9.1889772
},
"html_instructions" : "Continua su \u003cb\u003eVia Santa Margherita\u003c/b\u003e",
"polyline" : {
"points" : "ogotGoxaw#`#`#j#h#"
},
"start_location" : {
"lat" : 45.4669622,
"lng" : 9.189359399999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,2 km",
"value" : 192
},
"duration" : {
"text" : "1 min",
"value" : 87
},
"end_location" : {
"lat" : 45.46495119999999,
"lng" : 9.1892874
},
"html_instructions" : "Svolta a \u003cb\u003esinistra\u003c/b\u003e e prendi \u003cb\u003eVia Silvio Pellico\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "aeotGcvaw#Xw#DCBAxC#~A?d#A"
},
"start_location" : {
"lat" : 45.4665738,
"lng" : 9.1889772
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "//collapsed//"
},
"summary" : "A1/E35 e A1",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}
and if I use "transit" without an arrival time it works:
https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835,12.496365500000024&destination=45.4642035,9.
189981999999986&mode=transit&key=++API_KEY++
and:
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "EjJQaWF6emEgZGVsbGEgUmVwdWJibGljYSwgMTAsIDAwMTg1IFJvbWEgUk0sIEl0YWxpYQ",
"types" : [ "street_address" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJRYxePKzGhkcRsgPwamn2Pfo",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 45.4969308,
"lng" : 12.5528041
},
"southwest" : {
"lat" : 41.892252,
"lng" : 9.188611
}
},
"copyrights" : "Dati mappa ©2017 Google",
"legs" : [
{
"arrival_time" : {
"text" : "13:55",
"time_zone" : "Europe/Rome",
"value" : 1513169733
},
"departure_time" : {
"text" : "10:10",
"time_zone" : "Europe/Rome",
"value" : 1513156231
},
"distance" : {
"text" : "571 km",
"value" : 571467
},
"duration" : {
"text" : "3 ore 45 min",
"value" : 13502
},
"end_address" : "Piazza del Duomo, 1, 20121 Milano MI, Italia",
"end_location" : {
"lat" : 45.4639037,
"lng" : 9.188611
},
"start_address" : "Piazza della Repubblica, 10, 00185 Roma RM, Italia",
"start_location" : {
"lat" : 41.9028429,
"lng" : 12.4964793
},
"steps" : [
{
"distance" : {
"text" : "0,1 km",
"value" : 102
},
"duration" : {
"text" : "1 min",
"value" : 87
},
"end_location" : {
"lat" : 41.902193,
"lng" : 12.4958041
},
"html_instructions" : "Cammina fino a Repubblica",
"polyline" : {
"points" : "wcw~F_vgkA#CBADABA#A#?B?D?B?B#B?B#BBB##BBBDH#D#H##?D?BDRBL?#DXBLBJDHBFVM"
},
"start_location" : {
"lat" : 41.9028429,
"lng" : 12.4964793
},
"steps" : [
{
"distance" : {
"text" : "54 m",
"value" : 54
},
"duration" : {
"text" : "1 min",
"value" : 49
},
"end_location" : {
"lat" : 41.9024775,
"lng" : 12.4962611
},
"html_instructions" : "Procedi in direzione \u003cb\u003esudest\u003c/b\u003e verso \u003cb\u003ePiazza della Repubblica\u003c/b\u003e",
"polyline" : {
"points" : "wcw~F_vgkA#CBADABA#A#?B?D?B?B#B?B#BBB##BBBDH#D#H##?D?B"
},
"start_location" : {
"lat" : 41.9028429,
"lng" : 12.4964793
},
"travel_mode" : "WALKING"
},
{
"distance" : {
"text" : "48 m",
"value" : 48
},
"duration" : {
"text" : "1 min",
"value" : 38
},
"end_location" : {
"lat" : 41.902193,
"lng" : 12.4958041
},
"html_instructions" : "Esci dalla rotonda e prendi \u003cb\u003ePiazza della Repubblica\u003c/b\u003e",
"polyline" : {
"points" : "oaw~FstgkADRBL?#DXBLBJDHBFVM"
},
"start_location" : {
"lat" : 41.9024775,
"lng" : 12.4962611
},
"travel_mode" : "WALKING"
}
],
"travel_mode" : "WALKING"
},
{
"distance" : {
"text" : "0,4 km",
"value" : 437
},
"duration" : {
"text" : "1 min",
"value" : 46
},
"end_location" : {
"lat" : 41.9011732,
"lng" : 12.4996019
},
"html_instructions" : "Metropolitana verso Anagnina",
"polyline" : {
"points" : "u_w~FwqgkAeBIBoA#I|ByM~CmELR"
},
"start_location" : {
"lat" : 41.902193,
"lng" : 12.4958041
},
"transit_details" : {
"arrival_stop" : {
"location" : {
"lat" : 41.9011732,
"lng" : 12.4996019
},
"name" : "Termini"
},
"arrival_time" : {
"text" : "10:12",
"time_zone" : "Europe/Rome",
"value" : 1513156353
},
"departure_stop" : {
"location" : {
"lat" : 41.902193,
"lng" : 12.4958041
},
"name" : "Repubblica"
},
"departure_time" : {
"text" : "10:11",
"time_zone" : "Europe/Rome",
"value" : 1513156307
},
"headsign" : "Anagnina",
"line" : {
"agencies" : [
{
"name" : "Atac",
"phone" : "011 39 06 57003",
"url" : "http://www.atac.roma.it/"
}
],
"color" : "#e27439",
"name" : "Metro A",
"short_name" : "MEA",
"url" : "http://muovi.roma.it/percorso/js?query=MEA&cl=1",
"vehicle" : {
"icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/subway2.png",
"local_icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/it-metro.png",
"name" : "Metropolitana",
"type" : "SUBWAY"
}
},
"num_stops" : 1
},
"travel_mode" : "TRANSIT"
},
{
"distance" : {
"text" : "2,6 km",
"value" : 2641
},
"duration" : {
"text" : "6 min",
"value" : 330
},
"end_location" : {
"lat" : 45.4639037,
"lng" : 9.188611
},
"html_instructions" : "Metropolitana verso San Donato",
"polyline" : {
"points" : "//collapsed//"
},
"start_location" : {
"lat" : 45.4844397,
"lng" : 9.202612799999999
},
"transit_details" : {
"arrival_stop" : {
"location" : {
"lat" : 45.4639037,
"lng" : 9.188611
},
"name" : "Duomo"
},
"arrival_time" : {
"text" : "13:55",
"time_zone" : "Europe/Rome",
"value" : 1513169730
},
"departure_stop" : {
"location" : {
"lat" : 45.4844397,
"lng" : 9.202612799999999
},
"name" : "Centrale FS"
},
"departure_time" : {
"text" : "13:50",
"time_zone" : "Europe/Rome",
"value" : 1513169400
},
"headsign" : "San Donato",
"line" : {
"agencies" : [
{
"name" : "COMUNE DI MILANO",
"phone" : "011 39 02 0202",
"url" : "http://www.muoversi.milano.it/"
}
],
"color" : "#ffea00",
"name" : "M3 - Linea Gialla",
"short_name" : "3",
"vehicle" : {
"icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/subway2.png",
"local_icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/it-metro.png",
"name" : "Metropolitana",
"type" : "SUBWAY"
}
},
"num_stops" : 4
},
"travel_mode" : "TRANSIT"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "//collapsed//"
},
"summary" : "",
"warnings" : [
"Le indicazioni per tragitti a piedi sono in versione beta. Presta attenzione – questo percorso potrebbe non disporre di marciapiede o aree pedonali."
],
"waypoint_order" : []
}
],
"status" : "OK"
}
The issue in your request is the value that you pass as an arrival_time parameter. Please note that this value must be in seconds:
arrival_time — Specifies the desired time of arrival for transit directions, in seconds since midnight, January 1, 1970 UTC. You can specify either departure_time or arrival_time, but not both. Note that arrival_time must be specified as an integer.
https://developers.google.com/maps/documentation/directions/intro#DirectionsRequests
You are passing milliseconds 1513162800000 that corresponds to 03/22/49920 # 8:00am (UTC), the correct value is 1513162800 that corresponds to 12/13/2017 # 11:00am (UTC).
The request should be:
https://maps.googleapis.com/maps/api/directions/json?origin=41.9027835%2C12.496365500000024&destination=45.4642035%2C9.189981999999986&mode=transit&arrival_time=1513162800&key=YOUR_API_KEY
The same request in Directions calculator:
https://directionsdebug.firebaseapp.com/?origin=41.9027835%2C12.496365500000024&destination=45.4642035%2C9.189981999999986&mode=transit&arrival_time=1513162800
I hope this helps!
In case it helps someone else, I also found that setting a departure_time too far in the future (more than a year in my case) led to a "ZERO_RESULTS" response. Making it closer to the present fixed this.
When I fetchdata from https://maps.googleapis.com/maps/api/distancematrix/json?origins=10010&destinations=10011|10012|10013&key=AIzaSyBOsjeskUwOLYQZntsv7_34gVZ3xgcXko0
it returns :
{
"destination_addresses" : [
"New York, NY 10011, USA",
"New York, NY 10012, USA",
"New York, NY 10013, USA"
],
"origin_addresses" : [ "New York, NY 10010, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "3.4 km",
"value" : 3389
},
"duration" : {
"text" : "19 mins",
"value" : 1156
},
"status" : "OK"
},
{
"distance" : {
"text" : "2.6 km",
"value" : 2632
},
"duration" : {
"text" : "13 mins",
"value" : 767
},
"status" : "OK"
},
{
"distance" : {
"text" : "9.5 km",
"value" : 9545
},
"duration" : {
"text" : "19 mins",
"value" : 1143
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
But I need data order by distance. First showing small then large.
Is it possible?
Sure!
1. Parse the JSON data to an object (JSON.parse);
2. Sort the elements array using code as below:
// given els is one of the elements extracted from this object:
let sorted = els.sort((a, b) => a.distance.value - b.distance.value);
I have had some great help from #Joao and #Blakes Seven in order to get me as far as I've got. Awesome, thanks guys very much.
What I have a problem with is going from my original simple example and applying it to my real life scenario.
Where I've got to is two separate scripts which work perfectly by themselves but when I try to being the two together, it then only does the first part without applying the second part; it's my lack of experience in Mongo letting me down here.
So, I am able to get a name value pair from the first array set within a document using the following code:
db.raw_originBusinessData.aggregate([
{ "$match": {objectOriginAPI : "Profit & Loss"}}
,{ "$unwind": "$objectRawOriginData.Reports" }
,{ "$unwind": "$objectRawOriginData.Reports.Rows" }
,{ "$unwind": "$objectRawOriginData.Reports.Rows.Rows" }
,{ "$group": {"_id": "$_id","first": { "$first": "$objectRawOriginData.Reports.Rows.Rows.Cells.Value" }
, "temp": { "$push": "$objectRawOriginData.Reports.Rows.Rows.Cells.Value" }
}},
{ "$unwind": "$temp" }
,{"$skip":1}
,{ "$group": {"_id": "$_id", "AccountBalance":{ "$first": "$first" }
}}
])
This gives me the result below, which I'm happy with except for the fact I am not able to name the two values in the Account Balances array.
Mission 1: I want AccountBalance to be an array and the first position has two values: "AccountName" : "Sales", "AccountValue" : 5428.04.
{
"result" : [
{
"_id" : ObjectId("564d12da1506995581569428"),
"AccountBalance" : [
"Sales",
"5428.64"
]
}
],
"ok" : 1.0000000000000000
}
The second part of the problem is that it is only processing one set of values whereas, the document I am processing on has 9 sets to do. I have run the following $unwind on the document and it has perfectly split them into 9 results:
db.raw_originBusinessData.aggregate([
// find document
{ "$match": {objectOriginAPI : "Profit & Loss"}}
// unwind into multiple documents
,{ "$unwind": "$objectRawOriginData.Reports" }
,{ "$unwind": "$objectRawOriginData.Reports.Rows" }
,{ "$unwind": "$objectRawOriginData.Reports.Rows.Rows" }
])
So I get 9 results perfect. Mission 2: What I want to do is to combine this with the script I showed above. I tried the following but it does absolutely no more than the script just above.
db.raw_originBusinessData.aggregate([
// find document
{ "$match": {objectOriginAPI : "Profit & Loss"}}
// unwind into multiple documents
,{ "$unwind": "$objectRawOriginData.Reports" }
,{ "$unwind": "$objectRawOriginData.Reports.Rows" }
,{ "$unwind": "$objectRawOriginData.Reports.Rows.Rows" }
]
// process each document
,{ "$unwind": "$objectRawOriginData.Reports" }
, {"$unwind": "$objectRawOriginData.Reports.Rows" }
, {"$unwind": "$objectRawOriginData.Reports.Rows.Rows" }
, {"$group": {"_id": "$_id","first": { "$first": "$objectRawOriginData.Reports.Rows.Rows.Cells.Value" }
, "a": { "$push": "$objectRawOriginData.Reports.Rows.Rows.Cells.Value" }
}},
{ "$unwind": "$a" }
,{"$skip":1}
,{ "$group": {"_id": "$_id", "AccountBalance":{ "$first": "$first" }
}}
)
Desired Outcome
What I want to get is the following. This is combination of mission 1 and 2.
{
"result" : [
{
"_id" : ObjectId("564d12da1506995581569428"),
"AccountBalance" : [
{"AccountName" : "Sales",
"AccountValue" : "5428.64"},
{"AccountName" : "Total Income",
"AccountValue" : "5428.64"},
{"AccountName" : "Cost of Sales",
"AccountValue" : "100.00"},
{"AccountName" : "Total Cost of Sales",
"AccountValue" : "100.00"},
{"AccountName" : "Gross Profit",
"AccountValue" : "5328.64"},
{"AccountName" : "Advertising",
"AccountValue" : "100.00"},
{"AccountName" : "General Expenses",
"AccountValue" : "100.00"},
{"AccountName" : "Total Operating Expenses",
"AccountValue" : "200.00"},
{"AccountName" : "Net Profit",
"AccountValue" : "5128.64"}
]
}
],
"ok" : 1.0000000000000000
}
The document that I am using as a source is from Xero API - it's one of their reports. It has the same pattern as most reports exported in JSON like Oracle. Below is the actual report so that you have it for reference.
Thanks a million guys, very much appreciated!
{
"_id" : ObjectId("564d12da1506995581569428"),
"objectClass" : "Origin Data",
"objectCategory" : "Application",
"objectType" : "Customer",
"connection_id" : "562033dfca91840cd0c7c54f",
"connectionName" : "Building Accounts",
"entity_id" : "564149bcca9183a8d0c7c83c",
"objectCreationDate" : "2015-11-19 14:43:40",
"objectCycleID" : "12345678",
"objectStatus" : "PROCESSED",
"objectOrigin" : "Xero",
"objectOriginAPI" : "Profit & Loss",
"objectOriginService" : "Xero API - Profit & Loss v 1.0.0.8",
"objectRawOriginData" : {
"Id" : "d6e7fb37-9f2e-45ae-b0a4-de62aa95a783",
"Status" : "OK",
"ProviderName" : "Xero API Previewer",
"DateTimeUTC" : "/Date(1443405874333)/",
"Reports" : [
{
"ReportID" : "ProfitAndLoss",
"ReportName" : "Profit and Loss",
"ReportType" : "ProfitAndLoss",
"ReportTitles" : [
"Profit & Loss",
"Paddy's markets",
"28 September 2014 to 28 September 2015"
],
"ReportDate" : "28 September 2015",
"UpdatedDateUTC" : "/Date(1443405874333)/",
"Fields" : [],
"Rows" : [
{
"RowType" : "Header",
"Cells" : [
{
"Value" : ""
},
{
"Value" : "28 Sep 15"
}
]
},
{
"RowType" : "Section",
"Title" : "Income",
"Rows" : [
{
"RowType" : "Row",
"Cells" : [
{
"Value" : "Sales",
"Attributes" : [
{
"Value" : "a7e3f9e4-6f63-4b25-ae36-107131e8b9be",
"Id" : "account"
}
]
},
{
"Value" : "5428.64",
"Attributes" : [
{
"Value" : "a7e3f9e4-6f63-4b25-ae36-107131e8b9be",
"Id" : "account"
}
]
}
]
},
{
"RowType" : "SummaryRow",
"Cells" : [
{
"Value" : "Total Income"
},
{
"Value" : "5428.64"
}
]
}
]
},
{
"RowType" : "Section",
"Title" : "Less Cost of Sales",
"Rows" : [
{
"RowType" : "Row",
"Cells" : [
{
"Value" : "Cost of Sales",
"Attributes" : [
{
"Value" : "f78f7118-ad98-4862-a63c-39dd3c5ace8a",
"Id" : "account"
}
]
},
{
"Value" : "100.00",
"Attributes" : [
{
"Value" : "f78f7118-ad98-4862-a63c-39dd3c5ace8a",
"Id" : "account"
}
]
}
]
},
{
"RowType" : "SummaryRow",
"Cells" : [
{
"Value" : "Total Cost of Sales"
},
{
"Value" : "100.00"
}
]
}
]
},
{
"RowType" : "Section",
"Title" : "",
"Rows" : [
{
"RowType" : "Row",
"Cells" : [
{
"Value" : "Gross Profit"
},
{
"Value" : "5328.64"
}
]
}
]
},
{
"RowType" : "Section",
"Title" : "Less Operating Expenses",
"Rows" : [
{
"RowType" : "Row",
"Cells" : [
{
"Value" : "Advertising",
"Attributes" : [
{
"Value" : "67466588-132b-48ce-b897-0ceabffd7a9d",
"Id" : "account"
}
]
},
{
"Value" : "100.00",
"Attributes" : [
{
"Value" : "67466588-132b-48ce-b897-0ceabffd7a9d",
"Id" : "account"
}
]
}
]
},
{
"RowType" : "Row",
"Cells" : [
{
"Value" : "General Expenses",
"Attributes" : [
{
"Value" : "fdb25d7a-2fc8-406a-bf4b-6b4e8014b8cb",
"Id" : "account"
}
]
},
{
"Value" : "100.00",
"Attributes" : [
{
"Value" : "fdb25d7a-2fc8-406a-bf4b-6b4e8014b8cb",
"Id" : "account"
}
]
}
]
},
{
"RowType" : "SummaryRow",
"Cells" : [
{
"Value" : "Total Operating Expenses"
},
{
"Value" : "200.00"
}
]
}
]
},
{
"RowType" : "Section",
"Title" : "",
"Rows" : [
{
"RowType" : "Row",
"Cells" : [
{
"Value" : "Net Profit"
},
{
"Value" : "5128.64"
}
]
}
]
}
]
}
]
}
}
done it, see below, thanks:
db.raw_originBusinessData.aggregate([
{ "$match": {objectOriginAPI : "Profit & Loss"}}
,{ "$unwind": "$objectRawOriginData.Reports" }
,{ "$unwind": "$objectRawOriginData.Reports.Rows" }
,{ "$unwind": "$objectRawOriginData.Reports.Rows.Rows" }
,{ "$group": {"_id": "$_id","accountBalances": { "$push": "$objectRawOriginData.Reports.Rows.Rows.Cells.Value" }
}},
])