Text case-insitive search with crate.io SQL - sql

What is the proper SQL syntax to search an array text in the crate database?
My example table is:
create table
tasks(user string, entry array(object as (taskid string, eTime timestamp))).
I tried the following which give a syntax error:
select * from program where any(entry['taskid']) ~* '.*cleanup.*';

The correct syntax for the ANY operator would be:
SELECT * FROM tasks WHERE '.*cleanup.*' ~* ANY(entry['taskid']);
However, PCRE are currently not supported in combination with ANY. An alternative would be the LIKE predicate, but that is not case-insensitive (and can be quite slow if it starts with a wildcard character);
So ultimately, you could ...
... either use a fulltext index on the entry['taskid'] column with a lowercase analyzer (which is probably not the best solution, because I assume taskid is a single word and you want to use it "as is" also),
... or split up the array values into separate rows so you have a schema like:
CREATE TABLE tasks (
user string,
entry OBJECT AS (
taskid STRING,
etime TIMESTAMP
)
) ...
The you can use
SELECT * FROM tasks WHERE entry['taskid'] ~* '.*cleanup.*';

Related

Querying whole OBJECT or Document using LIKE condition in CrateDB

I have a table with one of the field defined as "attributes" OBJECT (DYNAMIC).
Now, my usecase is to be able to check if any particular string is part of this OBJECT. In SQL terms I want to execute a like command on this whole OBJECT or even whole Document. Do we currently support this feature
Query I want to execute: select * from gra.test_table where attributes like '%53.22.232.27%';
When I execute this on Crate 4.5.1, I am running into error: UnsupportedFeatureException[Unknown function: (gra.test_table.attributes LIKE '%53.22.232.27%'), no overload found for matching argument types: (object, text). Possible candidates: op_like(text, text):boolean]
When I execute this on Crate 3.x, I am running into error:
SQLActionException[SQLParseException: Cannot cast attributes to type string]
Table structure is below and attributes is the field I am talking about
CREATE TABLE IF NOT EXISTS "test"."test_table" (
"accountname" STRING,
"attributes" OBJECT (DYNAMIC) AS (
"accesslist" STRING,
"accesslistid" STRING,
"accessmask" STRING,
"accessreason" STRING
),
"employeeid" STRING,
"day" TIMESTAMP,
PRIMARY KEY ("day", "id"),
INDEX "all_columns_ft" USING FULLTEXT ("employeeid") WITH (
analyzer = 'standard'
)
)
CLUSTERED INTO 1 SHARDS
PARTITIONED BY ("day")
WITH (
"allocation.max_retries" = 5
)
Using like on a whole object is not supported.
A possible (slow) workaround could be to use a regular expression operator on the text cast of an object typed column:
select * from gra.test_table where attributes::text ~ '.*53.22.232.27.*'
But be aware that through the cast, no index could be utilized and such a full table scan + filter is executed resulting in slow query execution.

MariaDB MATCH AGAINST with single quote search term?

I'm trying to find a working query for using MATCH AGAINST while having a search term containing a single quote.
Example data in the database table:
I'm a freak
Example search term:
I'm
Search queries I tried:
SELECT * FROM table WHERE MATCH (name) AGAINST ('"I\'m"' IN NATURAL LANGUAGE MODE);
SELECT * FROM table WHERE MATCH (name) AGAINST ('"I\'m"' IN BOOLEAN MODE);
SELECT * FROM table WHERE MATCH (name) AGAINST ('I\'m*' IN BOOLEAN MODE);
SELECT * FROM table WHERE MATCH (name) AGAINST ('(I\'m)*' IN BOOLEAN MODE);
...and many more. Nothing is working.
I'm using MariaDB 10.1.33.
Any ideas?
I'm pretty sure contractions are not treated as words.
Instead the apostrophe is treated as a word separator giving you "I" and "m".
But you probably don't have innodb_ft_min_token_size=1, so those two "words" are ignored.
There are limitations of FT; you have encountered one of them.

sqlite3 with FTS4 table: Query returns wrong row

I have a weird issue with my FTS4 index in SQLite3, namely that a MATCH query for one term returns not the exact match but another, similar one, and vice versa.
Here is an example to illustrate it better:
SELECT name FROM test_idx WHERE name MATCH 'lehmbruck-museum';
-- "Lehmbruck-Archiv"
SELECT name FROM test_idx WHERE name MATCH 'lehmbruck-archiv';
-- "Lehmbruck-Museum"
It seems to have something to do with the dash, here is a similar case that exhibits the same behavior:
SELECT name FROM test_idx WHERE name MATCH 'some-thing';
-- "some-thang"
SELECT name FROM test_idx WHERE name MATCH 'some-thang';
-- "some-thing"
Here is how this test database is built, in case somebody wants to have a go at reproducing it:
CREATE VIRTUAL TABLE test_idx USING fts4(name);
INSERT INTO test_idx (name) VALUES
('some-thing'), ('some-thang'),
('Lehmbruck-Museum'), ('Lehmbruck-Archiv');
SELECT name FROM test_idx WHERE name MATCH 'lehmbruck-museum';
What you pass to MATCH here is a full text query expression. The - character is a unary operator in that expression language that is a stand in for the NOT set operation, and is certainly giving you your unexpected results. Notably - the exact opposite of what you expect! Of course, it is finding exactly what the query is instructed to find - the string lehmbruck and NOT museum at the end!
You'll need to escape it to get the results you want - or perhaps employ the LIKE operator if you are looking at a single column in a table.
Some more information on this expression language can be found in section 3 of the FTS3 and FTS4 documentation on the SQLite doc site here.

SQL Server 2012 query string containing slash

I've looked all over the internet and stackoverflow for an answer to this question, but can't seem to find anything that answers it so here goes...
Is there anything special that needs to be done to a forward slash (/) for them to be included in search queries?
Scenario: I have a query that contains a string being searched for that includes a forward slash. The search term is a dimension of a particular item so it must contain the slash to indicate a fraction. I've tried escaping it with a backslash, but that doesn't work. The query is as follows:
SELECT * FROM ITEMDATA WHERE CONTAINS(dimensions, '3/8')
This query returns 0 results.
Example of data to be searched on:
•Pitch: 3/8"
•Gauge: .050
I also need to get the double quote in the search phrase to specify units, but that's another problem.
Any suggestions?
As the following demonstrates, you can use "/" in a LIKE expression without any tricks:
create table foo(a int, b varchar(32))
go
insert into foo select 1, 'for 3/8 inch';
go
select * from foo where b like '%3/8%';
go
This will result in the single row being found.
If we need to search in sql table column with a particular string, we need to use LIKE command, so that we can use it like in various ways.
1. Returns results having dimensions column value contains the string 3/8
'SELECT * FROM ITEMDATA WHERE dimensions LIKE '%3/8%';`
Returns results having dimensions column value strictly starts with the string 3/8
SELECT * FROM ITEMDATA WHERE dimensions LIKE '3/8%';
Returns results having dimensions column value strictly ends with the string 3/8.
SELECT * FROM ITEMDATA WHERE dimensions LIKE '3/8%';
Disable STOPLIST for full-text:
ALTER FULLTEXT INDEX ON table SET STOPLIST OFF
and try
SELECT * FROM ITEMDATA WHERE CONTAINS (dimensions, '"3/8*"')

Finding the "&" character in SQL SERVER using a like statement and Wildcards

I need to find the '&' in a string.
SELECT * FROM TABLE WHERE FIELD LIKE ..&...
Things we have tried :
SELECT * FROM TABLE WHERE FIELD LIKE '&&&'
SELECT * FROM TABLE WHERE FIELD LIKE '&\&&'
SELECT * FROM TABLE WHERE FIELD LIKE '&|&&' escape '|'
SELECT * FROM TABLE WHERE FIELD LIKE '&[&]&'
None of these give any results in SQLServer.
Well some give all rows, some give none.
Similar questions that didn't work or were not specific enough.
Find the % character in a LIKE query
How to detect if a string contains special characters?
some old reference Server 2000
http://web.archive.org/web/20150519072547/http://sqlserver2000.databases.aspfaq.com:80/how-do-i-search-for-special-characters-e-g-in-sql-server.html
& isn't a wildcard in SQL, therefore no escaping is needed.
Use % around the value your looking for.
SELECT * FROM TABLE WHERE FIELD LIKE '%&%'
Your statement contains no wildcards, thus is equivalent to WHERE FIELD = '&'.
& isn't a special character in SQL so it doesn't need to be escaped. Just write
WHERE FIELD LIKE '%&%'
to search for entries that contain & somewhere in the field
Be aware though, that this will result in a full table scan as the server can't use any indexes. Had you typed WHERE FIELD LIKE '&%' the server could do a range seek to find all entries starting with &.
If you have a lot of data and can't add any more constraints, you should consider using SQL Server's full-text search to create and use and FTS index, with predicates like CONTAINS or FREETEXT