Random item from Algolia - algolia

How do I get random algolia item from it's index?
All of my items have:
objectID "POST#news#44.7704046#17.1900285"
name "News"
categories [ "cafe", "food", "establishment", "food" ]
_geoloc { lat: "44.7704046", lng: "17.1900285" }
I would like to optionally search by name, match 1 or all categories, geo location filtering with distance, and most importantly, I only want 1 RANDOM returned from Algolia.
I can't do client side random, because sometimes without filters I would get too many results back ( 10000 ), so I can't transfer that over the wire.
Please help

Hi #Djordje there are no real way to get a random result with Algolia though you you could use an attribute to randomise the results and only use the first item. See documentation [here][1]

Related

Query by item in array of document, and sort by item's position in the array

I have a collection dinosaurs with documents of this structure:
doc#1:
name: "Tyrannosaurus rex",
dominantColors: [
"beige",
"blue",
"green"
]
doc#2:
name: "Velociraptor",
dominantColors: [
"green",
"orange",
"white"
]
I want to query the collection by color name (for example: green) to get documents sorted by color's position in dominantColors array. First get the documents in which green occurs higher in the array, then those in which it is lower. So, in the provided case I would get doc#2 first, then doc#1.
Each dominantColors array contains 3 elements, with elements sorted from most dominant to least.
I am looking through documentation, but am not able to find a solution. Maybe I need a different data structure altogether?
Cloud Firestore doesn't support querying arrays by ranked index. The only way you can query an array is using an array-contains type query.
What you could do instead is organize your colors using maps where the color is the key and their rank is the value:
name: "Tyrannosaurus rex",
dominantColors: {
"beige": 1,
"blue": 2,
"green": 3
}
Then you can order the query by the value of the map's property. So, in JavaScript, it would be something like this:
firebase
.collection('dinosaurs')
.where('dominantColors.green', '>', 0)
.orderBy('dominantColors.green')

Search with flexible ranking

Can any one suggest search engine that has flexible ranking calculation?
What is flexible ranking calculation?
for example I have two documents:
obj1 = {
title: "new record"
tags: [
{value:"tag1", weight:1},
{value:"tag2", weight:0.8},
{value:"tag3", weight:2},
]
}
obj2 = {
title: "new record with tag1 in title"
tags: [
{value:"tag1", weight:0.5},
{value:"tag2", weight:1},
{value:"tag3", weight:0.01},
]
}
let's assume weight for "title" property is 0.25
When I do search for "tag1" in all properties
I want search to return ranking = 1 for obj1 and ranking = 0.75 for obj2
I know Solr can do it but do you have any other suggestions?
You mention weight for title but then the values you described for scores mapped directly to tag values. Not sure if you missed a detail on how these two connect.
Assuming you want the score of the title match to play a role in the overall document score in addition to boosting documents that match a particular tag or value range, you can do this with Azure Search using scoring profiles (if you want a search-as-a-service solution), and can do it with Solr or Elasticsearch by including boosts as part of the query if you prefer to deploy and management your own infrastructure; in Elasticsearch for example there are function boosts that will allow you to use the value of a field as input to boost computation.

Mongodb geolocation boundaries search/query

I have a documents contains list of location "boxes" (square area). Each box is represented by 2 points (bottom-left or south-west, top-right or north-east).
Document, for example:
{
locations: [
[[bottom,left],[top,right]],
[[bottom,left],[top,right]],
[[bottom,left],[top,right]]
]
}
I'm using 2d index for those boundaries points.
My input is a specific location point [x,y] and I want to fetch all documents that have at list one box that this point is located in it.
Is there any geospatial operator I can use to do that?
How do I write this query?
You can use the box operator, see:
http://docs.mongodb.org/manual/reference/operator/query/box/#op._S_box with the following example taken directly from that page:
db.places.find( { loc : { $geoWithin : { $box :
[ [ 0 , 0 ] ,
[ 100 , 100 ] ] } } } )
It is worth noting that the 2d index is considered legacy. If you can convert to using GeoJSON and a 2dsphere index, then you can use the $geoWithin operator: see
http://docs.mongodb.org/manual/reference/operator/query/geoWithin/#op._S_geoWithin
GeoJSON has a number of other benefits, not least of which, is that it is easily transmitted and digested by web mapping apps such as OpenLayers or Leaflet.

Selecting a different record in List View via Script

I have a table in Filemaker 11 which has fields: thingID, infoNumber (#), itemHistory. infoNumber displays the order in which we think the item history's happened (sometimes this is incorrect and needs to be rearranged).
thingID, #, itemHistory
Thing 1, 1, was with Adam
Thing 1, 2, was with Claire
Thing 1, 3, was with Ben
Thing 1, 4, was with Dave
I display these in a List View (ordered by infoNumber asc), and a user realises that it actually went "1,3,2,4", I want to have up and down arrows visible in order for users to switch them, i.e. clicking on the up arrow on the record with infoNumber=3 will set it to 2 and the old infoNumber=2 will be set as 3.
How can I write a script to switch these when the user clicks on a button in a list view?
My idea:
Set Variable[$clickedDown, infoNumber] #the record we click on's infoNumber
If [ $clickedDown != 1 ]
Set Field [ infoNumber, clickedDown -1 ]
# But how do I move to the record with infoNumber = clickedDown-1 ??
End If
The way I have done this, is to do several Finds, here is my solution for going one way.
# First you've clicked on something, record its current infoNumber
Set Variable [ $infoNumber, infoNumber ]
# Use -2 (arbitrary) as a temporary place holder
Set Field [ infoNumber = -2]
Error Capture [ On ]
Perform Find [ thingID = $thingID, infoNumber = $infoNumber - 1 ]
If [ Get (LastError = 401) ]
# No results then just re-search temporary and set it back to what it
was
Perform Find [ thingID = $thingID, infoNumber = -2 ]
Set Field [ infoNumber, $infoNumber ]
Else
Set Field [ infoNumber, $infoNumber ]
Perform Find [ thingID = $thingID, infoNumber = -2 ]
Set Field [ infoNumber, $infoNumber -1 ]
End If
# Go back to layout you were in before
I would be interested to know if there was a better way!
You could do this by using relationships. Create a new table occurrence (say, History Previous) and link it to the list layout's table occurrence (say, History) with the following predicates:
History::ThingID = History Previous::ThingID
History::infoNumber > History Previous::infoNumber
Sort the relationship by History Previous::infoNumber, descending.
This will provide you with a set of related History records that appear earlier in the list for the relevant Thing. The first record will be the immediately previous one, thanks to the sorting.
Now, when you run the script, you can:
If [ Count ( History Previous::ThingID ) > 0 ]
Set Variable [ $infoNumber, History Previous::infoNumber ]
Set Field [ History Previous::infoNumber, History::infoNumber ]
Set Field [ History::infoNumber, $infoNumber ]
End If
Note that, although the History Previous relationship may refer to multiple records, you can rely on the relationship sorting to provide you with access to the first record, both when getting data from it (in the Set Variable step) and setting data into it (in the first Set Field step).

Appcelerator Titanium ACS Order place by nearest the user

I am trying to order at list by nearest place. This is working fine with this code:
Cloud.Places.query({
page: 1,
per_page: 20,
where: {
lnglat: { '$nearSphere': [latitudefast,longitudefast], }
},
order: {
lnglat: { '$nearSphere': [latitudefast,longitudefast], }
},
latitudefast and longitudefast is representing the actual position on the user. It has be defined before the query.
But it is "upside down", which means that the nearest place is in the bottom of the list, and the one farthest away is at the top of the list! How come? How do I order in reverse? Am i ordering wrong?
Thanks!
Have you tried not specifying the order? $nearSphere by default returns results sorted by distance.
From MongoDB's documentation (which ACS uses as its data store) describing $near:
The above query finds the closest points to (50,50) and returns them
sorted by distance (there is no need for an additional sort
parameter).
This applies to $nearSphere as well.
http://www.mongodb.org/display/DOCS/Geospatial+Indexing