locked
Querying Windows Search RRS feed

  • Question

  • Hi,

    I would like to add the Windows Search Role, and setup it up so it will index properties and file content.

    Once that is done and the index completed, I would like to query the index for a particular phrase, and hopelly what I would get back from would be

    The file name and the file location (path)

    Is this posible? what tools can I use to query it? vba? vb? sql? and most important will I get the file name and full path?

    Thanks you all

    Bre-x

    Wednesday, October 20, 2010 5:09 PM

All replies

  • Hi Bre-x.

    You can see an example here: http://code.msdn.microsoft.com/windowssearch

    There are some variant how to query WDS. In comment for project WSOleDB.zip you will find all you need.

    Friday, October 22, 2010 11:31 AM
  • Once that is done and the index completed, I would like to query the index for a particular phrase, and hopelly what I would get back from would be

    The file name and the file location (path)

    Is this posible? what tools can I use to query it? vba? vb? sql? and most important will I get the file name and full path?

    Yes, it is possible using many different languages.

    Basically one of the easiest way to query the index is to use ADO. The index engine support a subset of the SQL language.

    Here is an extract from one of my programs (Delphi code). In this extract, I use an ADO connection and a ADO recorset. I do the query and copy the result to a string grid for display.

    procedure TAppBaseForm.ExecSqlSearch(const SQL : String);
    var
        AdoConnection : _Connection;
        AdoRecordSet  : _RecordSet;
        Fld           : Integer;
        Row           : Integer;
    begin
        AdoConnection := CoConnection.Create;
        AdoConnection.CursorLocation := 3; // User client
        AdoConnection.Open('provider=Search.CollatorDSO.1;EXTENDED PROPERTIES="Application=Windows"', '', '', 0);

        AdoRecordSet := CoRecordset.Create;
        AdoRecordSet.Open(SQL, AdoConnection, adOpenForwardOnly, adLockReadOnly, adCmdText);
        try
            StringGrid1.RowCount := 2;
            StringGrid1.ColCount := AdoRecordSet.Fields.Count + 1;
            for Fld := 0 to AdoRecordSet.Fields.Count - 1 do
                StringGrid1.Cells[Fld + 1, 0] := AdoRecordSet.Fields.Item[Fld].Name;

            if AdoRecordSet.Bof and AdoRecordSet.Eof then begin
                for Fld := 0 to AdoRecordSet.Fields.Count - 1 do
                    StringGrid1.Cells[Fld + 1, 1] := '';
                Exit;
            end;

            Update;
            try
                StringGrid1.RowCount := 1000;
                Row                  := 1;
                AdoRecordSet.MoveFirst;
                StringGrid1.ColCount := AdoRecordSet.Fields.Count + 1;
                StringGrid1.ColWidths[0] := 30;
                while not AdoRecordSet.EOF do begin
                    StringGrid1.Cells[0, Row] := IntToStr(Row);
                    for Fld := 0 to AdoRecordSet.Fields.Count - 1 do begin
                        if VarIsNull(AdoRecordSet.Fields.Item[Fld].Value) then
                            StringGrid1.Cells[Fld + 1, Row] := 'null'
                        else if VarIsArray(AdoRecordSet.Fields.Item[Fld].Value) then
                            StringGrid1.Cells[Fld + 1, Row] := VarArrayToString(AdoRecordSet.Fields.Item[Fld].Value)
                        else
                            StringGrid1.Cells[Fld + 1, Row] := AdoRecordSet.Fields.Item[Fld].Value;
                    end;
                    Row := Row + 1;
                    if Row >= StringGrid1.RowCount then
                        StringGrid1.RowCount := StringGrid1.RowCount + 1000;
                    AdoRecordSet.MoveNext;
                end;
                StringGrid1.RowCount := Row + 1;
            finally
                DisplayMemo.Lines.EndUpdate;
            end;
        finally
            AdoRecordSet.Close;
            AdoConnection.Close;
        end;
    end;

    The SQL query could be something like:

    SELECT System.ItemPathDisplay, System.ItemName, System.FileName, System.ItemUrl, System.Author   FROM SYSTEMINDEX WHERE CONTAINS("System.FileName", 'SearchAPI')

    In the colum, System.ItemUrl, you get the full path you need.

    Hopes this help.


    Francois Piette http://www.overbyte.be
    Friday, October 22, 2010 2:28 PM