2

Background

I am currently re-writing a search engine...which I originally wrote a couple years ago in a hurry :)

Today, the search encompasses ~5 different internal business objects/tables: Users, Organizations, News Headlines, etc. So when the user types in "The Quick Brown Fox" into the search box, he/she will get results of each type of business object.

Each business object has an associated stored procedure: spSearchUsers, spSearchOrganizations, etc.

The problem

Now, for each search, I am calling each stored procedure (via a BL -> Data Access Layer) multiple times for each business object type. For example, when the user searches for "The Quick Brown Fox", I send down the entire string unaltered, but also split the string on each single space, and search each word individually.

In this case, each stored procedure is called 5 times...a total of 25 separate database calls for a single search. I don't feel like this typical...but it still runs pretty quick. It takes roughly 4-5 seconds per search.

What I'd like to create

A single "master" stored procedure that takes the entire search string "The Quick Brown Fox" that in turn calls each individual business object stored procedure (spSearchUsers, spSearchOrganizations, etc), performs a little bit of logic, and returns 1 result set.

So 1 search... 1 result.

Can this be achieved using stored procedures, functions, and/or user-defined table types?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Robert
  • 265
  • 4
  • 14
  • What specific DB technology are you using? MS, Oracle, MySql etc.. – Erik Philips Oct 29 '12 at 19:46
  • What is returned in your spSearchUsers, spSearchOrganizations, etc. calls? – seekerOfKnowledge Oct 29 '12 at 19:48
  • if your database and your tables structure allows for it, then you could create a view that put together the fields to be searched, the primary keys of the tables and other needed info. Then create a stored procedure that search on the view. – Steve Oct 29 '12 at 19:50
  • @seeker basically each sp returns a "SELECT *" of each table. Obviously column defs are very different across each table :) – Robert Oct 29 '12 at 19:56
  • @Steve I really like that idea... I hadn't thought of that (probably b/c I haven't used them in a very long time.) Thanks – Robert Oct 29 '12 at 20:01
  • This doesn't answer the question (that's why it's a comment), but you would be MUCH better off using Lucene (or Solr) – BlackTigerX Oct 29 '12 at 21:44

2 Answers2

0

If you were using Ms-Sql you could create a Full Text Index that would allow you to search using all the terms without the need to alter the text. Additionally, Stored Procedures can return back multiple results sets, so you could create a new stored procedure to run each query and return all the records back, or one that calls all your previous stored procedures individually (shown below).

CREATE PROCEDURE [dbo].[DoMassiveSearch]
@searchText varchar(200) = null
AS
BEGIN
  EXEC spSearchUsers @searchText

  EXEC spSearchOrganizations @searchText
END
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
0

Supposing you could create a view like this

CREATE VIEW vSearchFields
as

    SELECT "U" as tableName, IDUser as ID, UserName as searchField 
    FROM USERS

UNION

    SELECT "O" as tableName, IDOrganizations as ID, OrgName as searchField
    FROM Organizations

UNION

    SELECT 'N' as tableName, IDNews as ID, newsText as searchField
    FROM News 

UNION

    SELECT 'H' as tableName, IDHeadline as ID, headlineText as searchField
    FROM Headlines

Now you could write a simple stored procedure that search for the text in the seachField

CREATE PROCEDURE [dbo].[searchText]
@textToSearch nvarchar(2000)
AS
BEGIN
    SELECT * FROM vSearchFields WHERE searchField = @textToSearch
END

The next step will be the introduction of a function to split the input text and do a repetitive search using the text parts, or, if your db version allows (2008 and after), use a Table Valued Parameter to pass a splitted string as a table from C# code to your stored procedure.

Split Function in T-SQL
Table Valued Parameters

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286