Neo4j Spatial order by distance - sorting

I'm currently using Spatial for my queries as follows:
START b=node:LocationIndex('withinDistance:[70.67,12.998,6.0]')
RETURN b
ORDER BY b.score
B is an entity that has a score and I'd like to order by this score, but I found a case in which, all the entities with score 0 were not ordered by distance. I know Spatial automatically orders by distance, but once I force the order by another field, I lose this order.
Is there any way of forcing this order as a second order field like:
START b=node:LocationIndex('withinDistance:[70.67,12.998,6.0]')
RETURN b
ORDER BY b.score, ?distance?

Unfortunately in the current spatial plugin, there is no cypher support at all, so the distance function (or distance result) cannot be accessed by the ORDER BY.
As you already noticed, the withinDistance function in the index itself will return results ordered by distance. If you do not add an extra ORDER BY in the cypher query, the distance order should be maintained. However, when adding the extra ORDER BY, the original order is lost. It would be an interesting feature request to the cypher developers to maintain the original order for elements that are comparatively identical in the ORDER BY.
There is also a separate plan to develop spatial functions within cypher itself, and that will solve the problem the way you want. But there is not yet any information on a development or release schedule for this.
One additional option that might help you in a shorter time frame and is independent of the neo4j development plans themselves, is to add an order by extension to the spatial index query. Right now you are specifying the index query as 'withinDistance:[70.67,12.998,6.0]', but you could edit the Spatial Plugin code to support passing extra parameters to this query, and they could be an order by parameter. Then you would have complete control of the order.

Related

In neo4j on what basis paths from apoc.path.spanningTree will get sorted(default sort)?

I am using apoc.path.spanningTree with some relationship filters and some label filters with maxLevel:-1
as a result, I am getting 5 paths as output in some order. I am not able to understand the basis of its sorting.
What I have noticed is, sorting is taking place on the basis of neo4j id of the last node in the path.
But If I update any intermediate node in any of the paths then this order changes.
The procedure is not documented to return the paths in any particular order, so you should not assume a particular ordering is used. And the algorithm can change at any time anyway.
If your query needs the paths in a specific order, it should sort the returned paths itself.

Elastic search calculation with data from different indexes

Good day, everyone. I have a lit bit strange case of using elastic search for me.
There are two different indexes, each index contain one data type.
First type contains next important for this case data:
keyword (text,keyword),
URL (text,keyword)
position (number).
Second type contains next data fields:
keyword (text,keyword)
numberValue (number).
I need to do next things:
1.Group data from the first ind by URL
2.For each object in group calculate new metric (metric A) by next simple formula: position*numberValue*Param
3.For each groups calculate sum of elements metric A we have calculated on stage 1
4.Order by desc result groups by sums we have calculated on stage 3
5.Take some interval of result groups.
Param - param, i need to set for calculation, this is not in elastic.
That is not difficult algorithm, but data in different indices, and i don`t know how to do it fast, and i prefer to do it on elastic search level.
I don`t know how to make effective data search or pipeline of data processing which can help me to implement this case.
I use ES version 6.2.3 if it is important.
Give me some advice, please, how can i implement this algorithm.
By reading 2. you seem to assume keyword is some sort of primary key. Elasticsearch is not an RDB and can only reason over one document at a time, so unless numberValue and position are (indexed) fields of the same document you can't combine them.
The rest of the items seem to be possible to achieve with the help of Aggregation

How to make ORDER by faster in cypher?

PART-I:
I have a lucene index on property a1 of a node n, and I have a cypher with
ORDER BY n.a1 DESC
Will it take advantage of the lucene index while sorting the results?
PART-II:
Lets assume i have similar indexes on a1, a2, a3...aN(individually), and I have a cypher with
ORDER BY n.a1, n.a2 DESC, n.a3... n.aN DESC
Will it take advantage of the indexes or, do i have to define some kind of a multi field index separately for this particular combination of the fields and asc/desc ?
Part I.
No. From the Java API you can add Lucene sort query objects.
Part II
No, see above.
The sorting happens without using any indexes just the results that are part of your query.
The index is only used to lookup nodes for starting points.

Setting priority in lucene.net results

I am using a Lucene.Net query like this
(PropertyID:1 OR PropertyID:25 OR PropertyID:5 OR PropertyID:10 OR PropertyID:15)
I want result from Lucene.Net in order of PropertyId. I passed for example first record should be for PropertyId 1 second for 25 and third for 5. But currently Lucene.Net arranging result set in different way.
The order of fields in the query has no effect on sorting.
There are 2 ways to achieve the sorting you're looking for:
Use boosts in your query. You can boost PropertyID:1 higher than the rest so that these matches are scored higher and thus appear first in the results, then score PropertyID:2 second highest, etc. For example:
(PropertyID:1^5 OR PropertyID:25^4 OR PropertyID:5^3 OR PropertyID:10^2 OR PropertyID:15) This is simple to implement but may not work right if you're including other criteria in your query because that other criteria will affecting the scoring.
Implement custom sorting via your own Comparator class. This may take quite a bit of work especially given the lack of resources on the web for doing this, however it will give you the greatest control over your sorting. Here is an example of a custom Comparator used to sort by a string value alphabetically that may be a good place for you to start.

MongoDB geospacial query

I use mongo's "$near" query, it works as expected and saves me a lot of time.
Now I need to perform something more complicated. Imagine, we have a collection of "checkins" (let's use foursquare notation), that contains the geospacial information (nothing unusual: just lat and lng) and time. Given the checkins by two people, how do I find their "were near to each other" checkins? I mean, e.g.: "1/23/12 you've been 100 meters away"
The easiest solution is to select all the checkins by the first user and find nearest checkin for each first user's checkin on the framework side (I use ruby). But is it the most efficient solution?
Do you have better ideaas? May be I need some kind of a special index?
Best,
Roman
The MongoDB GeoSpatial indexes provide two types of queries: $near and $within. The $near query returns all points in the database that are within a certain range of a requested point, while the $within query lists all points in the database that are inside of a particular area (box, circle, or arbitrary polygon).
MongoDB does not currently provide a query that will return all points that are within a certain distance of any member of another set of points, which is what you seem to want.
You could conceivably use the point data from user1 to build a polygon describing the "area of interest" and then use the $within query to see if there were any checkins by other people inside of that area. If you use a compound index on location & date, you could even restrict the query to folks who were inside of that area on a particular day.
References:
http://docs.mongodb.org/manual/core/indexes/#geospatial-indexes
http://docs.mongodb.org/manual/reference/operators/#geospatial

Resources