PreparedStatement with CONTAINS query

我与影子孤独终老i 提交于 2019-12-06 08:22:49

问题


I have a query that will need to run 28 000 times in a row, so I thought using a preparedStatement probably is a clever idea.

Here is my query :

String requestWithFirstName = "SELECT SE.ELEMENT_ID, SE.LASTNAME||' '||SE.FIRSTNAME AS ELEMENT, (SCORE(1)+SCORE(2))/2 AS SCORE "
                        + "FROM BL_SUSPICIOUS_ELEMENT SE "
                        + "WHERE CONTAINS(SE.LASTNAME, 'fuzzy({' || ? || '},' || ? || ',' || ? || ', weight)' , 1)>0 "
                        + "AND CONTAINS(SE.FIRSTNAME,  'fuzzy({' || ? || '},' || ? || ',' || ? || ', weight)' , 2)>0 "
                        + (type > 0 ? "AND SE.ELEMENT_TYPE_ID = ?" : "") 
                        + " ORDER BY SCORE DESC";

Everthings worked fine until we realized that the fuzzy methods doesn't perform well for splitted words like 'pikachu is my hero' and it is advised to created, in this case, 4 fuzzy search for 'pikachu' 'is' 'my' 'hero'. Not sure if this is true, but as I will run the query 28 000 times it's a good opportunity to see it in action.

So I tried to modify the query in this manner :

 "SELECT A.ELEMENT_ID, A.LASTNAME||' '||A.FIRSTNAME AS AKA, SCORE(1) AS SCORE "
        + "FROM BL_AKA A, BL_SUSPICIOUS_ELEMENT SE " 
        + "WHERE CONTAINS(A.LASTNAME, ?, 1)>0 " 
        + "AND SE.ELEMENT_ID = A.ELEMENT_ID "
        + (type > 0 ? "AND SE.ELEMENT_TYPE_ID = ?": "") 
        + " ORDER BY SCORE DESC";

In this case, ? will be set to :

'fuzzy({Burnham},70,4,weight),fuzzy({Investors},70,4,weight),fuzzy({Trust},70,4,weight)'

The query seems fine, running on sql dev. However, with Java, I get the following error :

ORA-20000: Oracle Text error: DRG-50900: text query parser error on line 1, column 30
DRG-50920: part of phrase not itself a phrase or equivalence
DRG-50900: text query parser error on line 1, column 30
DRG-50920: part of phrase not itself a phrase or equivalence

Any advice ? I wonder if this is the same situation as the in statement (impossible to create a select * from pokemon where monster in (?))

Thanks !


回答1:


When you use a prepared statement in java, it will set the parameter according to the method you use. So

String s = "'fuzzy({Burnham},70,4,weight),fuzzy({Investors},70,4,weight),fuzzy({Trust},70,4,weight)'";
statement.setString(s);

will be escaped again and results in:

'''fuzzy({Burnham},70,4,weight),fuzzy({Investors},70,4,weight),fuzzy({Trust},70,4,weight)'''

Try to set the parameter without the quotes.

You can create an IN (?) statement. But you will have to add a questionmark for every parameter: WHERE monster IN (?,?,?,?,?,?)...



来源:https://stackoverflow.com/questions/7723601/preparedstatement-with-contains-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!