SQL Server Forum / Other Technologies / Full-Text Search / September 2007
FTS Weighted Values Implementation Problem
|
|
Thread rating:  |
Organic Man - 01 Sep 2007 17:54 GMT I don't know if this is an appropriate problem for this forum but will pose it and if anyone can help, I would be very grateful.
I can run this weighted values FTS in Management Studio on a SQL Server 2005 db:
SELECT FT_TBL.FullDocNo, FT_TBL.DocType, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT (meditation weight (.8), exercise weight (.4), techniques weight (.2) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC
I can paste that Select statement into my Visual Basic FTS application and it works fine.
But when I tried to parameterize with search variables for runtime input from the user via textboxes, I ran into syntax problems. So I backed up and tried to get just the searchterm to work first (and will get to the weighted value later):
Dim SearchTerm1 As String SearchTerm1 = SearchTextBox1.Text.ToString
Dim SearchTerm2 As String SearchTerm2 = SearchTextBox2.Text.ToString
Dim SearchTerm3 As String SearchTerm3 = SearchTextBox3.Text.ToString
Dim conn As New SqlConnection("Data Source=xxx") Dim ds As New DataSet conn.Open()
'THIS SELECT STATEMENT WORKS 'Dim adp As New SqlDataAdapter("Select FullDocNo, DocType From FullDocuments WHERE CONTAINS (SectionText, ' """ & SearchTerm1 & """ ')", conn)
'THIS SELECT STATEMENT ALSO WORKS 'Dim adp As New SqlDataAdapter("SELECT FT_TBL.FullDocNo, FT_TBL.DocType, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT (meditation weight (.8), exercise weight (.4), techniques weight (.2) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC ", conn)
'THIS SELECT STATEMENT THROWS A SYNTAX ERROR Dim adp As New SqlDataAdapter("SELECT FT_TBL.FullDocNo, FT_TBL.DocType, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT (' """ & SearchTerm1 & """ ' weight (.8), ' """ & SearchTerm2 & """ ' weight (. 4), ' """ & SearchTerm3 & """ ' weight (.2) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC ", conn)
adp.Fill(ds) DataGridView1.DataSource = ds.Tables(0) conn.Close()
I typed meditation, exercise, and techniques into the three search textboxes.
The error states: Incorrect syntax near 'meditation'.
Any thoughts on what is wrong with my Select statement?
Thanks in advance for any help provided.
Jean-Pierre Riehl - 02 Sep 2007 10:49 GMT Hi,
...ISABOUT (' """ & SearchTerm1 & """ ' weight (.8), ... gives ...ISABOUT (' "a useless phrase " ' weight (.8), ...
The syntax error comes from the simple quotes surrounding your search phrase. The right syntax is :
...ISABOUT ("a useless phrase " weight (.8), ...
 Signature Jean-Pierre Riehl http://blog.djeepy1.net http://www.bewise.fr
>I don't know if this is an appropriate problem for this forum but will > pose it and if anyone can help, I would be very grateful. [quoted text clipped - 65 lines] > > Thanks in advance for any help provided. Organic Man - 02 Sep 2007 23:47 GMT Hi Jean-Pierre,
I modified my Select statement as you suggested:
Dim adp As New SqlDataAdapter("SELECT FT_TBL.FullDocNo, FT_TBL.DocType, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT ("SearchTerm1" weight (.8), "SearchTerm2" weight (.4), "SearchTerm3" weight (.2) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC ", conn)
I am getting an error on "SearchTerm1" (but not on SearchTerm2 or SearchTerm3) which states:
Error 1 Comma, ')', or a valid expression continuation expected.
I've tried pasting in the variable directly to make sure it isn't a typo or such. Any thoughts?
On Sep 2, 5:49 am, "Jean-Pierre Riehl" <jean-pierre.ri...@b-e-w-i-s- e.fr> wrote:
> Hi, > [quoted text clipped - 9 lines] > -- > Jean-Pierre Riehlhttp://blog.djeepy1.nethttp://www.bewise.fr Jean-Pierre Riehl - 03 Sep 2007 12:34 GMT Does your SearchTerm variables contain characters like parenthesis or comma ? Can you dump the full T-SQL before pass it to DataAdapter ?
-- Jean-Pierre
> Hi Jean-Pierre, > [quoted text clipped - 30 lines] >> -- >> Jean-Pierre Riehlhttp://blog.djeepy1.nethttp://www.bewise.fr Organic Man - 03 Sep 2007 14:06 GMT The SearchTerm variables are: SearchTerm1, SearchTerm2, SearchTerm3.
Wondering if the 1, 2, 3 might be causing a problem, I changed the variable names to: SearchTermA, SearchTermB, SearchTermC but still have same error message on SearchTermA.
Assigning a value to each variable (at design time) doesn't change the error message. Not able to assign a value at runtime since the program will not execute.
I am not sure what you mean by "dump". Are you referring to the TSQL equivalent Backup?
>On Sep 3, 7:34 am, "Jean-Pierre Riehl" <jean-pierre.ri...@b-e-w-i-s-e.fr> wrote: > Does your SearchTerm variables contain characters like parenthesis or comma [quoted text clipped - 3 lines] > -- > Jean-Pierre Jean-Pierre Riehl - 03 Sep 2007 15:09 GMT No, I meant output T-SQL string to Console in debug mode.
Dim tsql as String = "Select ......" Console.WriteLine(tsql)
Dim adp as SqlDataAdapter = new SqlDataAdapter(tsql, conn)
Have you tried your T-SQL command directly in Management Studio ?
 Signature Jean-Pierre Riehl http://blog.djeepy1.net http://www.bewise.fr
> The SearchTerm variables are: SearchTerm1, SearchTerm2, SearchTerm3. > [quoted text clipped - 20 lines] >> -- >> Jean-Pierre Organic Man - 03 Sep 2007 15:59 GMT I tried to output ot console with two variations, but the build failed:
'ERROR = "END OF STATEMENT EXPECTED" Dim tsql As String = "SELECT FT_TBL.FullDocNo, FT_TBL.DocType, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT ("SearchTerm1" weight (.8), "SearchTerm2" weight (.4), "SearchTerm3" weight (.2) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC " Console.WriteLine(tsql) Dim adp As SqlDataAdapter = New SqlDataAdapter(tsql, conn)
'ERROR = ')' EXPECTED - SEARCHTERM1 WITH SQUIGGLY UNDERLINE. Dim tsql As String = ("SELECT FT_TBL.FullDocNo, FT_TBL.DocType, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT ("SearchTerm1" weight (.8), "SearchTerm2" weight (.4), "SearchTerm3" weight (.2) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC ", conn) Console.WriteLine(tsql) Dim adp As SqlDataAdapter = New SqlDataAdapter(tsql, conn)
I then tried a series of queries in Management Studio. They all executed successfully, but the last one which included variables and weight values yielded 0 results.
SELECT FullDocNo FROM FullDocuments WHERE CONTAINS (SectionText, 'gold') *** 1670 results ***
DECLARE @SearchTerm1 nvarchar(10) SET @SearchTerm1 ='gold' SELECT FullDocNo FROM FullDocuments WHERE CONTAINS (SectionText, @SearchTerm1) *** 1670 results ***
SELECT FullDocNo FROM FullDocuments WHERE CONTAINS (SectionText, 'silver') *** 501 results ***
DECLARE @SearchTerm2 nvarchar(10) SET @SearchTerm2 ='silver' SELECT FullDocNo FROM FullDocuments WHERE CONTAINS (SectionText, @SearchTerm2) *** 501 results ***
SELECT FullDocNo FROM FullDocuments WHERE CONTAINS (SectionText, 'copper') *** 619 results ***
DECLARE @SearchTerm3 nvarchar(10) SET @SearchTerm3 ='copper' SELECT FullDocNo FROM FullDocuments WHERE CONTAINS (SectionText, @SearchTerm3) *** 619 results ***
SELECT FT_TBL.FullDocNo, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT ("gold" weight (.8), "silver" weight (.4), "copper" weight (. 2) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC *** 2067 results ***
DECLARE @SearchTerm1 nvarchar(10) SET @SearchTerm1 ='gold' DECLARE @SearchTerm2 nvarchar(10) SET @SearchTerm2 ='silver' DECLARE @SearchTerm3 nvarchar(10) SET @SearchTerm3 ='copper' SELECT FT_TBL.FullDocNo, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT (@SearchTerm1 weight (.8), @SearchTerm2 weight (.4), @SearchTerm3 weight (.2) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC *** 0 results ***
>On Sep 3, 10:09 am, "Jean-Pierre Riehl" <jean-pierre.ri...@b-e-w-i-s-e.fr> wrote: > No, I meant output T-SQL string to Console in debug mode. [quoted text clipped - 8 lines] > -- > Jean-Pierre Riehlhttp://blog.djeepy1.nethttp://www.bewise.fr Jean-Pierre Riehl - 03 Sep 2007 16:16 GMT Ok, I understand. Your syntax error is a VB.NET compilation error under Visual Studio. We misunderstood each other.
With VB.NET :
Dim tsql as String = String.Format("SELECT FT_TBL.FullDocNo, FT_TBL.DocType, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT (""" + SearchTerm1 + """ weight (.8), """ + SearchTerm2 + """ weight (.4), """ + SearchTerm3 + """ weight (.2) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC 'string above is on a unique line of code Dim adp as SqlDataAdapter = new SqlDataAdapter(tsql, conn)
With T-SQL, full-text pattern is a string (nvarchar). In your last query, it consider @Term as literal value. You also have to concatenate query :
[...] 'ISABOUT ("' + @SearchTerm1 + '" weight (.8), "' + @SearchTerm2 + '" weight (.4),[...]
 Signature Jean-Pierre Riehl http://blog.djeepy1.net http://www.bewise.fr
>I tried to output ot console with two variations, but the build > failed: [quoted text clipped - 87 lines] >> -- >> Jean-Pierre Riehlhttp://blog.djeepy1.nethttp://www.bewise.fr Organic Man - 03 Sep 2007 17:39 GMT FANTASTIC!!! Sorry for the confusion in how I originally presented my situation.
Now for the second part of my problem wherein I allow the user to type a number into a textbox which assigns the weight value for each SearchTerm.
I don't want the user to have to work with decimals directly, so I will have a label with the instruction to type a number between 1-9 into each WeightTextBox corresponding to each SearchTerm. Then I will convert it to decimal in code. For SearchTerm1 I have created WeightTextBox1 and written this code:
Dim Weight1 As Integer Weight1 = WeightTextBox1.Text Dim RealWeight1 As Decimal RealWeight1 = Weight1 * 0.1
I adjusted my SELECT statement using the syntax you provided for the SearchTerm variable which is applied to the RealWeight1 variable:
Dim adp As New SqlDataAdapter("SELECT FT_TBL.FullDocNo, FT_TBL.DocType, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT (""" + SearchTerm1 + """ weight (""" + RealWeight1 + """), """ + SearchTerm2 + """ weight (.4), """ + SearchTerm3 + """ weight (.2) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC ", conn)
I step through the code in Debugger mode and the value of RealWeight1 = 0.8D
But I get this error when it comes to the SELECT statement:
System.InvalidCastException was unhandled Message="Conversion from string "SELECT FT_TBL.FullDocNo, FT_TBL." to type 'Double' is not valid."
I was not sure if there was something wrong with my code syntax or if it was a datatype problem (should have used double instead of decimal). So I created a version with double datatype instead of decimal. The error message was the same.
I figured that the syntax using multiple quotes was suitable for string datatype (but not decimal or double), so I eliminated them and tried various ways of presenting RealWeight1, but always get syntax errors. FYI: When switching to double datatype the value was 0.8 instead of 0.8D that resulted from decimal datatype (although I doubt that is the root of the problem).
Thanks for your help so far and any further advice you can provide in helping me to get this functional.
>On Sep 3, 11:16 am, "Jean-Pierre Riehl" <jean-pierre.ri...@b-e-w-i-s-e.fr> wrote: > Ok, I understand. [quoted text clipped - 21 lines] > -- > Jean-Pierre Riehlhttp://blog.djeepy1.nethttp://www.bewise.fr Jean-Pierre Riehl - 03 Sep 2007 18:12 GMT It is another VB.NET syntax problem ;-)
You have to convert your numeric value to string with ToString() function. Double quote are useless as you expect that result : weight(0.8)
New query is : [...] 'ISABOUT (""" + SearchTerm1 + """ weight ( + RealWeight1.ToString() + ), """ + SearchTerm2 [...]
 Signature Jean-Pierre Riehl http://blog.djeepy1.net http://www.bewise.fr
> FANTASTIC!!! Sorry for the confusion in how I originally presented my > situation. [quoted text clipped - 79 lines] >> -- >> Jean-Pierre Riehlhttp://blog.djeepy1.nethttp://www.bewise.fr Organic Man - 03 Sep 2007 18:50 GMT Still getting a syntax error with ( + RealWeight1.ToString() + ),
System.Data.SqlClient.SqlException was unhandled Class=15 ErrorCode=-2146232060 LineNumber=1 Message="Syntax error near 'RealWeight1.ToString' in the full-text search condition 'ISABOUT ("gold" weight ( + RealWeight1.ToString() + ), "silver" weight (.4), "copper" weight (.2) )'."
When I stepped through it in debug mode, there was no value assigned to RealWeight1.To String().
If I cast to string datatype before the Select statement:
Dim Weight1 As Integer Weight1 = WeightTextBox1.Text Dim RealWeight1 As Double RealWeight1 = Weight1 * 0.1 RealWeight1 = RealWeight1.ToString
and use string syntax in the Select statement:
(""" + RealWeight1 + """),
and step through it in debugger, the value of RealWeight1 in the Select statement is 0.8 (as it should be). But this is a string value, not double or decimal.
I am wondering if the Select statement is looking for a double or decimal datatype for the weight value? So even if a string is provided with the correct value (e.g., 0.8), it is not accepted because it is the wrong datatype?
I am learning a lot here (I think), and am confident I will get this to work with your help. Greatly appreciate your patience and expertise.
>On Sep 3, 1:12 pm, "Jean-Pierre Riehl" <jean-pierre.ri...@b-e-w-i-s-e.fr> wrote: > It is another VB.NET syntax problem ;-) [quoted text clipped - 8 lines] > -- > Jean-Pierre Riehlhttp://blog.djeepy1.nethttp://www.bewise.fr Jean-Pierre Riehl - 05 Sep 2007 20:39 GMT Hi,
> Message="Syntax error near 'RealWeight1.ToString' in the full-text You wrote RealWeight1.ToString() as a literal string instead of a concatenation of values. Don't miss the parenthesis indicating to use ToString method on double object.
You shoud use String.Format routine to prepare your Full-Text query, it would be clearer in your mind. See MSDN documentation for examples.
> I am wondering if the Select statement is looking for a double or > decimal datatype for the weight value? So even if a string is > provided with the correct value (e.g., 0.8), it is not accepted > because it is the wrong datatype? The full-text query that you use as parameter in CONTAINSTABLE is a nvarchar parameter. So you have to build a string in VB.NET concatenating your variables (Double or other types). SQL Server takes care of type when parsing query.
 Signature Jean-Pierre Riehl http://blog.djeepy1.net http://www.bewise.fr
> Still getting a syntax error with ( + RealWeight1.ToString() + ), > [quoted text clipped - 48 lines] >> -- >> Jean-Pierre Riehlhttp://blog.djeepy1.nethttp://www.bewise.fr Organic Man - 06 Sep 2007 16:41 GMT I have been making it too hard (as usual).
Instead of trying to cast from double or decimal to string (or vice versa), I should have just started out with whole numbers between 0-9 as string data and concatenated into decimal string data. I thought the SELECT statement required double or decimal data. String is just fine all the way.
Dim Weight1 As String Weight1 = ("." + WeightTextBox1.Text)
Dim Weight2 As String Weight2 = ("." + WeightTextBox2.Text)
Dim Weight3 As String Weight3 = ("." + WeightTextBox3.Text)
Dim adp As New SqlDataAdapter("SELECT FT_TBL.FullDocNo, FT_TBL.DocType, KEY_TBL.RANK FROM FullDocuments AS FT_TBL INNER JOIN CONTAINSTABLE(FullDocuments, SectionText, 'ISABOUT (""" + SearchTerm1 + """ weight (""" + Weight1 + """), """ + SearchTerm2 + """ weight (""" + Weight1 + """), """ + SearchTerm2 + """ weight (""" + Weight3 + """) )' ) AS KEY_TBL ON FT_TBL.FullDocumentID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC ", conn)
Thanks for sticking with me till resolution.
Best wishes,
Dave
On Sep 5, 3:39 pm, "Jean-Pierre Riehl" <jean-pierre.ri...@b-e-w-i-s- e.fr> wrote:
> Hi, > [quoted text clipped - 7 lines] > You shoud use String.Format routine to prepare your Full-Text query, it > would be clearer in your mind. See MSDN documentation for examples.
|
|
|