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.
Related
I'm a little bit stuck.
I have an SQL column that contains weather codes (like Rain, Snow, etc.)
It is comma separated so the column would have a value of something like
rain,snow,haze
Now, I want to select the rows that contain values from an array.
I have an SQL code that is something like this:
SELECT * FROM locations WHERE currentWeather IN ('rain', 'snow', 'cloudy') ORDER BY name ASC
The problem is that this obviously works when currentWeather column only contains one item.
Is there a way to do it so that if the column value contains any of the items from the given array, it selects it?
Also, would it select it twice if two items match?
Best wishes
Use unnest in a subselect.
Select distinct A.myArray from (select unnest(column) as myArray from table) A where A.myArray in (your words to filter for)
Notice that using arrays in sql isn't very ideal and does not follows normalization rules. Your tables should ideally not contain arrays but rather just several rows each one containing the specific value you Want. It prevents issues such as this one.
To avoid the selection of repeated values, use the Distinct keyword right after you write select.
Rsference:
https://www.w3resource.com/PostgreSQL/postgresql_unnest-function.php
WHERE FIND_IN_SET(currentWeather, "rain,snow,cloudy")
Picks apart the string at commas (only) to see if currentWeather is any one of those 3 'words'.
See also FIELD(...)
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.
So I am try ing to create a MySQL command to search a table. What I want it to do is:
My table has 4 columns: id, categories, brands, names.
In php/html
on the search page the user selects a category from a dropdown. This is the category so this is posted as category to the search page. then thy type in a keyword into the text box. This is posted keyword to the search page.
I want to make a MySQL command that selects the category in categories posted then searches those selected rows for my posted keyword in the brands, and names columns
Here is what I have tried:
"SELECT brands, names
FROM tableName
WHERE categories LIKE".$_GET['category']."
AND (brands LIKE ".$_GET['keyword']."
OR names LIKE ".$_GET['keyword'].");";
You need quotes around strings in SQL. Try this:
"SELECT brands, names
FROM tableName
WHERE categories LIKE '".$_GET['category']."'
AND (brands LIKE '".$_GET['keyword']."'
OR names LIKE '".$_GET['keyword']."');";
Unless you are expecting there to be variations e.g. several words with the same beginning, using LIKE doesn't achieve much. If you are expecting this, you need to use wildcards to specify where you expect the variance to be e.g.
brands LIKE '".$_GET['keyword']."%'
// User enters 'Mc', SQL end up as:
brands LIKE 'Mc%'
// Columns matched: 'McDonalds' 'McHenry'
More examples here.
If you don't want to allow for this, just use = instead of LIKE.
You also need to sanitise these variables first using a regular expression or similar, as this would currently allow someone to attack your server with SQL injection.
Take your command, and play with it in your SQL document editor, I think it is something with your
(brands LIKE ".$_GET['keyword']." OR names LIKE ".$_GET['keyword'].");"
I know in sqlite, you would use an equals sign, instead of LIKE,
(brands='".$_GET['keyword']."' OR names='".$_GET['keyword']."');"
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/
I'm making a search engine which creates a query depending on what's chosen on the search page. Since the query is limited I can not just include anything in it, which is where I have to do the rest with IF clauses and query while-loops. So, there's no age field in the table, but a birthdate field, so I use an IF function to check if the age is correct and then prints out the username(s) in a while loop.
This works alright, but I also need to add two more fields into the search which are as follows: County and City, right now working on the County selection of the page. What I can't figure out by myself is the logic behind how I'm supposed to manage to print out the users that fit all the required fields without having 1000 IF ELSE's.
I thought of SELECTing and filtering out all the correct zip codes to the county/region chosen, then put it in an array, and then validate the output query while-loop against it, but that didn't work so well either.
In my database I have 3 tables which look like this:
county_table
id, name_of_county
municipial_table
id, county_id, municipial_name
zip_code_table
zip, zip_place_name, municipial_id
These are pre-made for my country. So, given the zip code of the user, I will have to do two different SELECT queries to connect it to the county_table (zip->municipial->county).
So basically, what I'm trying to say; I want the search engine to output the users that have the correct data, this depending if the age, region and city fields are selected. They need to be independent and not like:
if($age>X){
if($county==Y){
if($city==Z){
-OUTPUT RESULTS HERE-
} } }
Now, the problem with this is: What if one of the fields are not requested in the search? Say, the age? The county number? The city? I think what I need are non-nested and independent IF blocks, but I'm not sure how to set it up correctly.
Help very much appreciated, thank you alot.
My re-inventing the wheel? Unless you're making a search engine for educational purpose, use something that's already been tested and optimized. See this related question.
.