ElasticSearch Get Value of Nested Attribute - indexing

I have this structure in index:
{ details: {errors:[], warnings:[], results:[]}}
I want a query so that I can get the count of errors warnings and results for each record.
I was looking into the script filter on an aggregate, but the thing is I am already aggregating and I'm unsure on how to use the value count filter with this since I need to
1) Get the .length attribute of the array
2) When I do that I get an error saying no attribute like that exists
Can I get a sample query? I'm guessing it will somehow incorporate the script filter.

The best way to achieve this is the token count data type told in the below link
LINK - http://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-core-types.html#token_count
You can couple this with multi fields to achieve an extra field per error , warning and results. This can be used in the query to view the count of elements.

Related

Querying ActiveRecord Syntax

I'm working on a task where I have to define a function that attaches something to a column in my DB, however I'm quite new and despite looking at the ActiveRecord documentation I don't appear to be able to grab the column I'm looking for.
For example, I have a table with many columns including 'State' and 'Phase', I was able to grab state with the following code:
CaseFileStatus.where(state: case_file.state).first
However I can't somehow manage to grab the 'Phase' column now, as shown below.
CaseFileStatus.where(state: "case_file.phase")
CaseFileStatus Load (2.5ms) SELECT "case_file_statuses".* FROM "case_file_statuses" WHERE "case_file_statuses"."state" = $1 [["state", "case_file.phase"]]
=> []
I'm sure it's a super basic error, but how should I be structuring this query?
If you need to query records based on the phase column, you should do something like this:
CaseFileStatus.where(phase: case_file.phase)

how to get additional fields on udemy api

I would like to list fields on course eg title,header when use course-list. does anybody knows about a way to query by fields so the returned list of fields ?
when query on
GET api-2.0/courses/?category=Development&subcategory=Web Development&price=price-free&is_affiliate_agreed=True&instructional_level=all&fields=title,headline
result
result is count all query

Few questions about Grails' createCriteria

I read about createCriteria, and kind of interested on how these works, and its usability in providing values for dropdown box.
So say, i have a table in the database, Resource table, where i have defined the table in the domain class called Resource.groovy. Resource table has a total of 10 columns, where 5 of it are
Material Id
Material description
Resource
Resource Id
Product Code
So using the createCriteria, and i can use just like a query to return the items that i want to
def resList = Resource.createCriteria().list {
and {
eq('resource', resourceInstance)
ne('materialId', '-')
}
}
Where in the above, i want to get the data that matches the resource = resourceInstance, and none of the materialId is equal to '-'.
I want to use the returned data from createCriteria above on my form, where i want to use some of the column on my select dropdown. Below is the code i used for my select dropdown.
<g:select id="resourceId" name="resourceId"
from="${resList}"
disabled="${actionName != 'show' ? false : true}" />
How do i make it so that in a dropdown, it only shows the values taken from column Product Code? I believe the list created using createCriteria returns all 10 columns based on the createCriteria's specification. But i only want to use the Product Column values on my dropdown.
How do i customize the data if in one of the select dropdown in my form, i wanted to show the values as "Resource Id - Resource Description"? The values are combination of more than 1 columns for one select dropdown but i don't know how to combine both in a single select dropdown.
I read that hql and GORM query are better ways of fetching data from table than using createCriteria. Is this true?
Thanks
First of all refer to the document for using select in Grails. To answer all questions:
Yes, the list to select from in the dropdown can be customized. In this case it should be something like from="${resList*.productCode}"
Yes, this can be customized as well with something like
from="${resList.collect { \"${it.resourceId} - ${it.resourceDesc}\" } }"
It depends. If there are associations involved in a domain then using Criteria will lead to eager fetches which might not be required. But with HQL one gets the flexibility of tailoring the query as needed. With latest version of Grails those boundries are minimized a lot. Usage of DetachedCriteria, where queries etc are recommended whereever possible. So it is kind of mixing and matching to the scenario under consideration.

Selecting specific joined record from findAll() with a hasMany() include

(I tried posting this to the CFWheels Google Group (twice), but for some reason my message never appears. Is that list moderated?)
Here's my problem: I'm working on a social networking app in CF on Wheels, not too dissimilar from the one we're all familiar with in Chris Peters's awesome tutorials. In mine, though, I'm required to display the most recent status message in the user directory. I've got a User model with hasMany("statuses") and a Status model with belongsTo("user"). So here's the code I started with:
users = model("user").findAll(include="userprofile, statuses");
This of course returns one record for every status message in the statuses table. Massive overkill. So next I try:
users = model("user").findAll(include="userprofile, statuses", group="users.id");
Getting closer, but now we're getting the first status record for each user (the lowest status.id), when I want to select for the most recent status. I think in straight SQL I would use a subquery to reorder the statuses first, but that's not available to me in the Wheels ORM. So is there another clean way to achieve this, or will I have to drag a huge query result or object the statuses into my CFML and then filter them out while I loop?
You can grab the most recent status using a calculated property:
// models/User.cfc
function init() {
property(
name="mostRecentStatusMessage",
sql="SELECT message FROM statuses WHERE userid = users.id ORDER BY createdat DESC LIMIT 1,1"
);
}
Of course, the syntax of the SELECT statement will depend on your RDBMS, but that should get you started.
The downside is that you'll need to create a calculated property for each column that you need available in your query.
The other option is to create a method in your model and write custom SQL in <cfquery> tags. That way is perfectly valid as well.
I don't know your exact DB schema, but shouldn't your findAll() look more like something such as this:
statuses = model("status").findAll(include="userprofile(user)", where="userid = users.id");
That should get all statuses from a specific user...or is it that you need it for all users? I'm finding your question a little tricky to work out. What is it you're exactly trying to get returned?

MDX: Filter a member that doesn't exist?

I'm new to MDX, so I assume this is a newbie question.
Existing dimension: status
Existing Measure: count
Existing Query:
SELECT
NON EMPTY [status].CHILDREN ON 0,
NON EMPTY Measures.count ON 1
FROM [objects]
I want count the records with status='C'. But sometimes, there're no records that match that criteria. So if I do:
SELECT
NON EMPTY [status].[C] ON 0,
NON EMPTY Measures.count ON 1
FROM [objects]
I get this error:
*MDX object '[status].[C]' not found in cube 'objects'*
I would like a query that returns 0 in that case. Which is the right way to do that query?
Thanks!
I assume from your question that you don't actually have a Status dimension table in your datasource. Instead, you have a dimension defined that is using a column in the Fact table. This works great, but it doesn't allow you to define a fixed set of members for that dimension...you're basically stuck with the facts that you have.
The solution, the way I see it, would be to create a separate table called DimStatus, and pre-populate it with all of the valid statuses that might be used by your facts, and have the fact table reference the Status dimension table.
Then things will work the way you want them to. You can automatically hide unused members, or you can include all of them, or a set of just the ones you want.
One caveat is that it will show (null) instead of 0 if there are no matching facts. You can get around this with a simple IIF(ISEMPTY()):
WITH MEMBER
[ZeroCount] AS
IIF(ISEMPTY([Measures].[Count]), 0, [Measures].[Count])
SELECT [ZeroCount] ON COLUMNS,
[Status].Members on ROWS
FROM [MyCube]
This will show all statuses and either the count, or a zero. Unfortunately it requires changes to your data source and cube, so hopefully that's an option for you.
I played with some queries and I don't think it's possible to do this more easily. You can't create a set with invalid members--that would create invalid tuples, and nothing would work properly. I also tried the "hack," and didn't get it to work as expected.
The best idea, which is what most client applications do, is to query the metadata to find out what members exist in a dimension and then only issue queries for members that exist.
The following article lists the various ways of getting at dimension metadata link text
the other "hack" might be to do the following:
SELECT
NON EMPTY StrToSet("[status].[C]") ON 0,
NON EMPTY Measures.count ON 1
FROM [objects]
But that will still not give you a 0, instead it will give you cell set with no columns. And it's not really an approach that I would recommend.
The solution is to use dynamic sets (CREATE DYNAMIC SET). These are evaluated based on user permissions.
WITH DYNAMICS SET [MySet] {[status].[C]}
SELECT
NON EMPTY [MySet] ON 0,
NON EMPTY Measures.count ON 1
FROM [objects]
But as stated above, this solves only filtration, still you miss any member o axis 0 to display.