How to sort by totalCount in Gatsby? - sorting

I'm displaying a list of tags in Gatsby using this query:
query={graphql`
query {
tags: allMarkdownRemark {
group(field: frontmatter___tags) {
fieldValue
totalCount
}
}
}
`}
At the moment it includes all the tags ever used. I want to limit the list to the top 10 most popular tags. In order to do this I need to sort them first by totalCount. How can I accomplish this?

I do not think that you can sort them and then limit them using just a graphql query. My process for this would be to query the entire list of tags just like you have it, and then use javascript after that to manipulate the list of objects. There is a post that covers how to sort a list just like the one you will get back from this here:
Sorting Objects by Property Values
This would then return an array, and you could loop over the array for the first ten objects.

Related

How to query object with contains specific key in array in Graphql

I have object to query:
query demo {
demo {
name # string
words # array of strings
}
}
I am sending this query to API, property words is array of strings.
I would like to query all object which contains hello in words (array of strings) property.
It is possible on client side using for example #include ?
And how?
The #skip and #include directives are used to explicitly skip or include a particular field based on the provided if argument. They are normally used in conjunction with variables, so that the same document can be used to effectively generate queries with different selection sets (sets of fields) based on the provided variable.
For example, you can write a single query like this
query demo($includeName: Boolean!) {
demo {
name #include(if: $includeName)
words
}
}
and just pass in a different value for includeName instead of writing two separate queries:
query demoWithName {
demo {
name
words
}
}
query demoWithoutName {
demo {
words
}
}
GraphQL provides built-in mechanism for specifying which fields to return like fragments and the #skip and #include directives. It does not, however, have any built-in mechanism for filtering (or, for that matter, sorting, pagination, etc.). The server has to implement a way to filter fields by adding the appropriate arguments to fields and changing the field resolution logic to account for the values of those arguments.

Apollo GraphQl Query get data from two documents

i have a query and i want to go through two different documents, is it possible to do it ?
Document A { _id, senders_ID[sender_id]}
Document B { sender_id, firstname}
// i want to loop through document A and get each sender_id and go to document B to get the firstname.
Is it what you are looking for ?

Group by field in found document

The best way to explain what I want to accomplish is by example.
Let us say that I have an object with fields name and color and transaction_id. I want to search for documents where name and color match the specified value and that I can accomplish easily with boolean queries.
But, I do not want only documents which were found with search query. I also want transaction to which those documents belong, and that is specified with transaction_id. For example, if a document has been found with transaction_idequal to 123, I want my query to return all documents with transaction_idequal to 123.
Of course, I can do that with two queries, first one to fetch all documents that match criteria, and the second one that will return all documents that have one of transaction_idvalues found in first query.
But is there any way to do it in a single query?
You can use parent-child relation ship between transaction and your object. Or nest the denormalize your data to include the objects in the transactions. Otherwise you'll have to do an application side join, meaning 2 queries.
Try an index mapping similar to the following, and include a parent_id in the objects.
{
"mappings": {
"transaction": {},
"object": {
"_parent": {
"type": "transaction"
}
}
}
}
Further reading:
https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child-mapping.html

Can I get outer document fields from nested top hits aggregation?

I am making use of the functionality that was added in Elasticsearch 1.5 to allow a top hits aggregation inside a nested aggregation. The problem I have is that once I have my top nested documents I want to be able to also get fields from their outer documents.
my pseudo aggregation structure is
nested: {
some_other_aggreagation: {
"top_hits": {
}
}
}
The top nested hits include the index, type and id of the outer document, so I could perform a secondary search, but I'd like to avoid that. My other option is to return all of the hits from the query (currently I only return the results of the aggregations) and then match up the documents with the events in my code, but that seems bad from a performance point of view.
Can anyone suggest something better? Thanks.

Getting all nested data object using facets with Elastic

Greatings,
I have done some searching around but I have not come across a solid answer. We are using elastic search and trying to use facets to group together nested objects. Here is what the data looks like:
{
id:1,
name:abc,
object:{
tag: 5,
name:'test1',
set: true
id
}
},
{
id:2,
name:def,
object:{
tag: 2,
name:'test2',
set: false
}
}
and I want to use facets to get a count of object nested. I can get one field from the object using something like this:
{"facets":{"tags":{"terms":{"field":"object.name"}}}}'
but that just gets me the name and a count from the parent object its in. I want all the properties within object. I want tag,name and set to come back within the facet.
Is this possible? All signs seem to point to no, or to use something like script, where i can create a composite field of all 3, but that would require post processing, which i was hoping to avoid doing.
Any help would surely be appreciated. Thank you in advance.

Resources