fulltext query only works "with query expansion" - mysql

I am trying to build a very basic fulltext search app in php. I have found an example here Mysql fulltext search so I used it to build my own.
This is my table/data (field types are text)
name site result
RRR1 COS COSMETICS P R15-500 000847719903 20110607 094742.VER RRR1 P
RRR3 BIST MIDDLEWARE P R22-200 029051946829 20110607 101331.VER RRR3 P
RRR2 PRE PREHEAT F R16-500 000897546214 20110607 085111.VER RRR2 F
RRR1 COS COSMETICS P R16-300 000899331425 20110607 091337.VER RRR1 P
This is my index
ALTER TABLE automation_search_test ADD FULLTEXT search_idx (name ,site)
This is my query
SELECT * FROM automation_search_test WHERE MATCH (name,site) AGAINST ('RRR1' with query expansion);
I have however a few problems:
If I remove with query expansion, the query returns an empty set
If I add the RESULT column to my fulltext index, the query returns an
empty set after I change my SQL to this
SELECT * FROM automation_search_test WHERE MATCH (name,site,result)
AGAINST ('RRR1' with query expansion);
Is my index incorrectly setup? Is there an issue with the data?
I welcome any input you have. Thanks.

Not enough data was my problem. Mysql documentation clearly explains it. Thread closed.

Related

How to search on many fields of a single MySQL table without using many OR conditions?

I'm currently working on a php page that search over a table some data. The problem is that the searched string can be in "field1", "field2", "field3" or "field4".
How to do this SQL query without using 4 OR conditions ?
Here is the query I do have at this very moment.
SELECT a_field FROM my_table WHERE LOWER(field1) = 'searched_string' OR LOWER(field2) = 'searched_string' OR LOWER(field3) = 'searched_string' OR LOWER(field4) = 'searched_string'
Thanks
You can use match for this. But first you need to create your indexes.
SELECT * FROM table WHERE MATCH(field_1, field_2) AGAINST ('keyword or expression');
You can find details in documentation.
Innodb full text search support comes after version 5.6
Mysql Full text search

Trick to use variable in match against mysql

Please first read my question,and then you will find out it is not a duplicate of other question.
I'm using sphinx search for 98% of search,but need to use match against for just one query.
As we know from mysql documentation that AGAINST only takes string.The search string must be a literal string, not a variable or a column name.
But I have found this link http://bugs.mysql.com/bug.php?id=66573 ,which says it is possible.But I'm not sure how to use that in my case.
Here is my code
$sqli="SELECT busi_title,category FROM `user`.`user_det`";
$queryi=mysqli_query($connecti,$sqli);
if(mysqli_num_rows($queryi)>0){
while($rowi=mysqli_fetch_assoc($queryi)){
$busi_title=$rowi['busi_title'];
$category=$rowi['category'];
}
}else{
echo "OH NO";
}
$sqlj="SELECT * FROM `user`.`user_det` WHERE MATCH(student) AGAINST('$busi_title','$category')";
$queryj=mysqli_query($connecti,$sqlj);
if(mysqli_num_rows($queryj)>0){
..............................
..............................
}else{
foreach ( $res["matches"] as $doc => $docinfo ) {
................................
...............................
}
}
MATCH() AGAINST() is giving error,as it supposed to be.How to use that trick of that link in this case.I don't know the use of #word:= of that link.
Thanks in advance.
That link doesn't show a trick to get around a limitation of MySQL. It's a bug report demonstrating an incorrect statement in the MySQL documentation. The statement in the documentation has now been corrected.
The reason you're getting an error is because you're sending two parameters to AGAINST and it only accepts one. You can use a MySQL variable in AGAINST which is what the bug report is about, but this has nothing to do with the PHP variable that you're using.
EDIT
Upon reading your response, I rather suspect that you have your syntax backwards.
SELECT * FROM `user`.`user_dets` WHERE MATCH(busi_title, category) AGAINST('student')
But note this from the documentation:
The MATCH() column list must match exactly the column list in some FULLTEXT index definition for the table, unless this MATCH() is IN BOOLEAN MODE. Boolean-mode searches can be done on nonindexed columns, although they are likely to be slow.
If you don't have a Fulltext index, you'll actually want this:
SELECT * FROM `user`.`user_dets` WHERE `busi_title` LIKE '%student%' OR `category` LIKE '%student%'
When they say "The search string must be a literal string, not a variable or a column name" does not mean you cannot use variable to create your Query String.
So it is OK to make your query very simple.
Your WHERE could be this:
WHERE `student` = $busi_title OR `student` = $category

AS clause in influx DB

How to use AS clause in influxDB?
SELECT os_family AS OsName, os_Image AS PlatformIcon FROM statistics
When I run this query I got following error.
ERROR: syntax error, unexpected AS, expecting FROM SELECT os_family AS OsName, os_Image AS PlatformIcon FROM statistics ^^
How to use SQL like AS clause in influx DB?
The AS clause in InfluxDB is meant to be used in two cases (described below). That said we can definitely add this as a new feature if this is a common use case. I don't know if that's necessary though, if you want to get back the columns as OsName then why did you name it os_family to begin with. We can discuss this further on the mailing list (you can find information about the mailing list among other means to reach us here http://influxdb.com/community/)
The two use cases are for joining multiple time series:
select * from foo as f inner join bar as b where f.somecolumn > 0`
and to alias aggregators:
select count(value) as c from foo where foo.value > 100

Why is my query wrong?

before i use alias for table i get the error:
: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous
Then i used aliases and i get this error:
unknown index a
I am trying to get a list of category name ( dependant to a translation) and the associated category id which is unique. Since i need to put them in a select, i see that i should use the lists.
$categorie= DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->lists('nome.nome','a.id');
The best way to debug your query is to look at the raw query Laravel generates and trying to run this raw query in your favorite SQL tool (Navicat, MySQL cli tool...), so you can dump it to log using:
DB::listen(function($sql, $bindings, $time) {
Log::info($sql);
Log::info($bindings);
});
Doing that with yours I could see at least one problem:
->where('lingua.lingua','=','it-IT')
Must be changed to
->where('lingua.lingua','=',"'it-IT'")
As #jmail said, you didn't really describe the problem very well, just what you ended up doing to get around (part of) it. However, if I read your question right you're saying that originally you did it without all the aliases you got the 'ambiguous' error.
So let me explain that first: this would happen, because there are many parts of that query that use id rather than a qualified table`.`id.
if you think about it, without aliases you query looks a bit like this: SELECT * FROM `cat` JOIN `campo_cat` ON `id_cat` = `id` JOIN `campo` ON `id` = `id_campo`; and suddenly, MySQL doesn't know to which table all these id columns refer. So to get around that all you need to do is namespace your fields (i.e. use ... JOIN `campo` ON `campo`.`id` = `campo_cat`.`id_campo`...). In your case you've gone one step further and aliased your tables. This certianly makes the query a little simpler, though you don't need to actually do it.
So on to your next issue - this will be a Laravel error. And presumably happening because your key column from lists($valueColumn, $keyColumn) isn't found in the results. This is because you're referring to the cat.id column (okay in your aliased case a.id) in part of the code that's no longer in MySQL - the lists() method is actually run in PHP after Laravel gets the results from the database. As such, there's no such column called a.id. It's likely it'll be called id, but because you don't request it specifically, you may find that the ambiguous issue is back. My suggestion would be to select it specifically and alias the column. Try something like the below:
$categories = DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->select('nome.nome as nome_nome','a.id as a_id') // here we alias `.id as a_id
->lists('nome_nome','a_id'); // here we refer to the actual columns
It may not work perfectly (I don't use ->select() so don't know whether you pass an array or multiple parameters, also you may need DB::raw() wrapping each one in order to do the aliasing) but hopefully you get my meaning and can get it working.

Returning Rank with Full Text Search Contains Query

I have implemented Full Text Search on one of Sql Server 2008 Table.
Query works fine when search some words using contains.
I want to filter out the result on the basis of Rank functionality of Full Text Search. I am writing the following query
SELECT rank, * FROM Mas_text
WHERE CONTAINS(text, 'Wanted and Engineers')
This query does not compile and give me error of "Invalid column name 'RANK'"
Please suggest.
The rank function doesn't work with the CONTAINS query. You'll have to use CONTAINSTABLE to get the rank. See here.
Should look something like this:
SELECT Mas_text.*, k.rank
FROM Mas_text
INNER JOIN CONTAINSTABLE(Mas_text, text, 'Wanted and Engineers') k
ON Mas_text.primarykey = k.[Key]