Solr search by result of division of two fields - search

is it possible to search in Solr by a result of a mathematical function between values stored in two fields?
For example:
?fq=FIELDA:{!func}div(FIELDA, FIELDB)
or:
?fq=FIELDA:[* TO 100/FIELDB]
I appreciate greatly your suggestions.

Yes. You may use Frange query
For example: (note: the idea is to show an example rather than provide a well thought formula)
fq={!frange l=0 u=0} sub(div(FIELDA, FIELDB),FIELDA)
q={!frange l=10 u=10} div(FIELDA,1) # this will search for 10 in FIELDA

Related

Ignore numeric values in string, Presto

I have a column in a database which is all postcodes. I want to use that column to get statistical data about specific regions. To do this, I want to extract just the first non numeric characters of the postcode (B for Birmingham, BT for Belfast).
I can see solutions in other SQL formats using a CASE WHEN with ISNUMERIC but that function doesn't work in Presto. Are there any solutions to this?
As always, any advice would be greatly appreciated.
Many thanks,
Barry
I think you'll want to look at using regular expression functions to either extract the non-numeric characters using regexp_extract or replace all non-numeric characters using regex_replace.
See https://prestodb.io/docs/current/functions/regexp.html
I managed to get around this using
select concat(substr(postcode,1,1), case when substr(postcode,2,1) in ('1','2','3','4','5','6','7','8','9','0') then '' else substr(postcode,2,1) end)

Why isn't isnumber search not working correctly with given data?

I am trying to get Excel to search for a series of keywords in title using the ISNUMBER() search function, but it's bringing back all FALSE, which is wrong. Any help would be greatly appreciated:
I made sure I F4'd the item list -- still wrong
=ISNUMBER(SEARCH(R2,$G$2:$G$22))
Use MATCH:
=ISNUMBER(MATCH("*"&R2&"*",$G$2:$G$22,0))
With the Search set up the way you have it, it will only return the value of the first in the list. You can wrap SUMPRODUCT around the formula:
=SUMPRODUCT(--ISNUMBER(SEARCH(R2,$G$2:$G$22)))>0
But that is more intensive then the simple MATCH.
This assumes that the Keyword that is being looked-up is in R2 and the range $G$2:$G$22 us the list of titles.
If it is the other way around then the SEARCH is backwards and should be:
=SUMPRODUCT(--ISNUMBER(SEARCH($G$2:$G$22,R2)))>0

azure search exact match

I have a table with a lot of data. One field is a string for example
searchableField
row 1: abcdefgdefg1hijklmnopqrstuvw234234
row 2: abcdefgdefg1hijklmnopqrstuvw2dsfds33
row 3: abcdefgdefg1hijklmnopqrstuvw234234
row 4:
abcdefgdefg1hijklmnopqrstuvwweewere333wr
row 5:
abcdefgdefg2hijklmnopqrstuvw234222aadfff
row 6:
abcdefgdefg1hijklmnopqrstuvwdsfdsf
I only want result row 5 back, but adding search therm defg2 won't work.
In some other cases I want onl y result 1, 2, 3, 4, 6 back. but also searching on defg1 won't work for me.
Something that should work for me is a filter, but unfortunately there are no filters with contains. What can I do as work around?
Please read the How full text search works in Azure Search article. It will help you understand how your documents and query terms are processed and how to customize the behavior of your search index to achieve the results you want.
In your case, you might want to create a custom analyzer that will break up the long terms in your document into smaller ones that are likely to be used as query terms by users of your application.
Alternatively, you can issue a wildcard or a regex query using the Lucene query language to simulate the contains behavior you're looking for. More information here: Azure search, search by partial terms
Below lucene query will help for doing a like or contains search as above question
item : /.* defg2 .*/
you can use search.ismatch or search.ismatchscoring functions
ex:
"filter": "search.in(metadata_library, 'a3e9838f-3fec-49d8-a1ea-46f361238ffd') and search.ismatch('[exe pixel!][test new tags 102][css monitor]', 'metadata_tags','simple','all')",

Playframework: Dynamic Search Fields

I need to implement a search dialog within play but don't know to solve this.
I have 3 integer fields in my database and want to search each of them with min and max values:
select * from office
where maxSeats <= maxParamFromPage
and maxSeats >= minParamFromPage
...
All fields are optional so if a user only enters the minParamFromPage all offices should be listed which are higher than this param. Now I have 3 params like maxSeats and I need to buildup my query dynamically based on the input parameters. I thought about replacing them with "0" or null when those are not entered but this is placed one to one in the sql query.
Can somebody help me on this?
Thank you!
You should used JPA Criteria API to construct your criterion based on the form values.
Refer this example

Quick SQL question

Working on postgres SQL.
I have a table with a column that contains values of the following format:
Set1/Set2/Set3/...
Seti can be a set of values for each i. They are delimited by '/'.
I would like to show distinct entries of the form set1/set2 and that is - I would like to trim or truncate the rest of the string in those entries.
That is, I want all distinct options for:
Set1/Set2
A regular expression would work great: I want a substring of the pattern: .*/.*/
to be displayed without the rest of it.
I got as far as:
select distinct column_name from table_name
but I have no idea how to make the trimming itself.
Tried looking in w3schools and other sites as well as searching SQL trim / SQL truncate in google but didn't find what I'm looking for.
Thanks in advance.
mu is too short's answer is fine if the the lengths of the strings between the forward slashes is always consistent. Otherwise you'll want to use a regex with the substring function.
For example:
=> select substring('Set1/Set2/Set3/' from '^[^/]+/[^/]+');
substring
-----------
Set1/Set2
(1 row)
=> select substring('Set123/Set24/Set3/' from '^[^/]+/[^/]+');
substring
--------------
Set123/Set24
(1 row)
So your query on the table would become:
select distinct substring(column_name from '^[^/]+/[^/]+') from table_name;
The relevant docs are http://www.postgresql.org/docs/8.4/static/functions-string.html
and http://www.postgresql.org/docs/8.4/static/functions-matching.html.
Why do you store multiple values in a single record? The preferred solution would be multiple values in multiple records, your problem would not exist anymore.
Another option would be the usage of an array of values, using the TEXT[] array-datatype instead of TEXT. You can index an array field using the GIN-index.
SUBSTRING() (like mu_is_too_short showed you) can solve the current problem, using an array and the array functions is another option:
SELECT array_to_string(
(string_to_array('Set1/Set2/Set3/', '/'))[1:2], '/' );
This makes it rather flexible, there is no need for a fixed length of the values. The separator in the array functions will do the job. The [1:2] will pick the first 2 slices of the array, using [1:3] would pick slices 1 to 3. This makes it easy to change.
If they really are that regular you could use substring; for example:
=> select substring('Set1/Set2/Set3/' from 1 for 9);
substring
-----------
Set1/Set2
(1 row)
There is also a version of substring that understands POSIX regular expressions if you need a little more flexibility.
The PostgreSQL online documentation is quite good BTW:
http://www.postgresql.org/docs/current/static/index.html
and it even has a usable index and sensible navigation.
If you want to use .*/.* then you'd want something like substring(s from '[^/]+/[^/]+'), for example:
=> select substring('where/is/pancakes/house?' from '[^/]+/[^/]+');
substring
-----------
where/is
(1 row)

Resources