/// <summary> /// search for a specific record /// </summary> /// <param name="request"></param> /// <returns></returns> public IXQueryCollection Search(IXRequest request) { if (request == null || String.IsNullOrEmpty(request.Query)) { return(EmptySearchResult); } return(DoSearch(request)); }
static void Main(string[] args) { CommandLineArgs ca = new CommandLineArgs(args); if (ca.Arguments.Count < 1 && ca.Options.Count < 1) { Usage(); Environment.Exit(1); } SampleDataFeed fd = new SampleDataFeed(); bool reset = ca.Options.IsOptionEnabled("reset"); bool index = ca.Options.IsOptionEnabled("index"); bool forceindex = index && ca.Options["index"] == "force"; bool updateindex = index && ca.Options["index"] == "update"; bool usescoring = ca.Options.IsOptionEnabled("withscore"); if (reset) { fd.BuildStaticDataFeed(); } IndexerStorageMode mode = IndexerStorageMode.FS; if (ca.Options.IsOptionEnabled("mode")) { if (ca.Options["mode"] == "ram") { mode = IndexerStorageMode.RAM; } else if (ca.Options["mode"] == "fsram") { mode = IndexerStorageMode.FSRAM; } } bool doReindex = reset || (mode == IndexerStorageMode.RAM) || (mode != IndexerStorageMode.RAM && forceindex) || updateindex; Console.Write("Loading indexer in {0} mode... ", mode); long m0 = GC.GetTotalMemory(false); ISIndexer li = new ISIndexer(mode, IndexerAnalyzer.StandardAnalyzer); Console.WriteLine(" done."); if (doReindex) { Console.Write("Data reindexing had been requested: "); int origRow = Console.CursorTop; int origCol = Console.CursorLeft; ConsoleUtils.WriteAt("loading data feed", 0, 0, origRow, origCol); List <StaticDataFeed> data = fd.ReadStaticDataFeed(); if (reset || forceindex) { li.ClearAllIndex(); } int i = 1; foreach (StaticDataFeed f in data) { ConsoleUtils.WriteAt(String.Format("indexing item {0} ", i), 0, 0, origRow, origCol); li.AddUpdateLuceneIndex(f.ToIXDescriptor()); i++; } li.UpdateMemoryIndexFromFS(); data.Clear(); data = null; GC.Collect(); ConsoleUtils.WriteAt(" completed. ", 0, 0, origRow, origCol); Console.WriteLine(); } long m1 = GC.GetTotalMemory(false); Console.WriteLine("Index is using {0} kB of RAM", (m1 - m0) > 0 ? (m1 - m0) / (long)1024 : 0); if (mode == IndexerStorageMode.FSRAM && ca.Options.IsOptionEnabled("simfsramrefresh")) { Console.WriteLine("Simulate index refresh: {0}", li.UpdateMemoryIndexFromFS()); } if (ca.Arguments.Count > 0 && ca.Arguments[0] == "search") { Console.WriteLine("[press a key to execute search]"); Console.ReadLine(); IXRequest req = new IXRequest(ca.Arguments[1], 0, -1, (usescoring) ? IXRequestFlags.None : IXRequestFlags.UseScore); DateTime t1 = DateTime.Now; IXQueryCollection reply = li.Search(req); DateTime t2 = DateTime.Now; if (reply.Status == IXQueryStatus.Success) { foreach (IXQuery r in reply.Results) { Console.WriteLine("Result matching{2}: {0} \"{1}\"", r["id"].String, r["titolo"].String, (usescoring) ? String.Format(" at {0:n2}%", r.Score) : String.Empty); } } Console.WriteLine("Search stats: scope:{0} items status:{2} time:{1}ms results:{3} items", li.IndexSize, Convert.ToInt32((t2 - t1).TotalMilliseconds), reply.Status, reply.Count); } }
static void Main(string[] args) { CommandLineArgs ca = new CommandLineArgs(args); if (ca.Arguments.Count < 1 && ca.Options.Count < 1) { Usage(); Environment.Exit(1); } //SampleDataFeed fd = new SampleDataFeed(); // //bool reset = ca.Options.IsOptionEnabled("reset"); //bool index = ca.Options.IsOptionEnabled("index"); //bool forceindex = index && ca.Options["index"] == "force"; bool usescoring = ca.Options.IsOptionEnabled("withscore"); //if (reset) //{ // fd.BuildStaticDataFeed(); //} // //long m0 = GC.GetTotalMemory(false); //List<StaticDataFeed> data = fd.ReadStaticDataFeed(); //long m1 = GC.GetTotalMemory(false); //long ms1 = (m1 - m0) > 0 ? (m1 - m0) / (long)1024 : 0; //Console.WriteLine("Data feed: {0} items [mem: {1} kB]", data.Count, ms1); // //IndexerStorageMode mode = IndexerStorageMode.FS; //if (ca.Options.IsOptionEnabled("mode")) //{ // if (ca.Options["mode"] == "ram") // mode = IndexerStorageMode.RAM; // else if (ca.Options["mode"] == "fsram") // mode = IndexerStorageMode.FSRAM; //} // //Console.WriteLine("Index mode: {0}", mode); //IndexerInterop<StaticDataFeed> li = new IndexerInterop<StaticDataFeed>( // mode, // IndexerAnalyzer.StandardAnalyzer, // new StaticeDataFeedLI(delegate(int id) { return data.FirstOrDefault(p => p.id == id); })); // //long m2 = GC.GetTotalMemory(false); //long ms2 = (m2 - m1) > 0 ? (m2 - m1) / (long)1024 : 0; //long msA = (m2 - m0) > 0 ? (m2 - m0) / (long)1024 : 0; //Console.WriteLine("Index data: {0} items [mem: {1} kB - total so far: {2} kB]", li.IndexSize, ms2, msA); //if (reset || (mode == IndexerStorageMode.RAM) || (mode != IndexerStorageMode.RAM && forceindex)) //{ // Console.Write("Indexing item with identifier:"); // int origRow = Console.CursorTop; // int origCol = Console.CursorLeft; // // foreach (StaticDataFeed f in data) // { // ConsoleUtils.WriteAt(f.id.ToString() + "... ", 0, 0, origRow, origCol); // bool res = li.AddUpdateLuceneIndex(f); // ConsoleUtils.WriteAt((res == true ? "ok" : "fail") + " ", 0, 0, Console.CursorTop, Console.CursorLeft); // } // Console.WriteLine(); //} //if (mode == IndexerStorageMode.FSRAM && ca.Options.IsOptionEnabled("simfsramrefresh")) // Console.WriteLine("Simulate index refresh: {0}", li.UpdateMemoryIndexFromFS()); if (ca.Arguments.Count > 0 && ca.Arguments[0] == "search") { //WCFConnectionManager<InfoStream.Metadata.IInfoStreamService> zk = new WCFConnectionManager<IInfoStreamService>() //WCFServiceReferenceInfo zz = new WCFServiceReferenceInfo( //WCFSimpleConnectionManager<InfoStream.Metadata.IInfoStreamService> px = new WCFSimpleConnectionManager<IInfoStreamService>() // new WCFServiceReferenceInfo( // new WCFBindingInfo( // { // BindingType = WCFBindingType.BasicHttp, // CloseTimeout = new TimeSpan(0, 1, 0), // OpenTimeout = new TimeSpan(0, 1, 0), // SendTimeout = new TimeSpan(0, 1, 0), // ReceiveTimeout = new TimeSpan(0, 1, 0), // SecurityMode = WCFServiceSecurityMode.None, // TransferMode = System.ServiceModel.TransferMode.Buffered, // Protocol = WCFProtocolType.HTTP // }, // new Endpoint // ServiceReferenceName = "is" // }); IXQueryCollection found = null; //CoreService.InfoStreamServiceClient cli = new CoreService.InfoStreamServiceClient(); WCFConnectionManager <IInfoStreamService> cm = new WCFConnectionManager <IInfoStreamService>(wsConfiguration); Console.WriteLine("Press a key to execute the query"); Console.ReadLine(); string what = ca.Arguments[1]; IXRequest req = new IXRequest() { Query = ca.Arguments[1], Fields = new string[] { "id", "titolo" }, Skip = 0, Take = 1000, Flags = IXRequestFlags.UseScore }; DateTime t1 = DateTime.MinValue; DateTime t2 = DateTime.MinValue; using (WCFGenericProxy <IInfoStreamService> cli = cm.CreateProxyClient()) { t1 = DateTime.Now; found = cli.WsInterface.SearchData(req); //cli.SearchData(req); t2 = DateTime.Now; } foreach (IXQuery q in found.Results) { Console.WriteLine("Result matching{2}: {0} \"{1}\"", q.Records.First(f => f.Name == "id").String, q.Records.First(f => f.Name == "titolo").String, (usescoring) ? String.Format(" at {0:n2}%", q.Score) : String.Empty); } Console.WriteLine("search required {0}ms and produced {1} results", Convert.ToInt32((t2 - t1).TotalMilliseconds), found.Count); cm.Dispose(); } }
/// <summary> /// core search method /// </summary> /// <param name="request"></param> /// <returns></returns> protected IXQueryCollection DoSearch(IXRequest request) { // validation if (String.IsNullOrEmpty(request.Query.Replace("*", "").Replace("?", ""))) { return new IXQueryCollection() { Count = 0, Start = 0, Take = 0, Results = new IXQuery[] { }, Status = IXQueryStatus.ErrorQuerySyntax } } ; QueryParser parser = null; if (request.Fields.Count() < 1) { parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, null, analyzer); } else if (request.Fields.Count() == 1) { parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, request.Fields.ElementAt(0), analyzer); } else { parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, request.Fields.ToArray(), analyzer); } Query query = ParseQuery(request.Query, parser); if (query == null) { return new IXQueryCollection() { Count = 0, Start = 0, Take = 0, Results = new IXQuery[] { }, Status = IXQueryStatus.ErrorQuerySyntax } } ;; bool useScoring = ((request.Flags & IXRequestFlags.UseScore) == IXRequestFlags.UseScore); if (useScoring) { indexSearcher.SetDefaultFieldSortScoring(true, true); } else { indexSearcher.SetDefaultFieldSortScoring(false, false); } TopFieldDocs hits = indexSearcher.Search(query, null, this.MaxSearchHits, Sort.RELEVANCE); int skip = request.Skip; int take = request.Take; if (skip < 0) { skip = 0; } if (take < 1) { take = this.MaxSearchHits; } return(new IXQueryCollection { Results = MapLuceneIndexToDataList(hits.ScoreDocs.Skip(skip).Take(take), indexSearcher, useScoring, request.Fields), Start = skip, Take = take, Count = hits.TotalHits, Status = (hits.TotalHits > 0) ? IXQueryStatus.Success : IXQueryStatus.NoData }); }
public IXQueryCollection SearchData(IXRequest request) { return(indexer.Search(request)); }