How do i extract all the distinct values from a field in opensearch?
I am trying get list of all the distinct names using following:
{"find": "terms", "field": "first_name"}
I also want to utilize the same syntax in grafana to create a variable.
Related
One of my collections in Cloud Firestore has an where each item in the array contains three separate values (see the groupMembership field:
I know I can't write a query to find documents that match one of the array values. Is there a way to write a query to find documents that match a specific value for all three items?
For instance, I want to find all users where the groupMembership array contains one object that is equal to groupId: X, groupName: Y, membershipStatus: active
You can pass the entire object in an array-contains clause.
So something like:
usersRef
.whereField("groupMembership", arrayContains: [
"groupId": "kmDT8OUOTCxSMIBf9yZC",
"groupName": "Jon and Charles Group",
"membershipStatus": "pending",
])
The array item must completely and exactly match the dictionary you pass in. If you want to filter only on some of the properties in each array element, you will have to create additional fields with just those properties. For example, it is quite common to have say a field groupIds with just the (unique) group Ids, so that you can also filter on those (of course within the limits of Firestore's queries).
I have an Algolia Index that contains objects like this:
id: 2,
name: test,
important: ["lorem", "ipsum", "dolor", "sit", "amet"]
I want to retrieve all entries that e.g. contain either "dolor" or "sit".
How would I go about this?
Note: This is just an example, the importantarray of each entry would normally contain around 1 to 4 values (in total around 1.000 possible values). The array to filter it by / to search for could have anywhere between 1 to 400 values.
What AFAIK doesn't work:
searching in Facet Values by using a facetQuery: facetQuery does not allow for boolean operators. Therefore I can only search for only one of "dolor" or "sit" at once, see docs.
The filters docs however says
Non-numeric attributes (e.g. strings) need to be set up as categories, which we call facets.
So I am wondering if this is possible at all...? Or maybe I am approaching this issue the wrong way?
You are looking at the right place and need to combine attributesForFaceting and filters:
set the important attribute as an attributesForFaceting either via API or the Dashboard
then use the filters to filter on your desired values
Your filter will look like this: { "filters": "important:dolor OR important:sit" }
The JSON column type accepts non valid JSON
eg
[1,2,3] can be inserted without the closing {}
Is there any difference between JSON and string?
While [1,2,3] is valid JSON, as zerkms has stated in the comments, to answer the primary question: Is there any difference between JSON and string?
The answer is yes. A whole new set of query operations, functions, etc. apply to json or jsonb columns that do not apply to text (or related types) columns.
For example, while with text columns you would need to use regular expressions and related string functions to parse the string (or a custom function), with json or jsonb, there exists a separate set of query operators that works within the structured nature of JSON.
From the Postgres doc, given the following JSON:
{
"guid": "9c36adc1-7fb5-4d5b-83b4-90356a46061a",
"name": "Angela Barton",
"is_active": true,
"company": "Magnafone",
"address": "178 Howard Place, Gulf, Washington, 702",
"registered": "2009-11-07T08:53:22 +08:00",
"latitude": 19.793713,
"longitude": 86.513373,
"tags": [
"enim",
"aliquip",
"qui"
]
}
The doc then says:
We store these documents in a table named api, in a jsonb column named
jdoc. If a GIN index is created on this column, queries like the
following can make use of the index:
-- Find documents in which the key "company" has value "Magnafone"
SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc #> '{"company": "Magnafone"}';
This allows you to query the jsonb (or json) fields very differently than if it were simply a text or related field.
Here is some Postgres doc that provides some of those query operators and functions.
Basically, if you have JSON data that you want to treat as JSON data, then a column is best specified as json or jsonb (which one you choose depends on whether you want to store it as plain text or binary, respectively).
The above data can be stored in text, but the JSON data types have the advantage you can apply JSON rules in those columns. There are several functions which are JSON specified which cannot be used for text fields.
Refer to this link to understand about the json functions/procedures
I am wondering how to retrieve all values stored in a column in mongoDB, and put them in a list. find() only get all the fields, and specify <field>: <value> isn't an option as well.
If you are expecting to retrieve unique values in a column then distinct should work for you. Following is the syntax :
db.yourcollection.distinct("yourfield");
Learn more about distinct here
In mongodb you could use command like
db.sessions.distinct("Ip",{ 'Application': '123'})
which will return all unique ip for the selected application. How to do that via Mongoid?
I trying to pass 2 argument in distinct function but it fails with exception 'ArgumentError: wrong number of arguments (2 for 1)'
Distinct in Mongoid takes one argument -- the field you wish to filter distinct on. So in your case, you could chain a where clause w/a distinct like so:
YourModel.where(Application: '123').distinct(:Ip)
This would produce a collection of distinct YourModel's by field Ip where the field Application is equal to '123'.
Show you full distinct query, please.
I try follow syntax with users collection(3 documents with name Bob):
db.users.distinct("_id", {name: "Bob"})
And It's works:
[
ObjectId("5121792d499af102889f2576"),
ObjectId("5121792e499af102889f2577"),
ObjectId("5121792f499af102889f2578")
]
My MongoDB version is 2.2.0