Fulltext - query all columns

Posts   
 
    
JRR avatar
JRR
User
Posts: 125
Joined: 07-Dec-2005
# Posted on: 08-Apr-2009 04:05:50   

Consider this sql predicate:


Contains(*,N'SearchText')

This thread - http://llblgen.com/tinyforum/Messages.aspx?ThreadID=13883 demonstrates how we can use multiple columns in a predicate with FieldFullTextSearchPredicate, but what about a wildcard (*) where all columns are used?

That way we don't have to code in each column manually.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 08-Apr-2009 10:17:43   

You can use Linq for this.

The example in the docs can be modified as such:

/// Class which is used to specify the call to the CONTAINS construct. 
public class NorthwindFunctions
{
    public static bool FullTextSearch(string toFind)
    {
        // empty body, as it's just here to make the query compile. The call is converted to a SQL function.
        return true;
    }
}

/// Class which defines the custom mapping between NorthwindFunctions.FullTextSearch and CONTAINS()
public class NorthwindFunctionMappings : FunctionMappingStore
{
    public NorthwindFunctionMappings() : base()
    {       
         this.Add(new FunctionMapping(typeof(NorthwindFunctions), "FullTextSearch", 1, "CONTAINS(*, {0})"));
    }
}


//...

// these constructs are now allowing the developer to actually call CONTAINS from within 
// the Linq query. We have to pass the custom FunctionMappingStore 'NorthwindFunctionMappings' to 
// the LinqMetaData first:
metaData.CustomFunctionMappings = new NorthwindFunctionMappings();
// fetch the employees which have 'BA' in their Notes field which is Full text search enabled.
var q = from e in metaData.Employee
        where NorthwindFunctions.FullTextSearch("BA")
        select e;
JRR avatar
JRR
User
Posts: 125
Joined: 07-Dec-2005
# Posted on: 08-Apr-2009 16:05:38   

Thanks for the reply. I haven't learned linq yet, still using straight llblgen for data calls. Will this fulltext function class be callable from a standard predicate expression?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 08-Apr-2009 16:30:43   

Nope, this function mapping is used from Linq only.

But you can try to cook this using a DBFunctionCall as an expression, and using constants for function parameters

JRR avatar
JRR
User
Posts: 125
Joined: 07-Dec-2005
# Posted on: 09-Apr-2009 08:40:52   

Since this doesn't work:


select * from dbo.myTable
where CONTAINS(*,'text') = 1;

How can I include a function call as a predicate without assigning values?

I can create the function in c#:

DbFunctionCall containsFunction = new DbFunctionCall("CONTAINS", new object[] { "*", value });

but how does I include it in a predicate?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 09-Apr-2009 11:24:27   

I thought something like the following should work?

select * from dbo.myTable
where CONTAINS(*,'text') = true

Another option is to derive your own class from the FieldFullTextSearchPredicate class, and override the ToQueryText() method.