Sphinx / SphinxQL - Setting weights on select - mysql

I'm using Sphinx, and I'm trying to execute the next query in mysql:
SELECT *
FROM `index_efemerides`, `index_programas`, `index_guias_para_el_aula`, `index_producciones_interactivas`
WHERE MATCH('(#(titulo,descripcion,etiquetas) nuevo)')
OPTION `field_weights` = (index_programas.titulo=100, index_programas.etiqueta=15, index_programas.descripcion=5, index_producciones_interactivas.titulo=53, index_producciones_interactivas.etiqueta=13, index_producciones_interactivas.descripcion=5, index_guias_para_el_aula.titulo=52, index_guias_para_el_aula.etiqueta=12, index_guias_para_el_aula.descripcion=5, index_efemerides.titulo=51, index_efemerides.etiqueta=11, index_efemerides.descripcion=5)
But I'm getting the next error msg:
sphinxql: syntax error, unexpected SUBKEY, expecting '=' near '.titulo=100, index_programas.etiqueta=15, index_programas.descripcion=5, index_producciones_interactivas.titulo=53, index_producciones_interactivas.etiqueta=13, index_producciones_interactivas.descripcion=5, index_guias_para_el_aula.titulo=52, index_guias_para_el_aula.etiqueta=12, index_guias_para_el_aula.descripcion=5, index_efemerides.titulo=51, index_efemerides.etiqueta=11, index_efemerides.descripcion=5)'
If I remove the dots it seems to work (not really sure because if i invent a field name, it doesn't show me any error). But i need to set the field weights different per table.

I've just found index_weight property. A possibility is to complement index_weight with field_weights like this:
SELECT * FROM `index_efemerides`, `index_programas`, `index_guias_para_el_aula`, `index_producciones_interactivas` WHERE MATCH('(#(titulo,descripcion,etiquetas) nuevo)') OPTION `field_weights` = (titulo=100, etiqueta=15, descripcion=5), `index_weights` = (index_programas=100, index_guias_para_el_aula=50, index_efemerides=75, index_producciones_interactivas=25);
Not pretty sure of the resulting weight.

Related

How to remove extra apostrophe

I wrote a SQL query to find the desired output for my project. I was working fine with the correct output. But suddenly it started to give error and in the SQL query, there is some additional apoatrophe in. How to resolve it?
I tried to add the query to $this->db->query(); but still no use.
public function getStudentConut($id) {
$this->db->select('students.id')
->from('students')
->join('bp','students.pbp = bp.id','left')
->where(condition 1)
->where(condition 2);
$query1 = $this->db->get_compiled_select();
$this->db->select('students.id')
->from('students')
->join('bp','students.dbp = bp.id','left')
->where(condition 1)
->where(condition 2);
$query2 = $this->db->get_compiled_select();
$this->db->select('COUNT(id) as stud_count')
->from('('.$query1." UNION ALL ".$query2.') X')
->group_by('X.id');
$results = $this->db->get();
return $results->num_rows();
}
It was giving correct count earlier. But without any new changes, it started to give the error.
Now I get error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.id`` WHERE ``bp.some_value`` IS NULL AND ``students.`schoo' at line 2
SELECT COUNT(id) as stud_count FROM (SELECT students.id`` FROM ``students`` LEFT JOIN ``bp`` ON ``students.pbp`` = ``bp.id`` WHERE ``bp..Some other condition.. UNION ALL SELECT students.idFROMstudentsLEFT JOINbpONstudents.dbp=bp.id..some other condition....) X GROUP BYX.id`
I think the issue (at least with the double `) is that CodeIgniter isn't very good with subqueries and such. Basically every time you get the compiled select statement it already has the escape identifiers and then you are putting it in the from statement at the end which will add additional escape identifiers on top of that.
`->from('('.$query1." UNION ALL ".$query2.') X')`
Unfortunately, unlike other methods like set, from doesn't have a 2nd parameter that allows you to set escaping to false (which is what I think you need).
I suggest trying this:
$this->db->_protect_identifiers = FALSE;
$this->db->select('COUNT(id) as stud_count')
->from('('.$query1." UNION ALL ".$query2.') X')
->group_by('X.id');
$results = $this->db->get();
$this->db->_protect_identifiers = TRUE;
and also look in to this: ->where(condition 2); which I'm pretty sure shouldn't compile due to lack of quotes. You probably don't want this escaped so you can do ->where('condition 2', '', false); as per: https://www.codeigniter.com/user_guide/database/query_builder.html#CI_DB_query_builder::where
When all else fails, just know that CodeIgniter has some limitations with "advanced" queries and that maybe you should write it out manually as a string utilizing $this->db->escape_str(...) for escaping user input vars, and $this->db->query(...) to run the SQL.

DRUPAL PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

I do not know what I am doing wrong here? Can someone please help me? When the following query is executed in Drupal7 custom module, I get the following error:
ERROR:
ResponseText: PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens:
SELECT t.tid AS tid, t.name AS name
FROM
{taxonomy_term_data} t
WHERE (t.vid = :vid) AND (LOWER(t.name) LIKE LOWER('%%:last_string%%'))
LIMIT 50 OFFSET 0;
Array
(
[:vid] => 6
[:last_string] => server
)
CODE:
$result = db_select('taxonomy_term_data', 't')
->fields('t', array('tid', 'name'))
->where('t.vid = :vid', array(':vid'=>$vid))
->where("LOWER(t.name) LIKE LOWER('%%:last_string%%')", array(':last_string'=>$last_string))
->range(0, $num_results)
->execute();
The query works if I directly hard code the value for :last_string,
Example:
->where("LOWER(t.name) LIKE LOWER('%%server%%')")
any help is much appreciated..
Try using only one % because: % is a substitute for zero or more characters. You don't need 2 of them.
The LOWER function takes a string as parameter and '%:last_string%' is taken as string not as a binding to the array(':last_string'=>$last_string), that's why it works when you remove the binding. So try to not put :last_string inside the LOWER function because it won't recognize it as a binding.

Flask - Convert request.args to dict

I've been trying to figure out how to pass the request.args to sqlalchemy filter.
I thought this should work:
model.query.filter(**request.args).all()
But it's throwing the error:
TypeError: <lambda>() got an unexpected keyword argument 'userid'
When userid or any other get arg is present.
According to this post - https://stackoverflow.com/questions/19506105/flask-sqlalchemy-query-with-keyword-as-variable - you can pass a dict to the filter function.
Any ideas what I'm doing wrong?
Many thanks :)
UPDATE: Many thanks to the poster below, however now it's throwing the following error:
ProgrammingError: (ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY tblclients.clientname' at line 3") 'SELECT favourites.id AS favourites_id, favourites.userid AS favourites_userid, favourites.clientid AS favourites_clientid, favourites.last_visit AS favourites_last_visit \nFROM favourites INNER JOIN tblclients ON tblclients.clientid = favourites.clientid \nWHERE favourites.userid = %s ORDER BY tblclients.clientname' ([u'41'],)
Any ideas?
First, you have to use filter_by, not filter.
Second, Flask request.args uses a MultiDict, a dict with the values inside a list, allowing more than one value for the same key, because the same field can appear more than once in a querystring. You got the error because the SQL query got the [u'41'] when it expected only '41'. You can use request.args.to_dict() to fix that:
model.query.filter_by(**request.args.to_dict()).all()
Use filter_by:
model.query.filter_by(**request.args).all()
filter is used like this: query.filter(Class.property == value) while filter_by is used like this: query.filter_by(property=value) (the first one being an expression and the latter one being a keyword argument).
filter_by(**request.args) doesn't work well if you have non-model query parameters, like page for pagination, otherwise you get errors like these:
InvalidRequestError: Entity '<class 'flask_sqlalchemy.JobSerializable'>' has no property 'page'
I use something like this which ignores query parameters not in the model:
builder = MyModel.query
for key in request.args:
if hasattr(MyModel, key):
vals = request.args.getlist(key) # one or many
builder = builder.filter(getattr(MyModel, key).in_(vals))
if not 'page' in request.args:
resources = builder.all()
else:
resources = builder.paginate(
int(request.args['page'])).items
Considering a model with a column called valid, something like this will work:
curl -XGET "http://0.0.0.0/mymodel_endpoint?page=1&valid=2&invalid=whatever&valid=1"
invalid will be ignored, and page is available for pagination and best of all, the following SQL will be generated: WHERE mymodel.valid in (1,2)
(get the above snippet for free if you use this boilerplate-saving module)
You can:
http://localhost:5000/filter-test?var=test
query_dict = request.args.to_dict()
print(query_dict)
{'var': 'test'}
print(query_dict['var'])
var

How to put a literal "?" in a Mysql query with bind variables

I have the following query:
select subclasses.id,participants_subclasses.participant_id
from subclasses
left outer join participants_subclasses on
participants_subclasses.participant_id = ?
and subclasses.id = participants_subclasses.subclass_id
where
subclasses.classification_id = ?
and subclasses.showhover
order by subclasses.seq,
IF(LEFT(subclasses.code, 1) = '<',
Extractvalue(subclasses.code, "//texts/text/content"),
subclasses.code)
The above query is processing a table where the code column sometimes has text and sometimes has XML with text inside a tag. The above query works. The side-effect is that a code value cannot start with a "<" which should be acceptable, but the order by would mistake it for XML content. The query below would be more specific and accurate:
select subclasses.id,participants_subclasses.participant_id
from subclasses
left outer join participants_subclasses on
participants_subclasses.participant_id = ?
and subclasses.id = participants_subclasses.subclass_id
where
subclasses.classification_id = ?
and subclasses.showhover
order by subclasses.seq,
IF(LEFT(subclasses.code, 5) = '<?xml',
Extractvalue(subclasses.code, "//texts/text/content"),
subclasses.code)
However this variation checking the XML header in the content fails with a "NameInput Array does not match ?" error in MySQL. It appears that the ? inside <?xml literal is being mistaken for a bind target. And I am passing 2 values to be bound - which again is correct.
So my question is - how do I get the <?xml literal to not be mistaken for a bind value target???
SOLVED
This turns out to be a bug in ADODB interface from phpLens, and NOT in MySQL itself. It exists in current version which is 5.17 for PHP5.

ActiveRecord + SQLite 3 behaves strangely

Using activerecord I made this query
AdImage.select("ad_images.id, ad_images.locale_id, ad_campaigns.click_url,
ad_campaigns.default_ad_image_id").joins("left outer join ad_campaigns on
ad_campaigns.id = ad_images.ad_campaign_id").where("ad_images.ad_campaign_id" => 1)
which generates the following sql query:
SELECT ad_images.id, ad_images.locale_id, ad_campaigns.click_url,
ad_campaigns.default_ad_image_id FROM "ad_images" left outer join ad_campaigns on
ad_campaigns.id = ad_images.ad_campaign_id WHERE "ad_images"."ad_campaign_id" = 1
and the result is the following:
=> [#<AdImage id: 22, click_url: "market://details?id=com.mobiata.flighttrack",
locale_id: 2>]
which is wrong.
So I used ActiveRecord::Base.connection.execute method to directly run the sql query:
ActiveRecord::Base.connection.execute("SELECT ad_campaigns.click_url, ad_images.id,
ad_images.locale_id, ad_campaigns.default_ad_image_id FROM ad_campaigns inner join
ad_images on ad_campaigns.id = ad_images.ad_campaign_id WHERE ad_images.ad_campaign_id = 1")
which returns the following:
[{"click_url"=>"market://details?id=com.mobiata.flighttrack", "id"=>22, "locale_id"=>2,
"default_ad_image_id"=>22, 0=>"market://details?id=com.mobiata.flighttrack", 1=>22,
2=>2, 3=>22}]
which has the strange repetition in it.
The only difference between the first and the second is "ad_images" vs ad_images in the table names.
My questions are:
1) I don't understand what makes this difference.
2) Why does the second query returns the garbage in SQLite3 while it doesn't happen in MySQL server?
I ended up with using "ActiveRecord::Base.connection.execute" instead of using Rails' ActiveRecord helpers. There doesn't seem to be other solutions to it.
It turns out that you should use the index values instead of double quoted column names when you call values. Otherwise you will bump into Type errors when used in production with MySQL.