Modified string search in Mysql Table - php

I have a string query to search (assuming "this is my first query" as an example).
And I am also having a table which consists of (id, title, rating, ... ) attributes.
What I want is to find out the search results of my string query which match with "title" attribute (probable or exact both).
But what if complete text i.e. "this is my first query" is not there then there will be no results if I do like
SELECT * FROM test WHERE title LIKE '%$query%';
What I am trying to think next is to fire another query with lesser character this time.. i.e. I will fire the same query using search string "this is my first quer" (y is truncated) and so on until I get my desired no. of results.
But my problem is:
This is very costly operation
I want search data to be sorted in order of descending value of rating ( another field in "test" table)
Please help me out how can I proceed with this ?

Add index on this field and run query in loop. I don't like it.
Use fulltext searching http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.htm in mysql.
Use standalone searching server like Sphinx http://sphinxsearch.com/

Related

Advanced search term in php and MySql

I have a MySql table contain a title field.
Suppose a user enter a term in an input textbox.
Now I want to select rows whose title field has one of the following states:
1) its title is exactly the same as term : "sugar">>"sugar"
2) one of the words of its title is like the term : "pretty flower" >> "rose flower"
3) its title is from the same Word family as that term (term is root word of those) : "biology" >> "biography, biodegradable, symbiotic"
I'm using laravel. if can suggest any solution for that , Would be great
This was an easy solution until you mentioned word family...
However, this is something that is still achievable. It's about how you approach it.
You'll start by using the like condition in MySQL.
You can read more about it on the MySQL Website relating to Pattern Matching
Here's an example of that:
SELECT * FROM my_table WHERE field1 LIKE '%searchTerm%';
In terms of getting results by "word family", you may want to consider adding tags to the result.
Add a tags field in the table and add an array of tags that would relate to the "word family".
Your query would then look something like this:
SELECT * FROM my_table WHERE field1 LIKE '%searchTerm%' OR field2 LIKE '%searchTerm%';
You'll need to loop through the array of tags to find what matches, if at all.
Just my approach to get started.
In terms of how to do this is Laravel, your query may look something like this, according to their documentation
$results = DB::table('table1')
->where('myfield', 'like', '%searchterm%')
->get();
I surely hope this puts you in the right direction.

How to write conditional query in Solr?

I am new to sonr and I have to write a conditional query. I just wanted to know can I write a query with multiple conditions.
I have four column and the query condition is something like following:-
Columns: name, address, city, state
Now if someone is searching by keyword "Romania" then the result should be based on following conditions:-
Search in name columns if keyword match with the name column then return the data and do not go for any further column.
If name column doesn't have that keyword then it will go to the address column and similar to the first step if result found then return the data otherwise it will go to next column.
It is a priority search in which the priory of columns to be used in search is predefined. If sonr provide any such functionality I want to use that instead of writing any fuzzy logic.
Thanks
This would be to search all the columns you're talking about. Use qf to tell Solr which fields you want to query (when using the edismax query type):
qf=name address city
You can also give weights, so that a hit in the name column will be shown higher than a hit in the address column, which in turn will be shown higher than a hit in the city column:
qf=name^20 address^10 city
You might have to adjust the weights to get the search result you want.
You could use fq to search it like these:
fq=name:romania
or in the q
+name:"romania" or +address:"romania" or +city:"romania" or +state:"romania"
using OR is like in programming that if the first query is false, then move to the next one and to the next.

Solr filter query for string

I have been working with Solr recently to filter data using set_filter_query. I was able to work with Integer values.
$solr_dao->set_filter_query('ID:[3+TO+*]');
This query filters data having values 3 or more. I am also trying to include a word search on a field using
$solr_dao->set_filter_query('Comments:[*'.$search_term.'*]');
which doesn't seem to be working. This query should filter data with comments field having the search term. Any ideas what might cause this?
use this instead :
$solr_dao->set_filter_query('Comments:*'.$search_term.'*');
Note that in Solr, it indexes every words. So if a row contains "Hello honey my name is John" in the column "foo", if your query is foo:Hello World, this row will satisfy the query, since Solr looks for words, and read spaces as "OR"...

Find rows where column value is contained in input string

I have prepared a spreadsheet containing several entries, and each row of entry provides two columns of information. One is a product name, and another one is the price of the product. An Example is shown as follows:
Product (Column A) Price (Column B)
1. Watch $100
2. Mousepad $50
3. Notebook $1000
I am trying to create a web-based database with PHP and MYSQL. The basic searching function is that when I enter a sentence in a search box, such as "I have a watch and a notebook", those entries in my database matching some of the words in the sentence would be given (i.e. row 1 "Watch" and row 3 "Notebook"). Does MySQL support this kind of search, and how can I write the query to do so?
Writing an SQL query to do that would not be hard. All you need is to write a WHERE clause that searches for the column value inside your string. MySQL has a LOCATE() function you can use to search for substrings. In your example, you may have:
SELECT *
FROM myTable
WHERE LOCATE(product, 'I have a watch and a notebook.') <> 0;
It is important to note, as you'll see in the link, locate returns 0 if the substring is not found.
Here is an SQL Fiddle example.

Querying a table where the field contains any order of given text strings

I want to query a table as follows:
I have a field called "category" and my input match contains N separate words. I want the query to match all rows that contain all N words, but in any order.
For example if the field category contains "hello good morning world", my input query can contain "hello morning" or "good" or "world hello" and all are matches to the query.
How do I formulate such an SQL expression?
Also it would be good if the query can be made case insensitive.
If you are using MySQL you can use the boolean fulltext search feature to achieve this. You can put a + in front of each term and then only results with all the terms, in any order, will be returned. You will need to make sure the column containing the category field has a fulltext index specified on it for this to work. Other database engines probably have similar features. So for example you might do something like the following assuming there were a fulltext index over the category column...
SELECT * FROM myTable WHERE MATCH (category) AGAINST ('+term1 +term2 +term3' IN BOOLEAN MODE);
I would avoid using the "LIKE" operator as others have suggested you would have to worry about the headache of mixed upper/lower case and if you have a large database using a % in the front of a LIKE search term is going to cause a full table scan instead of using an index which is horrible for performance.
I'm not writing the loop that will build this query for you. This will get the job done, but it will be pretty inefficient.
SELECT * FROM table
WHERE (
TOUPPER(category) LIKE '*HELLO*' AND
TOUPPER(category) LIKE '*GOOD*' AND
TOUPPER(category) LIKE '*MORNING*' AND
TOUPPER(category) LIKE '*WORLD*'
);
You could also research using REGEXes with SQL.

Categories