Using another field value as default in a solr function query - solr

I'm trying to run the following query:
/solr/select?q=_val_:query("{!dismax qf=text v='solr rocks'}", my_field)
But, specifying my_field as the default value throws the error:
java.lang.NumberFormatException: For input string: "my_field"
Additionally, these queries also fail:
/solr/select?q=_val_:query("{!dismax qf=text v='solr rocks'}", ceil(my_field))
/solr/select?q=_val_:query("{!dismax qf=text v='solr rocks'}", ceil(1.0))
Can we not specify another field or function as the default in function queries? Is there another way to accomplish what I'm trying to do?
I'm using Solr 3.1.

According to the code of the ValueSourceParser for QueryValueSource (line 261), the 2nd argument of query can only be a float. So 3 or 4.5 would work, but my_field or ceil(1.0) which are ValueSources instead of constants would not.
I don't know what your use case is, but would taking max(query("{!dismax qf=text v='solr rocks'}"), my_field) be good enough? (Provided that my_field has positive values, the result would only differ from what you are trying to do when the score of the query is lower than the value of my_field)
Otherwise, if you really need this feature, it should be fairly easy to implement your own function based on QueryValueSource in order to take a ValueSource as the 2nd argument instead of a float.

I did find an alternate way to mimic the desired logic:
/solr/select?q=_val_:sum(query("{!dismax qf=text v='solr rocks'}"),product(map(query("{!dismax qf=text v='solr rocks'}",-1),0,100,0,1), my_field))
A little roundabout way to do it, but works fine.

Related

Table Field set twice in HQL only appears once in the compiled SQL

My HQL query is as under (Hibernate with MS SQL Server)
SELECT...FROM...WHERE...
AND...
AND REVERSE(SUBSTRING(REVERSE(**ALIAS.info**),0, CHARINDEX('#', REVERSE(**ALIAS.info**)))) in (:var1_0,:var1_1)
AND...
The query compiles correctly. However when I try to use the query.list() method, it fails. The reason for failure is the sql query generated is as under:
select...from...where...
and...
and (reverse(substring(reverse(**namedinfos2_.info**), 0, CHARINDEX('#')) in (?))
and...
Note that after the '#' it is missing the namedinfos2_.info again. Hence the CHARINDEX() function fails, as it expects 2 parameters.
It should have been the below:
and...
and (reverse(substring(reverse(**namedinfos2_.info**), 0, CHARINDEX('#', **namedinfos2_.info**)) in (?))
and---
Any idea why does this happen? Or what I should be doing to fix this?
The actual query is quite long.
An Example can be like this:
HQL:
select LastModifiedByUser.field
from namedinfo LastModifiedByUser
where REVERSE(SUBSTRING(REVERSE(LastModifiedByUser.info),0, CHARINDEX('#', REVERSE(LastModifiedByUser.info)))) in (:var1_0,:var1_1)
Generated SQL by Hibernate
select namedinfos2_.field
form namedinfo namedinfos2_
where (reverse(substring(reverse(namedinfos2_.info), 0, CHARINDEX('#')) in (?))
Note- the missing LastModifiedByUser.info conversion in the CHARINDEX method (2nd parameter).
Could you remove the CHARINDEX function? I was not able to found it the expressions functions section, so maybe the engine is not able to translate it correctly.
Also, you are allowed to use native SQL, so you can try building simple CHARINDEX statement and run it in that way.
Also, you can try to use LOCATE as alternative of what you are doing.
I have made a workaround by making a custom function say functionXyz() that takes input string and internally just returns something like the below.
return reverse(substring(reverse(namedinfos2_.info), 0, CHARINDEX('#'))
That is making it work, just that I everytime need to call this function. Not sure if it has an impact on performance.

a function calculate has been used in a true/false expression that is used as a table filter expression which is not allowed

I have a to check the current month and current year value for a measure in ssas tabular model but due to type mismatch i am not able to do this. For this, i have created a measure in which i am using this dax query
:CurrMonthYear:=CONCATENATE(CONCATENATE("""",concatenate(year(now()),CONCATENATE(0,month(now())))),"""") output: "201704"
...to calculate currentyear and currentmonth. But when i give this value in a measure like this:
SumOfRevisedForecast:=CALCULATE(SUM(DimRevisedForecast[RevisedForecast]),DimRevisedForecast[Approved] <>"N" && DimRevisedForecast[Approved] <>blank(),'DimRevisedForecast'[CalendarYearMonth] =CurrMonthYear)
...this doesnt work. Though, giving "201704" in place of CurrMonthYear works.
Can anybody help me in this?
Thanks in advance
The problem is not with CurrMonthYear measure, it's with your second formula - CALCULATE function does not accept measures as criteria, only values. That's why it works when you put "201704" explicitly but fails when you use the measure.
A common solution is to wrap the criteria into FILTER function, something like:
CALCULATE(SUM(DimRevisedForecast[RevisedForecast]),
FILTER ('DimRevisedForecast',
'DimRevisedForecast'[CalendarYearMonth] = [CurrMonthYear])
A great explanation of this issue is here:
FILTER() – When, Why, & How to Use It

strdist function stopped recognizing field reference in SOLR 5

I remember in previous SOLR version (4.x) I was able to run the following query:
"(lastName:HILL)"
with fields
"*,score, strDistLastName:$lnamestrdist"
and with raw query params
"lnamestrdist=strdist('HILL',lastName,jw)"
This would give me result with an additional field which is result of strdist function using Jaro Winkler algorithm between a value and returned field.
For some reason in SOLR version 5.1 it always returns 0, even if strings match 1 to 1 (i.e. strdist should be 1).
I have checked it without using variables, i.e. only specifying fields as
"*,score, strdist('HILL',lastName,jw)"
but it also returns 0.
And only when I use another string literal like below, it returns 1:
"*,score, strdist('HILL','HILL',jw)"
I assume it means that strdist does not recognize fields anymore. Does anyone know why? Maybe syntax has changed or it's simply a bug?
Thank you very much in advance!
My bad, I have "solr.LowerCaseFilterFactory" filter in index/query analyzers for the "lastNameExact" field, therefore value for strdist for 'HILL' and 'hill' was 0. After I corrected fields list to "*,score, strdist('hill',lastName,jw)", strdist became 1.
Sorry for the confusion :)

Use array formula with index match

Is it possible to do an array formula with index match:
e.g:
=arrayformula(if(len(A3:A),INDEX('SheetB'!E:E,MATCH(A3:A,'SheetB'!H:H,0))))
If not, is there a solution that doesn't involve google scripts?
It seems INDEX can not return multiple values. It can not be used inside ARRAYFORMULA.
The only solution I know of is to use VLOOKUP.
See this thread :
https://productforums.google.com/forum/#!topic/docs/jVvjbz8u7A8
Example from there :
=ArrayFormula(VLOOKUP( B12:B15; H2:R32; 1; TRUE))
Cheers!
Use XLOOKUP
=ARRAYFORMULA(XLOOKUP(K2:K,R2:R,S2:S))
With XLOOKUP you can look in one column for a search term, and return a result from the same row in another column, regardless of which side the return column is on
In the OP's specific case, one can actually use VLOOKUP for its intended purpose, as a replacement for MATCH:
=arrayformula(if(len(A3:A),VLOOKUP(A3:A,{SheetB!E:E,SheetB!H:H},2,false)))
In the general case of trying to use INDEX to retrieve multiple values, it can be replaced with a kludge of VLOOKUP and SEQUENCE:
=arrayformula(VLOOKUP(A:A,{SEQUENCE(rows(B:B)),B:B},2,true))
does what would have been accomplished by
=arrayformula(INDEX(B:B,A:A))
if the latter worked as OP expected.
I know this is old now, but it turns out that INDEX() acts as a defacto ARRAYFORMULA() now. You can see a fabulous example of this on this google sheet, which shows how to use a and index(split()) to extract a particular set of text from a cell. The MK.demo tab provides a visual on how the array formula is implied with the INDEX() function.
Nowadays, using a FILTER() or QUERY() function can give the kinds of multiple vlookup the OP was looking for.
im not sure if its gonna work but i did an "IF(ISBLANK() before the INDEX(...) in the ARRAYFORMULA and it went down all the way

Why do I get a SQL syntax error with this?

Trying to run this query in LINQPad 4:
SELECT item_group_id as AccountID, IIF(ISNULL(t_item_group.description),'[blank]',t_item_group.description) AS Name
FROM t_item_group
WHERE active = TRUE
I get, "the isnull function requires 2 argument(s)."
I've tried moving the parens around, changing the "[blank]" to "[blank]" and "[blank]" , but none of it helps...
The queries (I have two similar ones (with IIF(ISNULL)) that LINQPad won't run for this reason, yet they run in actuality (in my Web API app) fine; so, LINQPad is more "picky" than it needs to be, perhaps, but what is it expecting, SQL syntax-wise?
ISNULL is already like a 'if' type statement.
You can just replace
IIF(ISNULL(t_item_group.description),'[blank]',t_item_group.description)
with
ISNULL(t_item_group.description, '[blank]')
The ISNULL uses the first parameter (the 'description'), unless that value is null in which case it will use the second parameter.
As an aside, one of the reasons I don't care for ISNULL is that it is poorly named. You'd assume that given its name it will return a bit - true if the parameter is null, false if not null - which you could use in an 'if' statement like you attempted. But that's not how it works.
The alternative is to use COALESCE. It provides much the same functionality, but the naming makes sense.
co·a·lesce ˌkōəˈles verb
1. come together and form one mass or whole.
To COALESCE two parameters is to force them into one non-nullable result. And the function is actually more powerful, as you can provide multiple parameters - COALESCE(i.description, i.name, '[blank]') is perfectly valid.

Resources