FullTextSearch in MYSql - java

In mysql fulltext search, the match gives some values,I don't know which one is mentioned by that values?
I need the comparsion output in percentage?How can i achieve that?
SELECT id,title,body,MATCH (title,body)
AGAINST ('database') FROM articles WHERE MATCH (title,body)
AGAINST ('database');
Output:
id', 'title', 'body', 'MATCH'
1, 'MYSql Tutorial', 'My Sql is one of the database language', 0.93769526481628
10, 'MySQL vs. YourSQL', 'In the following database comparison ...', 0.93769526481628
6, 'MySQL Tutorial', 'DBMS stands for DataBase ...', 0.92749810218811

I think you mean you want the relevance as well as the result. That would look something like:
SELECT id, title, body, MATCH(title, body) AGAINST ('database') as Relevance FROM `articles` WHERE MATCH(title, body) AGAINST ('database' IN BOOLEAN MODE) ORDER BY `Relevance` DESC
edit I got the question wrong. Here's a list of all relevance's in percentage form:
SELECT a.id, a.title, a.body,
MATCH(a.title) AGAINST ('database') as titleRelevance,
MATCH(a.body) AGAINST ('database') as bodyRelevance,
MATCH(a.title) AGAINST ('database')/c.maxTitleRelevance *100 AS percentageTitleRelevance,
MATCH(a.body) AGAINST ('database')/d.maxBodyRelevance *100 AS percentageBodyRelevance,
c.maxTitleRelevance + d.maxBodyRelevance AS maxTotalRelevance,
(MATCH(a.title) AGAINST ('database')+MATCH(a.body) AGAINST ('database'))/(c.maxTitleRelevance + d.maxBodyRelevance)*100 AS percentageTotalRelevance
FROM `articles` a,
(SELECT MAX(MATCH(b.title) AGAINST('database')) as maxTitleRelevance FROM articles b LIMIT 1) c,
(SELECT MAX(MATCH(b.body) AGAINST('database')) as maxBodyRelevance FROM articles b LIMIT 1) d
WHERE MATCH(a.title, a.body) AGAINST ('database' IN BOOLEAN MODE)
This might be easier to read in the following fiddle. http://sqlfiddle.com/#!2/c7885/14
Full text searches in mysql are flaky at best. If the word database is in at least 50% of rows of the db, it will be ignored as a common stopword if you dont specify boolean mode like in the query above. In terms of how relevance is calculated I'm afraid I cant help you there.

Related

Insert if not exist and update certain values if it does

I'm using JDBI3 (and would like to use #SQLUpdate) and an Oracle DB.
I want to insert an item with 4 columns into the table if it does not exist, if it does exist I want to instead update 3 of the 4 values of the item. If it wasn't Oracle I would've used some ON DUPLICATE_KEY logic but that does not exist in Oracle. I read some things about using Merge but the Queries seemed really wonky for what I was trying to do. Any tips on what to look for?
Additional question: If it is Merge I should use (with some form of sub queries I assume), how does the query affect performance? I think this database is quite write heavy.
MERGE INTO device db USING (SELECT 'abc' AS col1, 'bcd' as col2, 'cde' as col3, 'def' as col4 FROM DUAL) input
on (db.col1 = input.col1 AND db.col2= input.col2)
WHEN MATCHED THEN UPDATE
SET db.col4 = input.col4
WHEN NOT MATCHED THEN INSERT
(db.col1, db.col2, db.col3, db.col4)
VALUES (input.col1, input.col2, input.col3, input.col4)
Merge it is. Performs well.
Dummy example based on your description:
merge into target_table a
using source_table b
on (a.id = b.id)
when matched then update set
a.name = b.name,
a.job = b.job,
a.sal = b.sal
when not matched then
insert (id, name, job, sal)
values (b.id, b.name, b.job, b.sal);

Using the IN clause with SQLite

In a development environment with SQLite 3.7.11 and Java, despite having read the following answers:
Answer 1
Answer 2
Answer 3,
am finding the usage of the SQLite IN clause not very straight-forward.
Assume a simple table TEST with the following structure:
----------------------------
id PRODUCT TAG
(int) (text) (text)
----------------------------
1 Cinthol Soap, Bath, Cleaning
2 Vim Dishwash, Kitchen, Cleaning
3 Burger Food, Breakfast, Lunch, Dinner
The following queries are behaving this way:
----------------------------------------------------------------------------
Query Result Expected
----------------------------------------------------------------------------
SELECT PRODUCT FROM TEST WHERE TAG IN('Soap') Cinthol Cinthol
SELECT PRODUCT FROM TEST WHERE TAG IN('Dinner') <EMPTY> Burger
SELECT PRODUCT FROM TEST WHERE TAG IN('Soap', 'Bath') <EMPTY> Cinthol
SELECT PRODUCT FROM TEST WHERE TAG IN('Cleaning') <EMPTY> Cinthol, Vim
So the questions are:
Except the first query, why are the others not producing the expected results? Is there something fundamentally wrong in the understanding?
If wrong, what is the right query to get the expected results (without using the instr function)?
Furthermore, the TAG column eventually has to be bound with an array of tokens in Java, building the query dynamically. The answers listed above have pointers to that, though.
Thanks in advance!
In clause doesn't work like this.assume if you had one TAG each column you could get the results.you need to add another table to keep your tags.in this table you need pk , foreign key(id deom tests) ,and tag so that you wil have multitags for each product.this is a solution you can make different.You had better search database notmalization first.gl

JPA Criteria "locate" ignoring "from" parameter

I have a table SASDOSSIERS with one column apentrytext which has one value 6.8.3 Dossiers "A" , SAS, Fürsorgeleistungen an Auslandschweizer which I need to select after the last ,: Fürsorgeleistungen an Auslandschweizer. If I perform the following query in sql, the select is correct:
SELECT distinct TRIM(SUBSTR(apentrytext,INSTR(apentrytext,',',-1)+1)) from SASDOSSIERS;
Whereas, if I try to use the equivalent in jpa critera
cq.multiselect(
cb.trim(
cb.substring(
sasdossier.get(Sasdossier_.apentrytext),
cb.sum(
cb.locate(sasdossier.get(Sasdossier_.apentrytext), cb.literal(","), cb.literal(Integer.valueOf(-1))),
1
)
)
)
).distinct(true);
It ignores totally what I put in the from parameter (I have tried with many different values, eg:-2, -100, 1, 2, 100) and it seems to ignore what I put there, the result is always the same. Moreover, the query generated by hibernate shows that the argument is, indeed, ignored:
SELECT DISTINCT trim(BOTH FROM substr(sasdossier0_.apentrytext, instr(sasdossier0_.apentrytext, ?) + 1)) AS col_0_0_
FROM ACCESS_DB.sasdossiers sasdossier0_
WHERE sasdossier0_.apentrytext IS NOT NULL
It seems like a legit bug, but I cuouldn't find anybody that has the same error before, so, am I doing something wrong? should I report the error?
Thank you.

Java update when data exists and insert if doesnt [duplicate]

In MySQL, if you specify ON DUPLICATE KEY UPDATE and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have identical effect:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
UPDATE table SET c=c+1 WHERE a=1;
I don't believe I've come across anything of the like in T-SQL. Does SQL Server offer anything comparable to MySQL's ON DUPLICATE KEY UPDATE?
I was surprised that none of the answers on this page contained an example of an actual query, so here you go:
A more complex example of inserting data and then handling duplicate
MERGE
INTO MyBigDB.dbo.METER_DATA WITH (HOLDLOCK) AS target
USING (SELECT
77748 AS rtu_id
,'12B096876' AS meter_id
,56112 AS meter_reading
,'20150602 00:20:11' AS time_local) AS source
(rtu_id, meter_id, meter_reading, time_local)
ON (target.rtu_id = source.rtu_id
AND target.time_local = source.time_local)
WHEN MATCHED
THEN UPDATE
SET meter_id = '12B096876'
,meter_reading = 56112
WHEN NOT MATCHED
THEN INSERT (rtu_id, meter_id, meter_reading, time_local)
VALUES (77748, '12B096876', 56112, '20150602 00:20:11');
There's no DUPLICATE KEY UPDATE equivalent, but MERGE and WHEN MATCHED might work for you
Inserting, Updating, and Deleting Data by Using MERGE
You can try the other way around. It does the same thing more or less.
UPDATE tablename
SET field1 = 'Test1',
field2 = 'Test2'
WHERE id = 1
IF ##ROWCOUNT = 0
INSERT INTO tablename
(id,
field1,
field2)
VALUES (1,
'Test1',
'Test2')
SQL Server 2008 has this feature, as part of TSQL.
See documentation on MERGE statement here - http://msdn.microsoft.com/en-us/library/bb510625.aspx
SQL server 2000 onwards has a concept of instead of triggers, which can accomplish the wanted functionality - although there will be a nasty trigger hiding behind the scenes.
Check the section "Insert or update?"
http://msdn.microsoft.com/en-us/library/aa224818(SQL.80).aspx

Complicated LIKE Expression in derby (Java DB)

i've a table with ID, Name both are String type in ID i'v value like
1.3.6.1,
1.3.6.2,
1.3.6.1.2,
1.3.6.1.3,
1.3.6.1.4,
1.3.6.2.1.
1.3.7.2,
1.3.7.5,
1.3.8.1,
etc
I need to retrieve records like 1.3.6. .. but not like 1.3.6.ANY_NUMBER. ..,
Can u help me to write a Derby query for it
Thanks in advance
Hanks
You wrote the answer yourself
WHERE id LIKE '1.3.6%' AND id NOT LIKE '1.3.6.1%'
Maybe you better don't use LIKE but a simple equals:
SELECT * FROM db WHERE id='1.3.6';
Use this to select data from [column_name] in format up to 3 dots (like 1, 1.1, 1.1.1 but never 1.1.1.1)
SELECT SUBSTRING_INDEX([column_name],".",3) FROM [table_name];
or
Use this to select rows where [column_name] is not longer than 5 chars (1, 1.1, 1.1.1 but never 1.1.1.1)
SELECT [column_name] FROM [table_name] WHERE CHAR_LENGTH([column_name]) < 6

Categories