Problems using MySQL FULLTEXT search - mysql

I have already posted a question about this, but the situation has changed sufficiently to warrant a new one.
I have a MySQL table called aromaProducts in which there are 7 columns with the FULLTEXT index, and which has three records in it. When I make a query against it like:
SELECT * FROM aromaProducts WHERE MATCH (title) AGAINST ('chamomile');
I get the correct result. However, when I try adding a second field to search in, I get an error:
Can't find FULLTEXT index matching the column list
Every column on its own works fine. I have also explicitly adding WITH QUERY EXPANSION and the same thing.
I have another table, aromaProducts1, and instead of assigning FULLTEXT to the fields one at a time, I assigned it to all 7 at the moment of table creation. Against this table, no queries work. When examining the table structure, the difference is this:
The first table shows each field having its own FULLTEXT index, while the second has one index, named title (the first field to have it assigned), and it applies to all seven fields.
All columns that I have made FULLTEXT are either VARCHAR or TEXT datatypes. I have no clue what the problem is.

You have to create the fulltext index on all columns you are going to match
If you want to match on (col1,col2,col3) you have to create fulltext index on col1,col2 and col3. If you want to match on(col1,col2) you have to implement another fulltext index(on col1 and col2), you can't use that one on col1, col2 and col3

you have to create one fulltext index with several columns, not one for each

Related

Displaying entries of an index (MYSQL)

I am trying to list all the entries of an index.
Let's say I have unique index_idx on 2 columns (col_1, col_2) on table table_test.
I assume that the uniqueness for a tuple is checked by concatenating the values in column col1 and col2 and checking if overall the value is unique.
But is there any method predefined using which we can list all the entries for index index_idx.
I saw a similar question here. But the answer didn't make much sense to me. What exactly is ''INDEX COLUMNS LIST' here?
There's no way directly to list the contents of an index (the MySQL / MariaDB developers may have a way to dump index contents, but if they do it's for debugging purposes).
SELECT col_1, col_2 FROM table_test gets you the contents of the index. The index you mentioned is a so-called covering index for that query. MySQL uses the index, rather than the table, to satisfy the query. That's a little faster.
The item you linked does this same thing.
And you are basically correct that the uniqueness of a two-column UNIQUE index is determined with the concatenation of the column values.

How to find exact match in a column faster without indexing being an option due to non unique values

I have a database column with titles of documents. These titles are not unique, and can be anywhere from a few words to a a few dozen words. I have over 3 million rows. I am trying to optimize looking for exact matches.
Indexing is not possible since there is no primary key, and the column is not unique. I have thought about a binary search, but that's done automatically I've heard when you index something. How can I implement a binary search on a column that's not index-able due to it not being unique?
SELECT * FROM cases where title = "Bondelmonte v Bondelmonte"
Takes a few seconds, I want it to take a fraction of that time.
Assuming MySQL
CREATE INDEX title_index ON cases (title)
Creates a non-unique index on table "cases" column "title"
https://dev.mysql.com/doc/refman/8.0/en/create-index.html
You would need to specify UNIQUE to create a unique index
Additionally you may want to create a full text index
CREATE FULLTEXT INDEX title_flt_Index ON cases ( title );
https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html

FULLTEXT search NOT working on two columns

Background:
Hey I'm using MySQL 5.6.17 InnoDB, I've read on mysql website that FULLTEXT is now avaiable for InnoDB in 5.6+ version. so i don't have to change from InnoDB to MyISAM Here is the link I altered TWO of my table columns for FULLTEXT search by using the following query
ALTER TABLE `es_officers` ADD FULLTEXT Index_officer_name (es_officer_name)
ALTER TABLE `es_officers` ADD FULLTEXT Index_officer_fname (es_officer_fname)
Altered Table Registered in Information Schema:
Then i checked in my information schema if the altered table columns are registered or not by running the following query
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME
FROM statistics
WHERE index_type LIKE 'FULLTEXT%'
It showed me exacttly two results which i was expecting
Problem:
when i write a query to MATCH a sting AGAINST two columns it gives me an error
SELECT * FROM `es_officers` WHERE MATCH (es_officer_name, es_officer_fname) AGAINST ('abc')
#1191 - Can't find FULLTEXT index matching the column list
BUT:
when i try to query the columns separately like below it works absolutely fine
SELECT * FROM `es_officers` WHERE MATCH (es_officer_name) AGAINST ('abc')
SELECT * FROM `es_officers` WHERE MATCH (es_officer_fname) AGAINST ('abc')
I don't know what is it I'm doing wrong, help is highly appreciated.
Multiples issues:
You have two separate fulltext indexes, each covering a single field. You have WHERE MATCH (es_officer_name, es_officer_fname) as your query, which requires a SINGLE index covering both fields. MySQL will not use two separate indexes for this query - it can't. that's not how fulltext indexing works. You need an alter ... fulltext (es_officer_name, es_officer_fname) instead added.
And even then, abc will fall under the default minimum word length and won't get indexed.
Besides, you can try something like:
SELECT * FROM es_officers
WHERE MATCH es_officer_name AGAINST ('abc') + MATCH es_officer_fname AGAINST ('abc')
Though, as Marc B said, 'abc' value is too short.

MySQL fulltext search with different permutations

I am creating a mysql fulltext search engine for my website, and I have an advanced search page that allows the user to limit which columns they would like to search under. However, whenever I make a fulltext search index, all the columns used in that index must be used or else I get an error message. Is there any way to make a mysql index for fulltext where I can use just some of the columns?
For example I have 5 columns in my index, made by this statement:
ALTER TABLE table1 ADD FULLTEXT fulltext_index(subject, course, prof, semester,
year);
If I wanted to search under only subject and course, I would get the error:
#1191 - Can't find FULLTEXT index matching the column list
You just need to create more indexes for each possible combination you want to search:
alter table table1 add fulltext fulltext_index2(subject,course);
alter table table1 add fulltext fulltext_index3(course,semester,year);
Etc...see if that solves the issue.
No, a fulltext index requires you to use all of the columns. If you want to use only some of them, you'll need a separate fulltext index for every permutation. I haven't tested it, but you MAY get around it by using only columns in the order specified. e.g. an index on columns (a,b,c) might allow you to search only (a,b) because they're listed first, but not (a,c) or (b,c).

Do indexes interfere with each other in MySQL?

I have a table with 3 columns. This table contains many raws (millions). When I select rows from the table I frequently use the following where clauses:
where column2=value1 and column3=value2
where column1=value
To speed up the select query I want to declare column1 and column2 as indexes. My questions is if declaring the second column as an index will not reduce the positive effect of declaring the first column as index.
I also would like to ask if declaring the second column as index will speed up the queries of this type: where column2=value1 and column3=value2.
ADDED
The column1, column2, and column3 are entity, attribute, value. It's very general. As entities I use person, movies, cities, countries and so on. Attributes are things like: "located in", "date of birth", "produced by".
You should create indexes that support your queries. In this case you want to create an index on column2,column3 together (not two separate indexes, but one index for the combination of columns) to support the first query, and another on column1 to support the second query. More generally, if a query uses a set of columns, adding an index for all those columns will speed it up (although there are many exceptions, of course).
An index on column2 would speed up the query column2=value1 and column1=value2, and so would an index on column2,column3 (the important thing is that column2 is the first column in the index).
When working with indexes the EXPLAIN keyword is very useful. Prefix your queries with EXPLAIN (e.g. EXPLAIN SELECT * FROM table) to get a description of how the database is going to perform your query. It will tell you if it's going to use an index, and in that case which.
Seems like neither of your plans are going to work. Based on both of the where clauses I would suggest having the primary key on column1 and a second index column2,column3. This would speed up both of your queries.