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"...
Related
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.
I'm looking for a way to compare database values against a given query using MySql
(in oppose of searching the query in the db)
I will elaborate:
I have a table that holds comma separated keywords and a result for each block of keywords
for example :
col 1 col 2
Mercedes,BMW,Subaru car
Marlboro,Winston cigarette
today im taking the user query (for example - Marlboroligt)
as you can see if i will search for the value 'Marlboroligt' in the db i will get no results.
for that matter i want to search 'Marlboro' from the db inside the query and return 'cigarette'
Hope my explanation is sufficient and that this is actually possible :-).
Normalization will help you the most. Otherwise, search in comma delimited fields is discussed here:
Select all where field contains string separated by comma
...and to search 'Marlboroligt' instead of 'Marlboro Light', you can try looking into the LEVENSHTEIN function, or maybe the Soundex encoding (which looks like too little bang for too large a buck, but then again, maybe...).
I see the following possible solutions:
setup a keyword-search engine like Sphinx and use it to search keywords in your db
normalize your db - col1 must contain the only keyword
use like patterns
select col2 from mytable where col1 like "%Marlboro%"
like slows down your application and can have substring-related issues.
How would the best way to approach this problem be?
Search for user by first name, where the parameter is a string. The result should display a list
of the 10 users whose first name is closest to the searched string.
Clearly there are different ways to approach this one. Is there any mysql functionality that does this already?
Should I just query all my rows and run some kind of search function?
Have a look at MySQL Full-Text search functions which will automatically return results based on relevancy, though it will not work with InnoDB.
I'm creating a search page for my application and its using MongoDB. So I need to search an array of strings in multiple fields.
When I search in a single field, I do this:
$docs = $collection->find(array('username' => new MongoRegex("/^query/"));
But when I search for multiple fields, what do I need to do? Something like this?
$docs = $collection->find(array('username','name', 'email' => new MongoRegex("/^query/"));
Here is a page that documents how to query MongoDB from PHP: http://php.net/manual/en/mongocollection.find.php
You may be thinking that because you want to compare multiple fields to the same value that there would be a special (shorter) syntax for it (just like when you want to compare a single field to multiple values, like in a range query).
However, that is not the case - you still have to list every field and that value you are comparing it to, as if each field was being compared to a different value.
The only other thing to consider is whether you are searching for all documents that have any of the fields match this regex or all of the fields match the regex. In the first case you have to do an "$or" query. See the last example on the bottom of the manual page for syntax.
could someone please point me in the right direction, I currently have a searchable database and ran into the problem of searching by title.
If the title begins with "The" then obviously the title will be in the 'T' section, what is a good way to avoid "The" being searched ? Should i concat two fields to display the title but search by only the second title ignoring the prefix. or is there another way to do this? Advice or direction would be great. thanks.
A few choices:
a) Store the title in "Library" format, which means you process the title and store it as
Scarlet Pimpernel, The
Tale of Two Cities, A
b) Store the original unchanged title for display purposes, and add a new "library_title" field to store the processed version from a).
c) Add a new field to store the articles, and the bare title in title field. For display, you'd concatenate the two fields, for searching you'd just look in the title field.
I believe the best approach is to use full-text search, with 'the' in the stopwords list. That would solve the search problem (i.e., 'the' on search phrases would be ignored).
However, if you are ordering the results by title, a title starting with 'The' would still be sorted, "in the 'T' section", as you put it. To solve that, there are several possible approaches. Here are some of them:
Separating the fields, the way you said on the quesiton
Having a separate field with the number of chars to be ignored from the beginning when sorting
Replacing initial 'The's for sorting
Among others...
If you are using mysql, you could use a str_replace function to remove "The" from your query, or if you are using PHP or Ruby or another language you can just sanitize your query before sending to the database server.
Create three columns in the database
1) TitlePrefix
2) Title
3) TitlePostfix
Code such that you have 4 methods like
searchTitleOnly(testToSearch) // search only title column
searchTitleWithPrefixAndPostfix(testToSearch)//concat all the three columns and search
searchTitlePrefix(testToSearch) // search title prefix only
searchTitlePostfix(testToSearch) // search title postfix only
Try looking into some sql functions like LTRIM, RTRIM etc and use these functions on a temp column which has exact same data. Modify the data by using LTRIM, RTRIM by dropping whichever words u please. Then perform the search on the modified column and return the entire row as the result!