Riak search queries via the java client - riak

I am trying to perform queries using the OR operator as following:
MapReduceResult result = riakClient.
mapReduce("some_bucket", "Name:c1 OR c2").
addMapPhase(new NamedJSFunction("Riak.mapValuesJson"), true).
execute();
I only get the 1st object in the query (where name='c1').
If I change the order of the query (i.e. Name:c2 OR c1) again I get only the first object in query (where name='c2').
is the OR operator (and other query operators) supported in the java client?

I got this answer from Basho engeneer, Sean C.:
You either need to group the terms or qualify both of them. Without a field identifier, the search query assumes that the default field is being searched. You can determine how the query will be interpreted by using the 'search-cmd explain' command. Here's two alternate ways to express your query:
Name:c1 OR Name:c2
Name:(c1 OR c2)
both options worked for me!

Related

How to query a DynamoDB index with Dynamoose?

I have a DynamoDB table with animals and I'm interacting with it using Dynamoose. My table has a 'UserId' attribute, that indicates the user that registered that animal. I want to write a query that finds all the animals registered by the same user, i.e., gets all the items that have the attribute 'UserId' matching the input string.
I'm trying to use Dynamoose's queries like this MyModel.query('UserId').eq(user.id).using('UserId-index').exec();, but it always gives this error Index can't be found for query. I imagine that this is caused because it is not finding the index for the attribute 'UserId', but I have an index 'UserId-index' on my table.
I also tried specifying the index that should be used on the query with the using() method, like this MyModel.query('UserId').eq(user.id).using('UserId-index').exec();, but it gave me this other error: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request, which I don't get at all.
Note that I don't wanna use scan(), as the official documentation highly encourages the developers to use query() instead.

R sqlexecute wildcard

Using RODBCext (and Teradata) my SQL query often need to be restricted and is done so with a where statement. However, this is not always required and it would be beneficial to not restrict, but I would like to use a single SQL query. (The actual query is more complex and has several instances of what I'm attempting to apply here)
In order to return all rows, using a wildcard seems like the next best option, but nothing appears to work correctly. For example, the sql query is:
SELECT *
FROM MY_DB.MY_TABLE
WHERE PROC_TYPE = ?
The following does work when passing in a string for proc_type:
sqlExecute(connHandle, getSQL(SQL_script_path), proc_type, fetch = TRUE)
In order to essentially bypass this filter, I would like to pass a wildcard so all records are returned.
I've tried proc_type set to '%', '*'. Also escaped both with backslashes and enclosed with double-quotes, but no rows are ever returned, nor are any errors produced.
You could use COALESCE to do this:
SELECT *
FROM MY_DB.MY_TABLE
WHERE PROC_TYPE = COALESCE(?, PROC_TYPE);
In the event that your parameter is NULL it will choose PROC_TYPE to compare to PROC_TYPE which will return everything.
As for your wildcard attempt you would have to switch over to an operator that can use a wildcard. Instead of =, LIKE for instance. I think you would end up with some oddball edge cases though depending on your searchterm and the data in that column, so the COALESCE() option is a better way to go.

BigQuery error: Cannot query the cross product of repeated fields

I am running the following query on Google BigQuery web interface, for data provided by Google Analytics:
SELECT *
FROM [dataset.table]
WHERE
  hits.page.pagePath CONTAINS "my-fun-path"
I would like to save the results into a new table, however I am obtaining the following error message when using Flatten Results = False:
Error: Cannot query the cross product of repeated fields
customDimensions.value and hits.page.pagePath.
This answer implies that this should be possible: Is there a way to select nested records into a table?
Is there a workaround for the issue found?
Depending on what kind of filtering is acceptable to you, you may be able to work around this by switching to OMIT IF from WHERE. It will give different results, but, again, perhaps such different results are acceptable.
The following will remove entire hit record if (some) page inside of it meets criteria. Note two things here:
it uses OMIT hits IF, instead of more commonly used OMIT RECORD IF).
The condition is inverted, because OMIT IF is opposite of WHERE
The query is:
SELECT *
FROM [dataset.table]
OMIT hits IF EVERY(NOT hits.page.pagePath CONTAINS "my-fun-path")
Update: see the related thread, I am afraid this is no longer possible.
It would be possible to use NEST function and grouping by a field, but that's a long shot.
Using flatten call on the query:
SELECT *
FROM flatten([google.com:analytics-bigquery:LondonCycleHelmet.ga_sessions_20130910],customDimensions)
WHERE
  hits.page.pagePath CONTAINS "m"
Thus in the web ui:
setting a destination table
allowing large results
and NO flatten results
does the job correctly and the produced table matches the original schema.
I know - it is old ask.
But now it can be achieved by just using standard SQL dialect instead of Legacy
#standardSQL
SELECT t.*
FROM `dataset.table` t, UNNEST(hits.page) as page
WHERE
  page.pagePath CONTAINS "my-fun-path"

Sqlite Query Optimization with OR and AND

The following query takes 5 seconds to execute:
SELECT DISTINCT(Product.Name) FROM Product WHERE (0=1 OR Product.Number="prod11");
While the following takes ONLY 15 milliseconds:
SELECT DISTINCT(Product.Name) FROM Product WHERE (Product.Number="prod11");
Interestingly the following also takes only 15 milliseconds:
SELECT DISTINCT(Product.Name) FROM Product WHERE (1=1 AND Product.Number="prod11");
The query plan shows that the first query uses a full table scan (for some unknown reason), while the second and third queries use an index (as expected).
For some reason it looks like Sqlite optimizes the "1=1 AND ..." but it doesn't optimize "0=1 OR ...".
What can I do to make Sqlite use the index for the first query as well?
The queries are built by NHibernate so it's kind of hard to change them...
Sqlite version is the latest for Windows.
SQLite's query optimizer is rather simple and does not support OR expressions very well.
For some reason, it can optimize this query if it can use a covering index, so try this:
CREATE INDEX TakeThatNHibernate ON Product(Number, Name)
1=1and 1=0 are SQL expressions used in some parts of the NHibernate framework to denote empty statements that won't alter the logic of the sql query. A Conjunction with no subcriterias generates an 1=1 expression, A Disjunction with no subcriterias generates an 1=0 expression. An In() generates an 1=0 expression if no values are provided.
To avoid such optimization, you could change the code that is creating those empty expressions and only use the criterions that have at least one subcriteria.

Why does JDOQL query using "matches" semantics only work with a literal?

I'm trying to construct a JDOQL query (using datanucleus) that will search for matches of a parent class based on criteria in an owned one-to-many child class. The query looks like this:
Query lQ = lPm.newQuery("select from "+Module.class.getName()+
" where moduleMappings.contains(m)" +
" && showNameParam.matches(m.criteria.trim())");
lQ.declareVariables(ModuleMapping.class.getName()+" m");
lQ.declareParameters("String showNameParam");
lRet = (List<Module>) lQ.execute("StarTrek");
My data set looks something like this:
Module[1]
ModuleMapping[1]: criteria=".*"
Module[2]
ModuleMapping[1]: criteria=".*StarTrek.*"
ModuleMapping[2]: criteria=".*StarWars.*"
The query never matches on anything! However, if I replace the argument to the matches JDOQL method with a literal:
Query lQ = lPm.newQuery("select from "+Module.class.getName()+
" where moduleMappings.contains(m)" +
" && showNameParam.matches('.*StarTrek.*')");
Things will work for that single example, and my query will find Module[2]. What am I missing? Am I not allowed to use the contents of a mapped field as the argument to a JDOQL method? Do I need to escape things in some way?
Dave
So I figured this out, although to me it seems like a bug in either JDOQL or datanucleus. When using a mapped field as the argument to the matches method, the generated SQL does not translate the JDOQL syntax to the syntax of the data store (in my case, SQL). So in my example above, if I change the criteria fields to use SQL wildcard syntax rather than JDOQL syntax things will start to work.
Specifically, if in my example above I use criteria="%StarTrek%" rather than criteria=".\*StarTrek.\*" the JDOQL queries will start to match.
This doesn't seem right to me as different data stores could use different matching syntax, but this workaround gets me moving again...
The log would obviously tell you what SQL is being invoked for your query. You could also address why you are mixing API JDOQL and single-string JDOQL ... definition of parameters/variables should be in the single-string if using single-string.

Resources