 |
Its a console application that utilizes Filter object while indexing
miscellaneous document types and Query – to run full-text queries against documents
stored on the database server.
Files folder should contain a set of files, DataObjects.Net installer installs
5 files into this folder: Sample.doc, Sample.xls, Sample.pdf, Sample.htm and Sample.txt.
Sample output
DataObjects.Net: Full-text filters demo
Select database server to use:
1) Microsoft SQL Server
2) Oracle
3) Native Oracle
4) SAP DB
> 1
Selected: Microsoft SQL Server.
Reading product key...
Building domain...
Connection URL: mssql://localhost/DataObjectsDotNetDemos
Driver: Microsoft SQL Server\MSDE database driver for DataObjects.Net
OK
Creating data...
Processing: "Sample.doc"
Filtered content: Sample Microsoft Word File You can find this file in Demo_Fu
llTextFilters by searching for this word: COM. Sample Word File Copyright (C) X-
tensive.com, INLINE, 2003-2004 1
Processing: "Sample.htm"
Filtered content: License Agreement for All Editions of DataObjects.Net Licens
e Agreement for All Editions of DataObjects.NetT Summary -This summary is provid
ed for your quick reference only and is not part of the license agreement. Pleas
e read the entire agreement. You may evaluate the DataObjects.Net f...
Processing: "Sample.pdf"
Filtered content: ROI Calculation Copyright (C) X-tensive.com, INLINE, 2003-20
04 1 Return of Investment Calculation DataObjects.Net is intended to be used as
transparent persistence layer for application business entities. It completely
solves a set of typical problems of ADO.NET-based solutions, including...
Processing: "Sample.txt"
Filtered content: DataObjects.Net Available Source Code Package --------------
-------------------------------------------------------------- Important notice:
Please note that further information is not a binding License Agreement, you sh
ould refer to the License Agreement bundled with the product for detai...
Processing: "Sample.xls"
Filtered content: Sheet3 Sheet2 Sheet1 This is a sample Microsoft Office Excel
File You can find this file in Demo_FullTextFilters by searching for this word:
Excel.
OK
Full-text search...
Type the text to search: ADO licensing
Searching for "ADO licensing":
Found files: 3
Sample.pdf
Sample.htm
Sample.txt
OK
Press Enter to close...
Model.cs
using System;
using System.IO;
using System.Data;
using DataObjects.NET;
using DataObjects.NET.Attributes;
using DataObjects.NET.FullText;
namespace Demo_FullTextFilters.Model
{
public abstract class FtFile: FtObject
{
[Length(256)]
[Indexed]
public abstract string Name {get; set;}
[NotPersistent]
public virtual string Extension {
get {
return Path.GetExtension(Name).Substring(1);
}
}
[LoadOnDemand]
public abstract byte[] Content {get; set;}
public override string ProduceFtData(Culture culture) {
return
base.ProduceFtData(culture) +
"\n File name: "+Name +
"\n Content: "+Filter.Create(Extension).GetFilteredContent(Content);
}
}
}
FullTextFilters.cs
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Globalization;
using System.Data;
using System.Data.SqlClient;
using DataObjects.NET;
using DataObjects.NET.FullText;
using DataObjects.NET.RuntimeServices;
using Demo_FullTextFilters.Model;
namespace Demo_FullTextFilters
{
class Demo_FullTextFilters
{
static Domain domain;
static string connectionUrl = "";
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("DataObjects.Net: Full-text filters demo\n");
SelectDatabase();
CreateDomain();
CreateData();
FullTextQuery();
Console.Write("\nPress Enter to close... ");
Console.ReadLine();
}
static void SelectDatabase()
{
while (connectionUrl=="") {
Console.Write("Select database server to use:\n" +
"1) Microsoft SQL Server\n" +
"2) Oracle\n" +
"3) Native Oracle\n" +
"4) SAP DB\n" +
"> ");
string dbType = Console.ReadLine();
switch (dbType) {
case "1":
Console.WriteLine("Selected: Microsoft SQL Server.");
connectionUrl = "mssql://localhost/DataObjectsDotNetDemos";
break;
case "2":
Console.WriteLine("Selected: Oracle.");
connectionUrl = "oracle://admin:admin@localhost/Demos";
break;
case "3":
Console.WriteLine("Selected: Native Oracle.");
connectionUrl = "nativeoracle://admin:admin@localhost/Demos";
break;
case "4":
Console.WriteLine("Selected: SAP DB.");
connectionUrl = "sapdb://ADMIN:ADMIN@localhost/DEMOS";
break;
default:
Console.WriteLine("Illegal selection.");
break;
}
}
}
static void CreateDomain()
{
Console.WriteLine("Reading product key...");
string productKeyFile = @"..\..\..\..\ProductKey.txt";
string productKey = "";
if (File.Exists(productKeyFile))
using (StreamReader sr = new StreamReader(productKeyFile)) {
productKey = sr.ReadToEnd().Trim();
}
Console.WriteLine("");
Console.WriteLine("Building domain...");
domain = new Domain(connectionUrl, productKey);
Console.WriteLine("Connection URL: {0}",domain.ConnectionURL);
Console.WriteLine("Driver: {0}",domain.Driver.Info.Description);
domain.RegisterCulture(new Culture("En","U.S. English", new CultureInfo("en-us",false)));
domain.Cultures["En"].Default = true;
domain.RegisterTypes("Demo_FullTextFilters.Model");
#if DEBUG
domain.DebugInfoOutputFolder = @"C:\Debug";
#endif
domain.Build(DomainUpdateMode.Recreate);
Console.WriteLine(" OK");
Console.WriteLine();
}
static void CreateData()
{
Console.WriteLine("Creating data...");
using (Session s = domain.CreateSession()) {
TransactionController tc = s.CreateTransactionController();
try {
string dirPath = Directory.GetCurrentDirectory()+@"\..\..\Files";
foreach (string fileName in Directory.GetFiles(dirPath)) {
Console.WriteLine(" Processing: \"{0}\"", Path.GetFileName(fileName));
FtFile file = (FtFile)s.CreateObject(typeof(FtFile));
file.Name = Path.GetFileName(fileName);
byte[] buffer;
using (FileStream fr = File.OpenRead(fileName)) {
buffer = new byte[fr.Length];
fr.Read(buffer, 0, (int)fr.Length);
}
file.Content = buffer;
string filtered = Filter.Create(file.Extension).GetFilteredContent(buffer);
filtered = Regex.Replace(filtered, @"\s+", " ").Trim();
if (filtered.Length>290)
filtered = filtered.Substring(0,290)+"...";
Console.WriteLine(" Filtered content: {0}", filtered);
Console.WriteLine();
}
// Let's update full-text data immediately!
FtIndexer ftIndexer = (FtIndexer)s.CreateService(typeof(FtIndexer), 1000000);
ftIndexer.Execute();
tc.Commit();
}
catch {
tc.Rollback();
throw;
}
}
Console.WriteLine(" OK");
Console.WriteLine();
}
static void FullTextQuery()
{
Console.WriteLine("Full-text search...");
if (!domain.ExtractedDatabaseModel.ExtractedInfo.FullTextIndexingRunning) {
Console.WriteLine(" Full-text search\\indexing isn't available.");
Console.WriteLine(" LikeExpression mode (slow) will be used.");
}
string searchString;
do {
Console.Write(" Type the text to search: ");
searchString = Console.ReadLine();
} while (searchString.Trim()=="");
Console.WriteLine(" Searching for \"{0}\":", searchString);
using (Session s = domain.CreateSession()) {
TransactionController tc = s.CreateTransactionController();
try {
DataObject[] result;
if (domain.ExtractedDatabaseModel.ExtractedInfo.FullTextIndexingRunning)
result = s.CreateQuery(
"Select FtFile instances " +
"textsearch freetext "+s.Utils.QuoteString(searchString)+" " +
"order by {FullTextRank} desc").ExecuteArray();
else
result = s.CreateQuery(
"Select FtFile instances " +
"textsearch likeExpression "+s.Utils.QuoteString("%"+searchString+"%")+" " +
"order by {ID}").ExecuteArray();
Console.WriteLine(" Found files: {0}", result.Length);
foreach (FtFile f in result)
Console.WriteLine(" "+f.Name);
tc.Commit();
}
catch {
tc.Rollback();
throw;
}
}
Console.WriteLine(" OK");
}
}
}
|
|