I have three table as follows:
documents (id, content)
words (id, word)
word_document (word_id, document_id, count)
Words table have all the words occurred in all documents and word_document relate a word to a document and the count of that word in that document.
I want to write a query to search for two words and returns only the documents which have both word ordered by the sum of the count of both words in documents.
For example
DocA: green apple is not blue
DocB: blue apple is blue
DocC: red apple is red
now a search for apple and blue returns:
DocA, 3
DocB, 2
becaus:
DocA contains both words and 3 of them
DocB contains both words and 2 of them
DocC only contains one word
I used intersect successfully but it doesn’t return the count sum and no order.
For those who want this, this only works:
You can create temp table from the inner query and execute outer on that. It can be done recursively for more words.