Query JCR SQL2 in Magnolia with a multivalue where-condition - magnolia

We are implementing a custom REST endpoint in magnolia that queries for pages and it should be able to filter by categories. I found the JCR Query Cheat Sheet, but I could not see an example about filtering a multivalued field. In our case, we have a categories field with an array of category UUIDs and I want all pages having a certain category uuid. It works with using a like query (lets say 123-456 is a uuid) such as:
"SELECT * FROM [mgnl:page] p where p.[categories] like '%123-456%'
Is there a better approach without using (possible slow) LIKE queries to explicitly check for an intersection with the categories array? Are there any SET/ARRAY functions to use in where conditions for such filtering?

In case of multifield values, you can put multiple WHERE statements like so:
SELECT * FROM [mgnl:content] AS p
WHERE p.[categories] = '415025c6-e4b5-4506-9384-34f428a52104'
AND p.[categories] = 'e007e401-1bf8-4658-8293-b9c743784264'
This will return nodes in which categories (multivalue) property contains both IDs.

Related

Wordpress Meta / Non-Meta Query 'OR' Clause

We've written a custom Wordpress API and the API has parameter called 'id'. The values that are passed into this parameter could be either Custom IDs defined in a custom field or (if that is empty) a Wordpress Post IDs. In other query languages I could accomplish this several different ways (either two WHERE clauses connected with an OR statement or else with a custom field that is populated with data from either column). I cannot, however, figure out how to do this in Wordpress. I feel like it should be possible, but I can't find any example.
If I was searching for two values in a Meta Query or a Tax Query, I could use an OR statement, but I can't figure out how to do this between a Meta Query and a Non-Meta Query.
Is this possible and how would you do it?

Firebase queries and compound indexes

I have the following document structure in firebase:
{
typeId: number,
tripId: number,
locationId: string,
expenseId: number,
createtAt: timestamp
}
I want to query this collection using different 'where' statement everytime. Sometimes user wants to filter by type id and sometimes by locationId or maybe include all of the filters.
But it seems like I would need to create a compound index of each possible permutation? For example: typeId + expenseId, typeId + locationId, location + expenseId, etc, otherwise it doesn't work.
What if I have 20 fields and I want to make it possible to search across all of these?
Could you please help me to construct a query and indexes for the following requirement: Possibility to query across all fields, query can contain one, two, three, all or no fields included in where clause and always has to be ordered descending order by createdAt.
Cloud Firestore automatically creates indexes for the individual fields of your documents. So it can already filter on each field without you have to manually add these indexes.
In many cases it is able to combine these indexes to allow queries on field combinations, by performing a so-called zig-zag-merge-join.
Custom additional indexes are typically only needed once you add an ordering-clause to your query, in addition to filter clauses. If you have such a case, the Firestore client will log an error telling you exactly what index to create (with a link to the Firestore console that is prepopulated to created the index for you).

How can I query multiple fields on a single map data in Firestore?

I'm building an app using React + Redux + Firebase. In Firestore users collection, I have multiple fields including language, subject as map data. So for example,
// language that user can speak
language = {
english: true
}
// subjects that the user can teach
subject = {
math: true
science: true
}
In the main app, user can search other users by filtering subjects and languages. For example, user may look for the other users who can teach 'math' and 'science' using an 'english' language.
And I'm struggling with filtering multiple fields in map data. I could query for on field by doing ref.where('subject.${val}', '==' , true). But is there any way to query on multiple fields to get a document which contains a subject hash at any given number of subjects?
I tried to put data in array but currently firebase Array membership only support filtering one item in the array. So I guess it's a good start with storing in hash map. Any suggestion?
I tried to put data in array but currently firebase Array membership only support filtering one item in the array.
You're right, you can filter your items only by a single item that exist in an array. If you'll chain more than one function call, you'll get an error similar with this:
Invalid Query. Queries only support having a single array-contains filter.
And to answer your question:
But is there any way to query on multiple fields to get a document which contains a subject hash at any given number of subjects?
Unfortunately, there are no wildcards in Firestore. You have to identify the property names of your documents by their exact property name.
If you want to limit the results by filtering on multiple properties, you need to chain where() functions, like in the following example:
ref.where('subject.math', '==' , true)
.where('subject.science', '==' , true)
.where('language.english', '==' , true)

Create filter of an attibute and other of a date range

I've a list of items of my Entity. I want to filter it by an attribute as do SonataAdminBundle or other Admin Generator.
For example, if I have an attribute "color" then in my view should have a select field with the types of color and when the user choose one of them my view should only show the list of items filtered by "color"
How do I do it?
Edit:
I have almost solved first filter creating a form and in the controller action get the attribute using the request. Then I used findBy to filter the query.
Now is time of date range filter and merge it with the filter above. I want to filter my list of items by two o three filters.
How do I get from the controller to return only items that belong to a date range?
What is the sentence DQL to use three filters which are optional? I can filter out for none, one or all filters.
You already mentioned the solution: Doctrine Filters
This is what I use if I only want to get a subset of related entities. I think the Documentation is straight forward.

How-to count nodes based on cck filed data in Drupal

Nodes with cck checkboxes
need counting nodes based on cck data and displaing via views field
In drupal6 + taxonomy - there is a simple and fast function taxonomy_term_count_nodes()
But I`m thinking about d7 without taxonomy via cck custom field
Are there any API functions for counting nodes based on CCK fileds ?
I don't know if there is an API function to count nodes with CCK fields in D7, but there's nothing magical about API functions, you can easily create the function yourself if you need it. I don't know how the table structure is going to be, but if it looks like CCK in D6, you could do something like this:
function mymodule_field_node_count($content_type, $field_name) {
return db_result(db_query("SELECT COUNT(*) FROM {field_%s}
WHERE %s <> NULL AND %s <> '';",
$content_type, $field_name, $field_name));
}
You could probably make it prettier than the above, this is just to show that is you need to do something, you can just create a function to solve it for you. After all many API functions is often little more than a bit of logic and some queries that's commonly needed.

Resources