public void Number_Field() { using (var luceneDir = new RandomIdRAMDirectory()) using (var indexer = new TestIndex( new FieldDefinitionCollection(new FieldDefinition("item2", "number")), luceneDir, new StandardAnalyzer(Version.LUCENE_30))) { indexer.IndexItem(new ValueSet(1.ToString(), "content", new Dictionary <string, IEnumerable <object> > { { "item1", new List <object>(new[] { "value1" }) }, { "item2", new List <object>(new object[] { 123456 }) } })); using (var s = (LuceneSearcher)indexer.GetSearcher()) { var luceneSearcher = s.GetLuceneSearcher(); var fields = luceneSearcher.Doc(luceneSearcher.MaxDoc - 1).GetFields().ToArray(); var valType = indexer.FieldValueTypeCollection.GetValueType("item2"); Assert.AreEqual(typeof(Int32Type), valType.GetType()); Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == "item2")); } } }
private void DoSpatialSearch( SpatialContext ctx, SpatialStrategy strategy, TestIndex indexer, double searchRadius, string idToMatch, Func <SpatialArgs, Query> createQuery, int lat, int lng) { var searcher = (LuceneSearcher)indexer.GetSearcher(); var luceneSearcher = searcher.GetLuceneSearcher(); GetXYFromCoords(lat, lng, out var x, out var y); // Make a circle around the search point var args = new SpatialArgs( SpatialOperation.Intersects, ctx.MakeCircle(x, y, DistanceUtils.Dist2Degrees(searchRadius, DistanceUtils.EARTH_MEAN_RADIUS_KM))); var filter = strategy.MakeFilter(args); var query = createQuery(args); // TODO: It doesn't make a whole lot of sense to sort by score when searching on only geo-coords, // typically you would sort by closest distance // Which can be done, see https://github.com/apache/lucene-solr/blob/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/SpatialExample.java#L169 TopDocs docs = luceneSearcher.Search(query, filter, MaxResultDocs, new Sort(new SortField(null, SortField.SCORE))); AssertDocMatchedIds(luceneSearcher, docs, idToMatch); // TODO: We should make this possible and allow passing in a Lucene Filter // to the LuceneSearchQuery along with the Lucene Query, then we // don't need to manually perform the Lucene Search //var criteria = (LuceneSearchQuery)searcher.CreateQuery(); //criteria.LuceneQuery(q); //var results = criteria.Execute(); }
private TestIndex CreateTestIndex(Directory luceneDirectory, string[] fieldNames) { var indexer = new TestIndex("TestIndex", luceneDirectory, fieldNames); using (indexer.ProcessNonAsync()) { //populate with some test data indexer.IndexItem(new ValueSet("1", "content", new Dictionary <string, object> { [fieldNames[0]] = "Hello world, there are products here", [UmbracoContentIndex.VariesByCultureFieldName] = "n" })); indexer.IndexItem(new ValueSet("2", "content", new Dictionary <string, object> { [fieldNames[1]] = "Hello world, there are products here", [UmbracoContentIndex.VariesByCultureFieldName] = "y" })); indexer.IndexItem(new ValueSet("3", "content", new Dictionary <string, object> { [fieldNames[2]] = "Hello world, there are products here", [UmbracoContentIndex.VariesByCultureFieldName] = "y" })); } return(indexer); }
private void Build(TestIndex index) { try { /* build an index */ IndexWriter writer = new IndexWriter(index.index, new SimpleAnalyzer(), T, IndexWriter.MaxFieldLength.LIMITED, null); for (int d = minId; d <= maxId; d++) { Document doc = new Document(); doc.Add(new Field("id", Pad(d), Field.Store.YES, Field.Index.NOT_ANALYZED)); int r = index.allowNegativeRandomInts ? rand.Next() : rand.Next(System.Int32.MaxValue); if (index.maxR < r) { index.maxR = r; } if (r < index.minR) { index.minR = r; } doc.Add(new Field("rand", Pad(r), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("body", "body", Field.Store.YES, Field.Index.NOT_ANALYZED)); writer.AddDocument(doc, null); } writer.Optimize(null); writer.Close(); } catch (System.Exception e) { throw new System.SystemException("can't build index", e); } }
private void RunTest(SpatialContext ctx, SpatialStrategy strategy, Func <SpatialArgs, Query> createQuery) { var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); using (var luceneDir = new RandomIdRAMDirectory()) { string id1 = 1.ToString(); string id2 = 2.ToString(); string id3 = 3.ToString(); string id4 = 4.ToString(); using (var indexer = new TestIndex(luceneDir, analyzer)) { indexer.DocumentWriting += (sender, args) => Indexer_DocumentWriting(args, ctx, strategy); indexer.IndexItems(new[] { ValueSet.FromObject(id1, "content", new { nodeName = "location 1", bodyText = "Zanzibar is in Africa", lat = -6.1357, lng = 39.3621 }), ValueSet.FromObject(id2, "content", new { nodeName = "location 2", bodyText = "In Canada there is a town called Sydney in Nova Scotia", lat = 46.1368, lng = -60.1942 }), ValueSet.FromObject(id3, "content", new { nodeName = "location 3", bodyText = "Sydney is the capital of NSW in Australia", lat = -33.8688, lng = 151.2093 }), ValueSet.FromObject(id4, "content", new { nodeName = "location 4", bodyText = "Somewhere unknown", lat = 50, lng = 50 }) }); DoSpatialSearch(ctx, strategy, indexer, SearchRadius, id3, createQuery, lat: -33, lng: 151); DoSpatialSearch(ctx, strategy, indexer, SearchRadius, id2, createQuery, lat: 46, lng: -60); DoSpatialSearch(ctx, strategy, indexer, SearchRadius, id1, createQuery, lat: -6, lng: 39); DoSpatialSearch(ctx, strategy, indexer, SearchRadius, id4, createQuery, lat: 50, lng: 50); } } }
public void MultiIndex_Simple_Search() { var analyzer = new StandardAnalyzer(Version.LUCENE_30); using (var luceneDir1 = new RandomIdRAMDirectory()) using (var luceneDir2 = new RandomIdRAMDirectory()) using (var luceneDir3 = new RandomIdRAMDirectory()) using (var luceneDir4 = new RandomIdRAMDirectory()) using (var indexer1 = new TestIndex(luceneDir1, analyzer)) using (var indexer2 = new TestIndex(luceneDir2, analyzer)) using (var indexer3 = new TestIndex(luceneDir3, analyzer)) using (var indexer4 = new TestIndex(luceneDir4, analyzer)) { indexer1.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "value1", item2 = "The agitated zebras gallop back and forth in short, panicky dashes, then skitter off into the absolute darkness." })); indexer2.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "value2", item2 = "The festival lasts five days and celebrates the victory of good over evil, light over darkness, and knowledge over ignorance." })); indexer3.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "value3", item2 = "They are expected to confront the darkness and show evidence that they have done so in their papers" })); indexer4.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "value4", item2 = "Scientists believe the lake could be home to cold-loving microbial life adapted to living in total darkness." })); indexer3.IndexItem(ValueSet.FromObject(2.ToString(), "content", new { item1 = "value3", item2 = "Scotch scotch scotch, i love scotch" })); indexer4.IndexItem(ValueSet.FromObject(2.ToString(), "content", new { item1 = "value4", item2 = "60% of the time, it works everytime" })); using (var searcher = new MultiIndexSearcher("testSearcher", new[] { indexer1, indexer2, indexer3, indexer4 }, analyzer)) { var result = searcher.Search("darkness"); Assert.AreEqual(4, result.TotalItemCount); foreach (var r in result) { Console.WriteLine("Score = " + r.Score); } } } }
public void SideBySideExecuteShouldNotCreateReplacementIndexIfIndexToReplaceIsIdentical() { using (var store = NewDocumentStore(runInMemory: false)) { var indexName = new TestIndex().IndexName; new TestIndex().SideBySideExecute(store); using (var session = store.OpenSession()) { session.Store(new Person { FirstName = "John", LastName = "Doe" }); session.SaveChanges(); } WaitForIndexing(store); store.DocumentDatabase.StopBackgroundWorkers(); store .DatabaseCommands .Admin .StopIndexing(); new TestIndex().SideBySideExecute(store); Assert.Null(store.DatabaseCommands.GetIndex("ReplacementOf/" + indexName)); } }
public void TestIndexCreationAndInsert() { var config = IConfigServiceMock.Build(new Dictionary <string, string> { { "ELASTICSEARCH_URL", "http://localhost:9200" }, }); var elasticService = new ElasticService(config); // Only run tests when a elastic endpoint is available if (!elasticService.Ping()) { return; } var indexStatus = elasticService.EnsureIndex <TestIndex>(); Assert.True(indexStatus); var subject = new TestIndex { Id = 123, Value = "potato" }; var insertStatus = elasticService.Index(subject); Assert.True(insertStatus); }
private TestIndex CreateTestIndex(Directory luceneDirectory, string[] fieldNames) { var index = new TestIndex(LoggerFactory, "TestIndex", luceneDirectory, fieldNames); using (index.WithThreadingMode(IndexThreadingMode.Synchronous)) { //populate with some test data index.IndexItem(new ValueSet("1", "content", new Dictionary <string, object> { [fieldNames[0]] = "Hello world, there are products here", [UmbracoExamineFieldNames.VariesByCultureFieldName] = "n" })); index.IndexItem(new ValueSet("2", "content", new Dictionary <string, object> { [fieldNames[1]] = "Hello world, there are products here", [UmbracoExamineFieldNames.VariesByCultureFieldName] = "y" })); index.IndexItem(new ValueSet("3", "content", new Dictionary <string, object> { [fieldNames[2]] = "Hello world, there are products here", [UmbracoExamineFieldNames.VariesByCultureFieldName] = "y" })); } return(index); }
public void Can_Add_Doc_With_Fields() { using (var luceneDir = new RandomIdRAMDirectory()) using (var indexer = new TestIndex(luceneDir, new StandardAnalyzer(Version.LUCENE_30))) { indexer.IndexItem(new ValueSet(1.ToString(), "content", "test", new Dictionary <string, IEnumerable <object> > { { "item1", new List <object>(new[] { "value1" }) }, { "item2", new List <object>(new[] { "value2" }) } })); using (var s = (LuceneSearcher)indexer.GetSearcher()) { var luceneSearcher = s.GetLuceneSearcher(); var fields = luceneSearcher.Doc(0).GetFields().ToArray(); Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == "item1")); Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == "item2")); Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == LuceneIndex.ItemTypeFieldName)); Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == LuceneIndex.ItemIdFieldName)); Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == LuceneIndex.CategoryFieldName)); Assert.AreEqual("value1", fields.Single(x => x.Name == "item1").StringValue); Assert.AreEqual("value2", fields.Single(x => x.Name == "item2").StringValue); Assert.AreEqual("test", fields.Single(x => x.Name == LuceneIndex.ItemTypeFieldName).StringValue); Assert.AreEqual("1", fields.Single(x => x.Name == LuceneIndex.ItemIdFieldName).StringValue); Assert.AreEqual("content", fields.Single(x => x.Name == LuceneIndex.CategoryFieldName).StringValue); } } }
public void Custom_Extension_Method_Is_Translated_As_Method_Call() { var index = new TestIndex(); var map = index.CreateIndexDefinition().Map; Debug.WriteLine(map); Assert.Contains("Baz = CustomExtensionMethods.DoSomething(foo.Bar)", map); }
public static void BeforeClassBaseTestRangeFilter() { MaxId = AtLeast(500); SignedIndexDir = new TestIndex(Random(), int.MaxValue, int.MinValue, true); UnsignedIndexDir = new TestIndex(Random(), int.MaxValue, 0, false); SignedIndexReader = Build(Random(), SignedIndexDir); UnsignedIndexReader = Build(Random(), UnsignedIndexDir); }
public void Index_Exists() { using (var luceneDir = new RandomIdRAMDirectory()) using (var indexer = new TestIndex(luceneDir, new StandardAnalyzer(Version.LUCENE_30))) { indexer.EnsureIndex(true); Assert.IsTrue(indexer.IndexExists()); } }
public override void BeforeClass() // LUCENENET specific: renamed from BeforeClassBaseTestRangeFilter() so we can override to control the order of execution { base.BeforeClass(); MaxId = AtLeast(500); SignedIndexDir = new TestIndex(Random, int.MaxValue, int.MinValue, true); UnsignedIndexDir = new TestIndex(Random, int.MaxValue, 0, false); SignedIndexReader = Build(Random, SignedIndexDir); UnsignedIndexReader = Build(Random, UnsignedIndexDir); }
public virtual RedirectToRouteResult Index(TestIndex model) { switch (model.ActionSubmit) { case TestIndex._Action.Submit: AddSuccessMessage("Submmit success"); return RedirectToAction(MVC.Home.Index()); default: throw new WebSiteException("Unknown action?!"); } }
public static void AfterClassBaseTestRangeFilter() { SignedIndexReader.Dispose(); UnsignedIndexReader.Dispose(); SignedIndexDir.Index.Dispose(); UnsignedIndexDir.Index.Dispose(); SignedIndexReader = null; UnsignedIndexReader = null; SignedIndexDir = null; UnsignedIndexDir = null; }
public override void AfterClass() // LUCENENET specific: renamed from AfterClassBaseTestRangeFilter() so we can override to control the order of execution { SignedIndexReader?.Dispose(); UnsignedIndexReader?.Dispose(); SignedIndexDir?.Index?.Dispose(); UnsignedIndexDir?.Index?.Dispose(); SignedIndexReader = null; UnsignedIndexReader = null; SignedIndexDir = null; UnsignedIndexDir = null; base.AfterClass(); }
public void Rebuild_Index() { using (var d = new RandomIdRAMDirectory()) using (var indexer = new TestIndex(d, new StandardAnalyzer(Version.LUCENE_30))) { indexer.CreateIndex(); indexer.IndexItems(indexer.AllData()); var indexWriter = indexer.GetIndexWriter(); var reader = indexWriter.GetReader(); Assert.AreEqual(100, reader.NumDocs()); } }
/// <summary> /// 保存数据 /// </summary> public void Save() { lock (lockObject) { Dictionary <string, string> buff = new Dictionary <string, string>(); buff.Add("OilIndex", OilIndex.ToString()); buff.Add("TestIndex", TestIndex.ToString()); buff.Add("AnGuiIndex", AnGuiIndex.ToString()); buff.Add("ErrorIndex", ErrorIndex.ToString()); buff.Add("MaterialIndex", MaterialIndex.ToString()); buff.Add("MaterialIndexReport", MaterialIndexReport.ToString()); buff.Add("OverFirstNiuJu", OverFirstNiuJu.ToString()); buff.Add("OverSecondNiuJu", OverSecondNiuJu.ToString()); All.Class.FileIO.Write(string.Format("{0}\\{1}", XMLDirectory, FileName), All.Class.SSFile.Dictionary2Text(buff), System.IO.FileMode.Create); } }
public void MultiIndex_Field_Count() { var analyzer = new StandardAnalyzer(Version.LUCENE_30); using (var luceneDir1 = new RandomIdRAMDirectory()) using (var luceneDir2 = new RandomIdRAMDirectory()) using (var luceneDir3 = new RandomIdRAMDirectory()) using (var luceneDir4 = new RandomIdRAMDirectory()) using (var indexer1 = new TestIndex(luceneDir1, analyzer) { RunAsync = false }) using (var indexer2 = new TestIndex(luceneDir2, analyzer) { RunAsync = false }) using (var indexer3 = new TestIndex(luceneDir3, analyzer) { RunAsync = false }) using (var indexer4 = new TestIndex(luceneDir4, analyzer) { RunAsync = false }) { indexer1.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "hello", item2 = "The agitated zebras gallop back and forth in short, panicky dashes, then skitter off into the absolute darkness." })); indexer2.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "world", item2 = "The festival lasts five days and celebrates the victory of good over evil, light over darkness, and knowledge over ignorance." })); indexer3.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "here", item2 = "They are expected to confront the darkness and show evidence that they have done so in their papers" })); indexer4.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "are", item2 = "Scientists believe the lake could be home to cold-loving microbial life adapted to living in total darkness." })); indexer3.IndexItem(ValueSet.FromObject(2.ToString(), "content", new { item3 = "some", item2 = "Scotch scotch scotch, i love scotch" })); indexer4.IndexItem(ValueSet.FromObject(2.ToString(), "content", new { item4 = "values", item2 = "60% of the time, it works everytime" })); using (var searcher = new MultiIndexSearcher("testSearcher", new[] { indexer1, indexer2, indexer3, indexer4 }, analyzer)) { var result = searcher.GetAllIndexedFields(); //will be item1 , item2, item3, and item4 Assert.AreEqual(4, result.Count()); foreach (var s in new[] { "item1", "item2", "item3", "item4" }) { Assert.IsTrue(result.Contains(s)); } } } }
public void Can_Add_One_Document() { using (var luceneDir = new RandomIdRAMDirectory()) using (var indexer = new TestIndex(luceneDir, new StandardAnalyzer(Version.LUCENE_30))) { indexer.IndexItem(new ValueSet(1.ToString(), "content", new Dictionary <string, IEnumerable <object> > { { "item1", new List <object>(new[] { "value1" }) }, { "item2", new List <object>(new[] { "value2" }) } })); var indexWriter = indexer.GetIndexWriter(); var reader = indexWriter.GetReader(); Assert.AreEqual(1, reader.NumDocs()); } }
public void Can_Add_Doc_With_Easy_Fields() { using (var luceneDir = new RandomIdRAMDirectory()) using (var indexer = new TestIndex(luceneDir, new StandardAnalyzer(Version.LUCENE_30))) { indexer.IndexItem(ValueSet.FromObject(1.ToString(), "content", new { item1 = "value1", item2 = "value2" })); using (var s = (LuceneSearcher)indexer.GetSearcher()) { var luceneSearcher = s.GetLuceneSearcher(); var fields = luceneSearcher.Doc(0).GetFields().ToArray(); Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == "item1")); Assert.IsNotNull(fields.SingleOrDefault(x => x.Name == "item2")); Assert.AreEqual("value1", fields.Single(x => x.Name == "item1").StringValue); Assert.AreEqual("value2", fields.Single(x => x.Name == "item2").StringValue); } } }
public void Index_Tests() { // var index = JsonConvert.DeserializeObject<Index<Scene>>("1"); // Assert.Equal(1, index.Value); // var str = JsonConvert.SerializeObject(index, new IndexConverter()); // Assert.Equal("1", str); var ti = new TestIndex { Scene = new Index <Scene> { Value = 1 } }; var str2 = JsonConvert.SerializeObject(ti, Formatting.None); Assert.Equal("{\"scene\":1}", str2); var ti2 = JsonConvert.DeserializeObject <TestIndex>(str2); Assert.Equal(1, ti2.Scene.Value); }
public ActionResult GetTests() { try { IEnumerable <REF_TEST_TB> tests = _uow.Repository <REF_TEST_TB>().GetAll(); List <TestIndex> testList = new List <TestIndex>(); tests = tests.ToList().OrderBy(u => u.SZ_DESCRIPTION).ThenBy(u => u.SZ_LABEL); foreach (var test in tests) { TestIndex item = new TestIndex() { AMethod = test.SZ_ANALYSIS_METHOD == null ? "" : test.SZ_ANALYSIS_METHOD, EffectiveDate = test.DT_EFFECTIVE == null || test.DT_EFFECTIVE.Value.ToShortDateString() == "1/1/0001" ? "" : test.DT_EFFECTIVE.Value.ToShortDateString(), ExpiredDate = test.DT_EXPIRED == null || test.DT_EXPIRED.Value.ToShortDateString() == "1/1/0001" ? "" : test.DT_EXPIRED.Value.ToShortDateString(), SampleMedia = test.SZ_SAMPLE_MEDIA == null ? "" : test.SZ_SAMPLE_MEDIA, SampleType = test.SZ_SAMPLE_TYPE == null ? "" : test.SZ_SAMPLE_TYPE, TestID = test.N_TEST_SYSID, TestName = test.SZ_DESCRIPTION == null ? "" : test.SZ_DESCRIPTION, Units = test.SZ_TITLE == null ? "" : test.SZ_TITLE }; testList.Add(item); } return(Json(testList, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { if (ex.InnerException == null) { ViewBag.Message = "Function: TestController.GetTests_GET\n\nError: " + ex.Message; } else { ViewBag.Message = "Function: TestController.GetTests_GET\n\nError: " + (ex.Message + "\n\nInnerException: " + ex.InnerException.Message); }; Session["ErrorMessage"] = ViewBag.Message; return(RedirectToAction("InternalServerError", "Error")); }; }
public void Track_Readers() { var analyzer = new StandardAnalyzer(Version.LUCENE_30); using (var luceneDir = new RandomIdRAMDirectory()) using (var indexer = new TestIndex(luceneDir, analyzer)) { indexer.IndexItems(new[] { ValueSet.FromObject(1.ToString(), "content", new { nodeName = "umbraco", headerText = "world", writerName = "administrator" }), ValueSet.FromObject(2.ToString(), "content", new { nodeName = "umbraco", headerText = "umbraco", writerName = "administrator" }), ValueSet.FromObject(3.ToString(), "content", new { nodeName = "umbraco", headerText = "umbraco", writerName = "administrator" }), ValueSet.FromObject(4.ToString(), "content", new { nodeName = "hello", headerText = "world", writerName = "blah" }) }); LuceneSearcher searcher = (LuceneSearcher)indexer.GetSearcher(); IndexSearcher luceneSearcher = (IndexSearcher)searcher.GetLuceneSearcher(); //Arrange var sc = searcher.CreateQuery("content").Field("writerName", "administrator"); //Act var results = sc.Execute(); using (var e1 = results.GetEnumerator()) { Assert.AreEqual(2, luceneSearcher.IndexReader.RefCount); using (var e2 = results.Skip(2).GetEnumerator()) { Assert.AreEqual(3, luceneSearcher.IndexReader.RefCount); } Assert.AreEqual(2, luceneSearcher.IndexReader.RefCount); } Assert.AreEqual(1, luceneSearcher.IndexReader.RefCount); } }
public void Can_Have_Multiple_Values_In_Fields() { using (var luceneDir = new RandomIdRAMDirectory()) using (var indexer = new TestIndex(luceneDir, new StandardAnalyzer(Version.LUCENE_30))) { indexer.IndexItem(new ValueSet(1.ToString(), "content", new Dictionary <string, IEnumerable <object> > { { "item1", new List <object> { "subval1", "subval2" } }, { "item2", new List <object> { "subval1", "subval2", "subval3" } } })); using (var s = (LuceneSearcher)indexer.GetSearcher()) { var luceneSearcher = s.GetLuceneSearcher(); var fields = luceneSearcher.Doc(0).GetFields().ToArray(); Assert.AreEqual(2, fields.Count(x => x.Name == "item1")); Assert.AreEqual(3, fields.Count(x => x.Name == "item2")); Assert.AreEqual("subval1", fields.Where(x => x.Name == "item1").ElementAt(0).StringValue); Assert.AreEqual("subval2", fields.Where(x => x.Name == "item1").ElementAt(1).StringValue); Assert.AreEqual("subval1", fields.Where(x => x.Name == "item2").ElementAt(0).StringValue); Assert.AreEqual("subval2", fields.Where(x => x.Name == "item2").ElementAt(1).StringValue); Assert.AreEqual("subval3", fields.Where(x => x.Name == "item2").ElementAt(2).StringValue); } } }
public void Dont_Initialize_Searchers_On_Dispose_If_Not_Already_Initialized() { var analyzer = new StandardAnalyzer(Version.LUCENE_30); using (var luceneDir1 = new RandomIdRAMDirectory()) using (var luceneDir2 = new RandomIdRAMDirectory()) using (var luceneDir3 = new RandomIdRAMDirectory()) using (var luceneDir4 = new RandomIdRAMDirectory()) using (var indexer1 = new TestIndex(luceneDir1, analyzer)) using (var indexer2 = new TestIndex(luceneDir2, analyzer)) using (var indexer3 = new TestIndex(luceneDir3, analyzer)) using (var indexer4 = new TestIndex(luceneDir4, analyzer)) { var searcher = new MultiIndexSearcher("testSearcher", new[] { indexer1, indexer2, indexer3, indexer4 }, analyzer); Assert.IsFalse(searcher.SearchersInitialized); searcher.Dispose(); Assert.IsFalse(searcher.SearchersInitialized); } }
private void InitBlock() { signedIndex = new TestIndex(this, System.Int32.MaxValue, System.Int32.MinValue, true); unsignedIndex = new TestIndex(this, System.Int32.MaxValue, 0, false); }
private static IndexReader Build(Random random, TestIndex index) { /* build an index */ Document doc = new Document(); Field idField = NewStringField(random, "id", "", Field.Store.YES); Field randField = NewStringField(random, "rand", "", Field.Store.YES); Field bodyField = NewStringField(random, "body", "", Field.Store.NO); doc.Add(idField); doc.Add(randField); doc.Add(bodyField); RandomIndexWriter writer = new RandomIndexWriter(random, index.Index, NewIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetOpenMode(OpenMode.CREATE).SetMaxBufferedDocs(TestUtil.NextInt(random, 50, 1000)).SetMergePolicy(NewLogMergePolicy())); TestUtil.ReduceOpenFiles(writer.w); while (true) { int minCount = 0; int maxCount = 0; for (int d = MinId; d <= MaxId; d++) { idField.StringValue = Pad(d); int r = index.AllowNegativeRandomInts ? random.Next() : random.Next(int.MaxValue); if (index.MaxR < r) { index.MaxR = r; maxCount = 1; } else if (index.MaxR == r) { maxCount++; } if (r < index.MinR) { index.MinR = r; minCount = 1; } else if (r == index.MinR) { minCount++; } randField.StringValue = Pad(r); bodyField.StringValue = "body"; writer.AddDocument(doc); } if (minCount == 1 && maxCount == 1) { // our subclasses rely on only 1 doc having the min or // max, so, we loop until we satisfy that. it should be // exceedingly rare (Yonik calculates 1 in ~429,000) // times) that this loop requires more than one try: IndexReader ir = writer.Reader; writer.Dispose(); return(ir); } // try again writer.DeleteAll(); } }
public void Index_Read_And_Write_Ensure_No_Errors_In_Async() { using (var d = new RandomIdRAMDirectory()) using (var writer = new IndexWriter(d, new CultureInvariantStandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.LIMITED)) using (var customIndexer = new TestIndex(writer)) using (var customSearcher = (LuceneSearcher)customIndexer.GetSearcher()) { var waitHandle = new ManualResetEvent(false); void OperationComplete(object sender, IndexOperationEventArgs e) { //signal that we are done waitHandle.Set(); } //add the handler for optimized since we know it will be optimized last based on the commit count customIndexer.IndexOperationComplete += OperationComplete; //remove the normal indexing error handler customIndexer.IndexingError -= IndexInitializer.IndexingError; //run in async mode customIndexer.RunAsync = false; //get a node from the data repo var node = _contentService.GetPublishedContentByXPath("//*[string-length(@id)>0 and number(@id)>0]") .Root .Elements() .First(); var idQueue = new ConcurrentQueue <int>(Enumerable.Range(1, 10)); var searchThreadCount = 500; var indexThreadCount = 10; var searchCount = 10700; var indexCount = 100; var searchCountPerThread = Convert.ToInt32(searchCount / searchThreadCount); var indexCountPerThread = Convert.ToInt32(indexCount / indexThreadCount); //spawn a bunch of threads to perform some reading var tasks = new List <Task>(); Action <ISearcher> doSearch = (s) => { try { for (var counter = 0; counter < searchCountPerThread; counter++) { //get next id and put it to the back of the list int docId; if (idQueue.TryDequeue(out docId)) { idQueue.Enqueue(docId); var r = s.CreateQuery().Id(docId.ToString()).Execute(); Debug.WriteLine("searching thread: {0}, id: {1}, found: {2}", Thread.CurrentThread.ManagedThreadId, docId, r.Count()); Thread.Sleep(50); } } } catch (Exception ex) { Debug.WriteLine("ERROR!! {0}", ex); throw; } }; Action <IIndex> doIndex = (ind) => { try { //reindex the same node a bunch of times for (var i = 0; i < indexCountPerThread; i++) { //get next id and put it to the back of the list int docId; if (idQueue.TryDequeue(out docId)) { idQueue.Enqueue(docId); var cloned = new XElement(node); cloned.Attribute("id").Value = docId.ToString(CultureInfo.InvariantCulture); Debug.WriteLine("Indexing {0}", docId); ind.IndexItems(new[] { cloned.ConvertToValueSet(IndexTypes.Content) }); Thread.Sleep(100); } } } catch (Exception ex) { Debug.WriteLine("ERROR!! {0}", ex); throw; } }; //indexing threads for (var i = 0; i < indexThreadCount; i++) { var indexer = customIndexer; tasks.Add(Task.Factory.StartNew(() => doIndex(indexer), TaskCreationOptions.LongRunning)); } //searching threads for (var i = 0; i < searchThreadCount; i++) { var searcher = customSearcher; tasks.Add(Task.Factory.StartNew(() => doSearch(searcher), TaskCreationOptions.LongRunning)); } try { Task.WaitAll(tasks.ToArray()); } catch (AggregateException e) { var sb = new StringBuilder(); sb.Append(e.Message + ": "); foreach (var v in e.InnerExceptions) { sb.Append(v.Message + "; "); } Assert.Fail(sb.ToString()); } //reset the async mode and remove event handler customIndexer.IndexingError += IndexInitializer.IndexingError; customIndexer.RunAsync = false; //wait until we are done waitHandle.WaitOne(); writer.WaitForMerges(); var results = customSearcher.CreateQuery().Field("nodeName", (IExamineValue) new ExamineValue(Examineness.Explicit, "Home")).Execute(); Assert.AreEqual(10, results.Count()); } }
private void Build(TestIndex index) { try { /* build an index */ IndexWriter writer = new IndexWriter(index.index, new SimpleAnalyzer(), T, IndexWriter.MaxFieldLength.LIMITED); for (int d = minId; d <= maxId; d++) { Document doc = new Document(); doc.Add(new Field("id", Pad(d), Field.Store.YES, Field.Index.NOT_ANALYZED)); int r = index.allowNegativeRandomInts ? rand.Next() : rand.Next(System.Int32.MaxValue); if (index.maxR < r) { index.maxR = r; } if (r < index.minR) { index.minR = r; } doc.Add(new Field("rand", Pad(r), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("body", "body", Field.Store.YES, Field.Index.NOT_ANALYZED)); writer.AddDocument(doc); } writer.Optimize(); writer.Close(); } catch (System.Exception e) { throw new System.SystemException("can't build index", e); } }
void AssertParserQuery(string query, params int[] result) { Console.WriteLine("Query: " + query); TestIndex.AssertQuery(parser.Parse(query), result); }
/// <summary> /// LUCENENET specific /// Passed in because NewStringField and NewIndexWriterConfig are no /// longer static. /// </summary> private IndexReader Build(Random random, TestIndex index) { /* build an index */ Document doc = new Document(); Field idField = NewStringField(random, "id", "", Field.Store.YES); Field randField = NewStringField(random, "rand", "", Field.Store.YES); Field bodyField = NewStringField(random, "body", "", Field.Store.NO); doc.Add(idField); doc.Add(randField); doc.Add(bodyField); RandomIndexWriter writer = new RandomIndexWriter(random, index.Index, NewIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetOpenMode(OpenMode.CREATE).SetMaxBufferedDocs(TestUtil.NextInt(random, 50, 1000)).SetMergePolicy(NewLogMergePolicy())); TestUtil.ReduceOpenFiles(writer.w); while (true) { int minCount = 0; int maxCount = 0; for (int d = MinId; d <= MaxId; d++) { idField.StringValue = Pad(d); int r = index.AllowNegativeRandomInts ? random.Next() : random.Next(int.MaxValue); if (index.MaxR < r) { index.MaxR = r; maxCount = 1; } else if (index.MaxR == r) { maxCount++; } if (r < index.MinR) { index.MinR = r; minCount = 1; } else if (r == index.MinR) { minCount++; } randField.StringValue = Pad(r); bodyField.StringValue = "body"; writer.AddDocument(doc); } if (minCount == 1 && maxCount == 1) { // our subclasses rely on only 1 doc having the min or // max, so, we loop until we satisfy that. it should be // exceedingly rare (Yonik calculates 1 in ~429,000) // times) that this loop requires more than one try: IndexReader ir = writer.Reader; writer.Dispose(); return ir; } // try again writer.DeleteAll(); } }