<p>I'm looking for a pattern for performing a dynamic search on multiple tables.</p>

<p>I have no control over the legacy (and poorly designed) database table structure.</p>

<p>Consider a scenario similar to a resume search where a user may want to perform a search against any of the data in the resume and get back a list of resumes that match their search criteria.  Any field can be searched at anytime and in combination with one or more other fields.</p>

<p>The actual sql query gets created dynamically depending on which fields are searched.  Most solutions I've found involve complicated if blocks, but I can't help but think there must be a more elegant solution since this must be a solved problem by now.</p>

<hr>

<p>Yeah, so I've started down the path of dynamically building the sql in code.  Seems godawful.  If I really try to support the requested ability to query any combination of any field in any table this is going to be one MASSIVE set of if statements.  <em>shiver</em></p>

<hr>

<p>I believe I read that COALESCE only works if your data does not contain NULLs.  Is that correct?  If so, no go, since I have NULL values all over the place.</p>

[{'answer_id': 9889, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>As far as I understand (and I\'m also someone who has written against a horrible legacy database), there is no such thing as dynamic WHERE clauses.  It has NOT been solved.  </p>\n\n<p>Personally, I prefer to generate my dynamic searches in code.  Makes testing convenient.  Note, when you create your sql queries in code, don\'t concatenate in user input.  Use your @variables!</p>\n\n<p>The only alternative is to use the COALESCE operator.  Let\'s say you have the following table:</p>\n\n<pre><code>Users\n-----------\nName nvarchar(20)\nNickname nvarchar(10)\n</code></pre>\n\n<p>and you want to search optionally for name or nickname.  The following query will do this:</p>\n\n<pre><code>SELECT Name, Nickname\nFROM Users\nWHERE\n    Name = COALESCE(@name, Name) AND\n    Nickname =  COALESCE(@nick, Nickname)\n</code></pre>\n\n<p>If you don\'t want to search for something, just pass in a null.  For example, passing in "brian" for @name and null for @nick results in the following query being evaluated:</p>\n\n<pre><code>SELECT Name, Nickname\nFROM Users\nWHERE\n    Name = \'brian\' AND\n    Nickname =  Nickname\n</code></pre>\n\n<p>The coalesce operator turns the null into an identity evaluation, which is always true and doesn\'t affect the where clause.</p>\n'}, {'answer_id': 9918, 'author': 'DevelopingChris', 'author_id': 1220, 'author_profile': 'https://Stackoverflow.com/users/1220', 'pm_score': 1, 'selected': False, 'text': '<p>Search and normalization can be at odds with each other. So probably first thing would be to get some kind of "view" that shows all the fields that can be searched as a single row with a single key getting you the resume. then you can throw something like <a href="http://lucene.apache.org/java/docs/" rel="nofollow noreferrer">Lucene</a> in front of that to give you a full text index of those rows, the way that works is, you ask it for "x" in this view and it returns to you the key. Its a great solution and come recommended by joel himself on the podcast within the first 2 months IIRC.</p>\n'}, {'answer_id': 1081248, 'author': 'elviejo79', 'author_id': 54848, 'author_profile': 'https://Stackoverflow.com/users/54848', 'pm_score': 1, 'selected': False, 'text': '<p>What you need is something like <a href="http://sphinxsearch.com/" rel="nofollow noreferrer">SphinxSearch</a> (for MySQL) or <a href="http://lucene.apache.org/java/docs/" rel="nofollow noreferrer">Apache Lucene</a>.</p>\n\n<p>As you said in your example lets imagine a Resume that will composed of several fields: </p>\n\n<ul>\n<li>List item</li>\n<li>Name, </li>\n<li>Adreess, </li>\n<li>Education (this could be a table on its own) or </li>\n<li>Work experience (this could grow to its own table where each row represents a previous job)</li>\n</ul>\n\n<p>So searching for a word in all those fields with WHERE rapidly becomes a very long query with several JOINS.</p>\n\n<p>Instead you could change your framework of reference and think of the Whole resume as what it is a Single Document and you just want to search said document.</p>\n\n<p>This is where tools like Sphinx Search do. They create a FULL TEXT index of your \'document\' and then you can query sphinx and it will give you back where in the Database that record was found.</p>\n\n<p>Really good search results.</p>\n\n<p>Don\'t worry about this tools not being part of your RDBMS it will save you a lot of headaches to use the appropriate model "Documents" vs the incorrect one "TABLES" for this application.</p>\n'}]
['https://Stackoverflow.com/questions/9687', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1097/']