AND and OR Condition in Thinking Sphinx - sphinx

Is it possible to do something like an AND condition with OR?
This is my simple sphinx query with AND only...
Job.search('', {with: {canonical_type: Zlib::crc32('SubCategory')}, conditions: { sub_category_ids: [4]}})
Sphinx Query (22.8ms) SELECT * FROM `job_core`, `job_delta` WHERE MATCH('#sub_category_ids [4]') AND `canonical_type` = 1916160457 AND `sphinx_deleted` = 0 LIMIT 0, 20 OPTION max_matches=50000
Sphinx Found 1 results
I'd like to add an OR... something like...
WHERE (MATCH('#sub_category_ids [4]') AND `canonical_type` = 1916160457 OR MATCH('#sub_category_ids [4]') AND `canonical_type` = 4282022807)
so that I can search for IDs with different canonical_type.
Not sure how to do it in Thinking Sphinx.
Thanks!

This is actully a limitation of Sphinx itself. It doesnt support OR in WHERE (nor nesting) - which is why it not in thinking-sphinx.
But
WHERE (MATCH('#sub_category_ids [4]') AND `canonical_type` = 1916160457
OR MATCH('#sub_category_ids [4]') AND `canonical_type` = 4282022807)
could be written as SphinxQL:
WHERE MATCH('#sub_category_ids [4]') AND `canonical_type` IN (1916160457,4282022807)
... ie IN() operator is kinda like 'OR'. Wouldn't be surprised if thinkinx-sphinx does it automatically with a array
with: {canonical_type: [Zlib::crc32('SubCategory1'), Zlib::crc32('SubCategory2')] }
based on http://freelancing-gods.com/thinking-sphinx/searching.html#filters

Related

SphinxQL / Sphinx Search: Query to find rows where columns are equal (category1 = category2)?

SphinxSearch: 3.3.1
Ubuntu 22.04
PHP 8.1.12
I am trying to use SphinxQL with PHP to find rows where two different categories match, such as:
$db = new PDO('mysql:host=127.0.0.1;port=9306', '', '');
$sql = "SELECT id FROM index,delta,rtindex WHERE category2 > 0 AND category2 = category1 LIMIT 1000 OPTION max_matches=1000";
$stmt = $db->query($sql);
However, I am receiving a syntax error whenever I do this. I thought this query had worked in the past, but I'm just now noticing it in the logs. Is this the correct way to do it, or is there some other way dictated by sphinx's rules?

query error: no field 'face' found in schema

i have bigint column named as face in mysql. and this is my sphinx.conf
source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = pass
sql_db = nums
sql_port = 3306 # optional, default is 3306
sql_query = SELECT id,id AS id_attr,tel,name,sex,face from tel
sql_attr_uint = id_attr
sql_attr_bigint = face
}
index num
{
rt_attr_bigint = face
rt_field = face
source = src1
path = C:/sphinx/bin/data/numaralar
}
i can make search by name and tel but not with face.
Fatal error: Uncaught exception 'Foolz\SphinxQL\Exception\DatabaseException' with message '[1064] index nums: query error: no field 'face' found in schema [ SELECT * FROM nums WHERE MATCH('(#face 123456)') LIMIT 0, 10 OPTION max_matches = 5000;SHOW META]' in ..
why may it be?
You are trying to use the value as an field. The # fulltext operator (and indeed the whole of MATCH() full text query, operates on fields ONLY.
You've instead defined face as an atribute. Attributes don't work in full-text queries.
Can
Make face a field instead (remove the sql_attr_bigint) or make it both an attribute and field. (to do that, would have to duplicate it like you've duplicated the id, one for field, one for attribute. or use sql_field_string, but that makes a string attribute)
or
Use filter by the attribute instead. Dont really know how to do that in Foolz. But the SphinxQL query would be something like
SELECT * FROM nums WHERE `face` = 123456 LIMIT 0, 10

SELECT with substring in ON clause?

I have the following select statement in ABAP:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
INTO corresponding fields of table GT_INSTMUNIC_F
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN EVER AS EV on
MUNIC~POD = EV~VREFER(9).
"where EV~BSTATUS = '14' or EV~BSTATUS = '32'.
My problem with the above statement is that does not recognize the substring/offset operation on the 'ON' clause. If i remove the '(9) then
it recognizes the field, otherwise it gives error:
Field ev~refer is unknown. It is neither in one of the specified tables
nor defined by a "DATA" statement. I have also tried doing something similar in the 'Where' clause, receiving a similar error:
LOOP AT gt_instmunic.
clear wa_gt_instmunic_f.
wa_gt_instmunic_f-mandt = gt_instmunic-mandt.
wa_gt_instmunic_f-bis = gt_instmunic-bis.
wa_gt_instmunic_f-ab = gt_instmunic-ab.
wa_gt_instmunic_f-zzelecdate = gt_instmunic-zzelecdate.
wa_gt_instmunic_f-ZZCERTDATE = gt_instmunic-ZZCERTDATE.
wa_gt_instmunic_f-CONSYEAR = gt_instmunic-CONSYEAR.
wa_gt_instmunic_f-ZDIMO = gt_instmunic-ZDIMO.
wa_gt_instmunic_f-ZZONE_M = gt_instmunic-ZZONE_M.
wa_gt_instmunic_f-ZZONE_T = gt_instmunic-ZZONE_T.
wa_gt_instmunic_f-USAGE_M = gt_instmunic-USAGE_M.
wa_gt_instmunic_f-USAGE_T = gt_instmunic-USAGE_T.
temp_pod = gt_instmunic-pod.
SELECT vrefer
FROM ever
INTO wa_gt_instmunic_f-vrefer
WHERE ( vrefer(9) LIKE temp_pod ). " PROBLEM WITH SUBSTRING
"AND ( BSTATUS = '14' OR BSTATUS = '32' ).
ENDSELECT.
WRITE: / sy-dbcnt.
WRITE: / 'wa is: ', wa_gt_instmunic_f.
WRITE: / 'wa-ever is: ', wa_gt_instmunic_f-vrefer.
APPEND wa_gt_instmunic_f TO gt_instmunic_f.
WRITE: / wa_gt_instmunic_f-vrefer.
ENDLOOP.
itab_size = lines( gt_instmunic_f ).
WRITE: / 'Internal table populated with', itab_size, ' lines'.
The basic task i want to implement is to modify a specific field on one table,
pulling values from another. They have a common field ( pod = vrefer(9) ). Thanks in advance for your time.
If you are on a late enough NetWeaver version, it works on 7.51, you can use the OpenSQL function LEFT or SUBSTRING. Your query would look something like:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN ever AS ev
ON MUNIC~POD EQ LEFT( EV~VREFER, 9 )
INTO corresponding fields of table GT_INSTMUNIC_F.
Note that the INTO clause needs to move to the end of the command as well.
field(9) is a subset operation that is processed by the ABAP environment and can not be translated into a database-level SQL statement (at least not at the moment, but I'd be surprised if it ever will be). Your best bet is either to select the datasets separately and merge them manually (if both are approximately equally large) or pre-select one and use a FAE/IN clause.
They have a common field ( pod = vrefer(9) )
This is a wrong assumption, because they both are not fields, but a field an other thing.
If you really need to do that task through SQL, I'll suggest you to check native SQL sentences like SUBSTRING and check if you can manage to use them within an EXEC_SQL or (better) the CL_SQL* classes.

UPDATE with Aggregate SELECT - SET columns 0 when SELECT is empty

I have the following UPDATE statement
UPDATE stuff
SET stuff.total = t.total
FROM (
SELECT SUM(price) FROM things WHERE stuff_id = ? GROUP BY stuff_id
) t
WHERE stuff.id = ?
This works fine when there are actually rows in things, but when not no UPDATE is executed (which I guess makes sense). What would be an elegant way to set stuff.total to 0 in that case? I'd like to do it in one query.
I already tried SET stuff.total = coalesce(t.total, 0) but it had no effect.
You haven't used coalesce in the right place. Also, GROUP BY can be omitted. Try this query:
UPDATE stuff
SET stuff.total = t.total
FROM (
SELECT coalesce(SUM(price), 0) FROM things WHERE stuff_id = ?) t
WHERE stuff.id = ?

JPA Native Query delete in

I try to delete a list of rows from a table using this Native Query:
#NamedNativeQuery(name="WebGroup.DeleteIn",
query="DELETE FROM WebGroup WHERE
WebGroup.GROUP_ID IN (:IDsList)"
getEm().createNamedQuery("WebGroup.DeleteIn")
.setParameter("IDsList", groupToDeleteIDs)
.executeUpdate();
and this is the SQL that MySQL executes:
DELETE FROM WebGroup WHERE WebGroup.GROUP_ID IN (:IDsList)
SO, JPA doesn't replace the variable IDsList...
Some one could help me please?
One way that works is if you not use the id value like you tried, but instead use the entity and let JPA handle the identification of it like this:
HashSet<Transaction> transactions = new HashSet<Transaction>();
...
entityManager.createQuery(
"DELETE FROM Transaction e WHERE e IN (:transactions)").
setParameter("transactions", new ArrayList<Transaction>(
transactions)).executeUpdate();
Hope it helps you in the right direction.
native queries do not support collection expansion neither named parameters.
you should write:
#NamedNativeQuery(name="WebGroup.DeleteIn", query="DELETE FROM WebGroup WHERE WebGroup.GROUP_ID IN (?,?,?,?)"
Query query = getEm().createNamedQuery("WebGroup.DeleteIn");
for(int i = 0; i < 4; i++) query.setParameter(i + 1, groupToDeleteIDs.get(i));
query.executeUpdate();
but it is horrible
on eclipselink + mysql this one works:
#NamedNativeQuery(name="WebGroup.DeleteIn", query="DELETE FROM WebGroup WHERE WebGroup.GROUP_ID IN (?)"
Query query = getEm().createNamedQuery("WebGroup.DeleteIn");
query.setParameter(1, StringUtils.join(groupToDeleteIDs, ",");
query.executeUpdate();
however it is not very nice...
but there isn't any other solution using a named query.