filter the gremlin results - filter

I am new to Gremlin and I am using Gremlin 3.0.2 with Stardog 5.0. I wrote this query to find out paths between two entities in schema.org ontology. Below is the output -
gremlin>`g.V().hasLabel('Canal').repeat(both().simplePath()).until(g.V().hasLabel('Continent')).path().limit(5)`
==>[v[Canal], v[rdf-schema#Class]]
==>[v[Canal], v[BodyOfWater], v[Landform], v[Continent]]
==>[v[Canal], v[BodyOfWater], v[rdf-schema#Class], v[Continent]]
==>[v[Canal], v[BodyOfWater], v[Pond], v[rdf-schema#Class], v[Continent]]
==>[v[Canal], v[BodyOfWater], v[OceanBodyOfWater], v[rdf-schema#Class], v[Continent]]
I am unable to figure out a way to eliminate all the paths that has "rdf-schema#Class" in them. Please could someone suggest a solution? I want to do the filtering using Gremlin. Thanks!

One way to do it would be to apply a post filter to the path that is returned. So for example, if you had something like this:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().outE('created').inV().path()
==>[v[1],e[9][1-created->3],v[3]]
==>[v[4],e[10][4-created->5],v[5]]
==>[v[4],e[11][4-created->3],v[3]]
==>[v[6],e[12][6-created->3],v[3]]
and wanted to get rid of the edge, you could do something like this:
gremlin> g.V().outE('created').inV().path().
......1> local(unfold().filter(__.not(hasLabel('created'))).fold())
==>[v[1],v[3]]
==>[v[4],v[5]]
==>[v[4],v[3]]
==>[v[6],v[3]]
So that new line basically just says, for each path item, unfold it, do some filter on the label for those that aren't "created", and then fold the path back up to a list.

Related

AQL update succeeds in collection loop but throws not found error in graph traversal

I am trying to figure out how to update edge documents using a graph traversal query in arangodb. I am able to do this using the standard
FOR e IN collectionName UPDATE e with {newProps} IN collectionName. However I cannot figure out what is wrong with my attempt to accomplish the same thing using the
FOR v, e, p IN 1..5 OUTBOUND #startId GRAPH #graphName syntax.
I am using the arangodb:latest docker image on macOS Mojave 10.14.3, and I am using arango's go library (github.com/arangodb/go-driver) to query by building a query string and sending it using the Database.Query() function. I have tried just returning the key for the edge I'm trying to update (i.e. just returning e._key instead of attempting to update in the query below), and have verified using arangosh that that is indeed the correct key of the edge I'm trying to update. Additionally as stated above, I have been able to update an edge using the relational AQL syntax.
Here is my query:
FOR v, e, p IN 1..5 OUTBOUND #startId GRAPH #graphName
FILTER e.#key0 == #val0
UPDATE e._key WITH {#propName0: #propValue0} IN has_skill
RETURN {new: NEW, old: OLD}
and here are my bind variables:
[graphName:Matthew_Loughney, key0:_from, propName0:testProp, propValue0:testVal, startId:applicant/232, val0:applicant/232]
I would expect that when I view the has_skill collection using arangosh, I would see that my edge now has a property testProp with value testVal; however, I instead just get the error AQL: document not found (while executing) and my edge remains unchanged.
It turned out that my filter was returning some edges not in the has_skill collection (4 total to be exact), so when it tried to update in has_skill, it succeeded for the one I wanted but failed for the other 3, and since the UPDATE operation is atomic that made it fail for all of them. I did not notice this because I was only looking at the first edge returned when looking at the keys, and this wasn't an issue in my FOR e IN collectionName query because obviously those are all in the correct collection, so that only returned the 1 edge I was looking for.

Units of an elasticsearch query to get distance from arbitrary point to Geopoint

I have a django project which uses elasticsearch 6.5.3 to index products in a store with locations as GeoPoints. I am trying to query this index and also calculate distance between an arbitrary point, say user's location to each oh these results.
I am using elasticsearch_dsl and my code looks something like this:
search_query = search_query.script_fields(distance={
'script':{
'inline':"doc['location'].arcDistance(params.lat, params.lon)",
'params': {
'lat':user_loc.lat,
'lon':user_loc.lon
}
}
})
for result in search_query.execute():
print(result.distance)
Which gives me values that looks like:
[123456.456879123]
But I'm not sure about its units.
By using and online distance calculator in https://www.nhc.noaa.gov/gccalc.shtml,
which gives me the distance as ~123km,
It looks like value is in meters.
So:
1. Where can I find some definitive answers about its units?
Please point me to the relevant documentation for these methods.
I am also interested to know if there is a way to specify the units expected for the results in the method call.
2. Is there a better way to do this in python?
The units are those returned by the arcDistance method providing the value in your script.
The arc distance (in meters) of this geo point field from the provided lat/lon
The painless docs leave a lot to be desired (there appears to be no docs on this method in 6.5). The quote above was obtained from here: https://www.elastic.co/guide/en/elasticsearch/reference/2.3/modules-scripting.html
Additionally, they mention arcDistance caluclates meters here: https://www.elastic.co/guide/en/elasticsearch/reference/5.5/breaking_50_scripting.html
I'm not sure about the exact python API, but elasticsearch have Geo Distance Query:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html
In: https://github.com/elastic/elasticsearch-dsl-py/issues/398 there's an example of python usage of ES API:
MyDocType.search().filter(
'geo_distance', distance='1000m', location={"lat": "40", "lon": "-74"}
)
The 'geo_distance' query is the easiest way to get a distance between two geo points indexed to elasticsearch. I thinking that you don't need to use scripting in order to achieve that.
Regarding the distance unit, as you suspected the default is meters. from:
https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#distance-units
Wherever distances need to be specified, such as the distance parameter in the Geo Distance Query), the default unit if none is specified is the meter.

Pythons Elasticsearch-DSL filter for exactly one match from list of values

I saw some realted posts but none of them match my exact issue.
Using Python 2.7 with Elasticsearch-dsl (6.3, that is also my Elasticsearch version).
I want to do something like,
s = Search(using=elastic_conn, index='my_index').filter("match", service_name=['exmp_name1', 'exmp_name2'])
This syntax doesn't work though.
I wish to get back all documents with service_name == 'exmp_name1' OR service_name == 'exmp_name2'
I prefer to use the filter context rather then query context as from my understanding it's faster and scoring really isn't important to me, just an absolute match (or mismatch).
How can I achieve this behavior?
Thanks
Ok. All I needed is to filter by terms rather then match.
The terms syntax supports several values.
Working code:
s = Search(using=elastic_conn, index='audit').filter("terms", service_name=['exmp_name1', 'exmp_name2'])

Is it possible to obtain full path the objects find by a query

Suppose you have **[Colour = 'Purple'] query (see here) and you wished to find actual path to the resulting nodes as below:
[
Account.Order[0].Product[0].Description
Account.Order[0].Product[3].Description
]
Is this possible? Any functions that can achieve this?
Thanks in advance.

Grafana graph title from Prometheus snmp_exporter metrics

I want to graph interfaces usage from HP switch using snmp_exporter. As an index I use ifName. snmp.yml generator and scraping to Prometheus from switch works ok. Grafana shows graphs with a title taken from ifName.
But ifName is like Ethernet1/0/1 and so on. I have another metrics: ifAlias which I can configure on a switch with 'description' command e.g. 'UPLINK'.
So what I want to achieve, is to put ifAlias as a graph title next to ifName. I can not use ifAlias as an index, because it is not unique (most of ifAlias is empty like ifAlias="").
Is it possible to do something like that? If yes, then how? :-)
What I tried, it to use group_left and combine two metrics. It works, but I can only put ifAlias into Legend field like {{ifAlias}}.
Thanks in advance.
V.
ifAlias{ifAlias="",ifName="Ethernet1/0/1",instance="access",job="3com_snmp_exporter"}
ifAlias{ifAlias="UPLINK",ifName="GigabitEthernet1/0/25",instance="access",job="3com_snmp_exporter"}
ifName{ifName="Ethernet1/0/1",instance="access",job="3com_snmp_exporter"}
ifName{ifName="GigabitEthernet1/0/25",instance="access",job="3com_snmp_exporter"}

Resources