Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Boolean Full Text Match searches - how to give preference to exact phrases without requiring them?

Status
Not open for further replies.

Katerine

Programmer
Mar 9, 2001
234
US
Hi,
So, I've got a table with a fulltext index, and my client wants search terms to be exclusive (that is, all search terms MUST be included in the record, or the record should not appear in the search results). So I've got the query set up as a boolean-type query (because, AFAIK, that's the only way to have all search terms be required), where a search for, say, calcium oxide translates as a search for +calcium* +oxide* (they want to allow partial word matches).

So far, so good.

Now, in addition to that, my client would like sort preference given to exact phrase matches, without requiring exact phrase matches. So, for example, a search for:
acetate solution
should, again, only return search results with both "acetate" and "solution" somewhere in the results. Partial words are ok. It can also return LEAD ACETATE AQUEOUS SOLUTION... but that result should appear below all of the results that have the exact phrase, acetate solution. So it might return:
AMMONIUM ACETATE SOLUTION
CALCIUM ACETATE SOLUTION
MAGNESIUM ACETATE SOLUTION
SODIUM ACETATE SOLUTION
COPPER II ACETATE 3% AQUEOUS SOLUTION CUPRIC ACETATE
LEAD ACETATE AQUEOUS SOLUTION
SODIUM ACETATE BUFFER SOLUTION PH 5.2 +/- 0.05 AT 25 C MOLECULAR BIOLOGY REAGENT S7899
SODIUM ACETATE BUFFER SOLUTION PH 7.0 +/- 0.05 AT 250C MOLECULAR BIOLOGY REAGENT S2404


...in that order (that is, in order of whether there's an exact phrase match, and then in alphabetical order).

According to enclosing a search term in double-quotes makes an exact phrase required, not optional. So is this even possible, to require all search terms be present in the results, and allow partial word matches, and make exact phrases optional, and sort exact phrases first?

Thanks! :)

Katie
 
...(that is, all search terms MUST be included in the record,

Surely that would be inclusive rather than exclusive, unless your usage of 'exclusive' is different to the normal usage, and if it is we need a definition of your usage.

Now, in addition to that, my client would like sort preference given to exact phrase matches, without requiring exact phrase matches.

That would be a contradiction in terms.

What it sounds like what you are really asking for is a Google type search, where results are weighted by relevance to the user search term.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Unfortunately, no. That would be simpler.

Using the above "acetate solution" example:

A search for acetate solution can only return records where both "acetate" and "solution" appear in the name. If only one of those words appears in the name, then it should not return the record at all. They're very insistent on this point. They want the number of records returned to be strictly limited as much as humanly possible.

At the same time, they want records with the exact phrase, acetate solution to be given preference over records that have both words, but not in that exact order (or those words but with words in between).

Is there a way to do this?

Thanks. :)

Katie
 
A search for acetate solution can only return records where both "acetate" and "solution" appear in the name. If only one of those words appears in the name, then it should not return the record at all.

Use a wildcard search

Code:
SELECT 'fields' FROM 'table' WHERE 'field' LIKE '%word1%' AND field LIKE '%word2%';


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Thanks. That part's already done, though. The question is whether it's possible to rank exact phrase matches as more relevant (while at the same time requiring all words be present in the returned records, and without limiting to exact phrase matches only).

Katie
 
The question is whether it's possible to rank exact phrase matches as more relevant

Yes, but that is much easier with an inverted index (how Google works) as I suggested in my first post. you have to 'score' the words in a document and calculate a 'relevance weighting'

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
You could UNION your data

SELECT 1 as rank , 'fields' FROM 'table' WHERE 'field' = '%word1%'
union all
SELECT 2 as rank , 'fields' FROM 'table' WHERE 'field' LIKE '%word1%' AND field LIKE '%word2%';

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top