forked from SolrNet/SolrNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegrationFixture.cs
587 lines (542 loc) · 24.1 KB
/
IntegrationFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#region license
// Copyright (c) 2007-2010 Mauricio Scheffer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using MbUnit.Framework;
using Microsoft.Practices.ServiceLocation;
using SolrNet.Commands.Parameters;
using SolrNet.Impl;
using SolrNet.Tests.Integration.Sample;
using SolrNet.Tests.Utils;
namespace SolrNet.Tests.Integration {
[TestFixture]
[Category("Integration")]
public class IntegrationFixture
{
private static readonly string serverURL = ConfigurationManager.AppSettings["solr"];
private static readonly Lazy<object> init = new Lazy<object>(() => {
Startup.Init<Product>(new LoggingConnection(new SolrConnection(serverURL)));
return null;
});
private static readonly Lazy<object> initDict = new Lazy<object>(() => {
Startup.Init<Dictionary<string, object>>(new LoggingConnection(new SolrConnection(serverURL)));
return null;
});
[SetUp]
public void Setup() {
var x = init.Value;
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
solr.Delete(SolrQuery.All);
solr.Commit();
}
[Test]
public void Add_then_query() {
const string name = "Samsuñg SpinPoint P120 SP2514N - hárd drívè - 250 GB - ÁTÀ-133";
var guid = new Guid("{78D734ED-12F8-44E0-8AA3-8CA3F353998D}");
var p = new Product {
Id = "SP2514N",
Guid = guid,
Name = name,
// testing UTF
Manufacturer = "Samsung Electronics Co. Ltd.",
Categories = new[] {
"electronics",
"hard drive",
},
Features = new[] {
"7200RPM, 8MB cache, IDE Ultra ATA-133",
"NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor",
"áéíóúñç & two", // testing UTF
@"ÚóÁ⌠╒""ĥÛē…<>ܐóジャストシステムは、日本で初めてユニコードベースのワードプロセ ッサーを開発しました。このことにより、10年以上も前から、日本のコンピューターユーザーはユニコード、特に日中韓の統合漢 字の恩恵を享受してきました。ジャストシステムは現在、”xfy”というJava環境で稼働する 先進的なXML関連製品の世界市場への展開を積極的に推進していますが、ユニコードを基盤としているために、”xfy”は初めから国際化されているのです。ジャストシステムは、ユニコードの普遍的な思想とアーキテクチャに 感謝するとともに、その第5版の刊行を心から歓迎します",
@"control" + (char)0x07 + (char)0x01 + (char)0x0E +(char)0x1F + (char)0xFFFE, // testing control chars
},
Prices = new Dictionary<string, decimal> {
{"regular", 150m},
{"afterrebate", 100m},
},
Price = 92,
PriceMoney = new Money(92m, "USD"),
Popularity = 6,
InStock = true,
DynCategories = new Dictionary<string, ICollection<string>> {
{"t", new[] {"something"}},
}
};
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
solr.Delete(SolrQuery.All);
solr.AddWithBoost(p, 2.2);
solr.Commit();
solr.Query(new SolrQueryByField("name", @"3;Furniture"));
var products = solr.Query(new SolrQueryByRange<decimal>("price", 10m, 100m).Boost(2));
Assert.AreEqual(1, products.Count);
Assert.AreEqual(name, products[0].Name);
Assert.AreEqual("SP2514N", products[0].Id);
Assert.AreEqual(guid, products[0].Guid);
Assert.AreEqual(92m, products[0].Price);
Assert.IsNotNull(products[0].Prices);
Assert.AreEqual(2, products[0].Prices.Count);
Assert.AreEqual(150m, products[0].Prices["regular"]);
Assert.AreEqual(100m, products[0].Prices["afterrebate"]);
Assert.IsNotNull(products.Header);
Console.WriteLine("QTime is {0}", products.Header.QTime);
}
private static readonly IEnumerable<Product> products = new[] {
new Product {
Id = "DEL12345",
Name = "Delete test product 1",
Manufacturer = "Acme ltd",
Categories = new[] {
"electronics",
"test products",
},
Features = new[] {
"feature 1",
"feature 2",
},
Prices = new Dictionary<string, decimal> {
{"regular", 150m},
{"afterrebate", 100m},
},
Price = 92,
PriceMoney = new Money(123.44m, "EUR"),
Popularity = 6,
InStock = false,
},
new Product {
Id = "DEL12346",
Name = "Delete test product 2",
Manufacturer = "Acme ltd",
Categories = new[] {
"electronics",
"test products",
},
Features = new[] {
"feature 1",
"feature 3",
},
Prices = new Dictionary<string, decimal> {
{"regular", 150m},
{"afterrebate", 100m},
},
Price = 92,
PriceMoney = new Money(123.44m, "ARS"),
Popularity = 6,
InStock = false,
},
new Product {
Id = "DEL12347",
Name = "Delete test product 3",
Manufacturer = "Acme ltd",
Categories = new[] {
"electronics",
"test products",
},
Features = new[] {
"feature 1",
"feature 3",
},
Prices = new Dictionary<string, decimal> {
{"regular", 150m},
{"afterrebate", 100m},
},
Price = 92,
PriceMoney = new Money(123.44m, "GBP"),
Popularity = 6,
InStock = false,
}
};
[Test]
public void QueryByRangeMoney() {
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
solr.AddRange(products);
solr.Commit();
var results = solr.Query(new SolrQueryByRange<Money>("price_c", new Money(123, null), new Money(3000, "USD")));
Assert.AreEqual(2, results.Count);
}
[Test]
public void DeleteByIdAndOrQuery() {
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
solr.AddRange(products);
solr.Commit();
solr.Delete(new[] { "DEL12345", "DEL12346" }, new SolrQueryByField("features", "feature 3"));
solr.Commit();
var productsAfterDelete = solr.Query(SolrQuery.All);
Assert.AreEqual(0, productsAfterDelete.Count);
}
[Test]
public void Highlighting() {
Add_then_query();
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var results = solr.Query(new SolrQueryByField("features", "fluid"), new QueryOptions {
Highlight = new HighlightingParameters {
Fields = new[] {"features"},
}
});
Assert.IsNotNull(results.Highlights);
Assert.AreEqual(1, results.Highlights.Count);
foreach (var h in results.Highlights[results[0].Id])
{
Console.WriteLine("{0}: {1}", h.Key, string.Join(", ", h.Value.ToArray()));
}
}
[Test]
public void HighlightingWrappedWithClass()
{
Add_then_query();
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var results = solr.Query(new SolrQueryByField("features", "fluid"), new QueryOptions
{
Highlight = new HighlightingParameters
{
Fields = new[] { "features" },
}
});
Assert.IsNotNull(results.Highlights);
Assert.AreEqual(1, results.Highlights.Count);
foreach (var h in results.Highlights[results[0].Id].Snippets)
{
Console.WriteLine("{0}: {1}", h.Key, string.Join(", ", h.Value.ToArray()));
}
}
[Test]
public void DateFacet() {
Add_then_query();
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var results = solr.Query(SolrQuery.All, new QueryOptions {
Rows = 0,
Facet = new FacetParameters {
Queries = new[] {
new SolrFacetDateQuery("timestamp", DateTime.Now.AddHours(-1), DateTime.Now.AddHours(1), "+1DAY") {
HardEnd = true,
Other = new[] {FacetDateOther.After, FacetDateOther.Before}
},
}
}
});
var dateFacetResult = results.FacetDates["timestamp"];
Console.WriteLine(dateFacetResult.DateResults[0].Key);
Console.WriteLine(dateFacetResult.DateResults[0].Value);
}
[Test]
public void Ping() {
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
solr.Ping();
}
[Test]
public void Dismax() {
Add_then_query();
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var products = solr.Query(new SolrQuery("samsung"), new QueryOptions { ExtraParams = new Dictionary<string, string> {
{"qt", "dismax"},
{"qf", "sku name^1.2 manu^1.1"},
}});
Assert.GreaterThan(products.Count, 0);
}
[Test]
public void FilterQuery() {
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var r = solr.Query(SolrQuery.All, new QueryOptions {
FilterQueries = new[] {new SolrQueryByRange<string>("price", "4", "*"),}
});
foreach (var product in r) {
Console.WriteLine(product.Id);
}
}
[Test]
public void SpellChecking() {
Add_then_query();
AddSampleDocs();
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var r = solr.Query(new SolrQueryByField("name", "hell untrasharp"), new QueryOptions {
SpellCheck = new SpellCheckingParameters(),
});
Console.WriteLine("Products:");
foreach (var product in r) {
Console.WriteLine(product.Id);
}
Console.WriteLine();
Console.WriteLine("Spell checking:");
Assert.GreaterThan(r.SpellChecking.Count, 0);
foreach (var sc in r.SpellChecking) {
Console.WriteLine(sc.Query);
foreach (var s in sc.Suggestions) {
Console.WriteLine(s);
}
}
}
[Test]
public void RandomSorting() {
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var results = solr.Query(SolrQuery.All, new QueryOptions {
OrderBy = new[] {new RandomSortOrder("random")}
});
foreach (var r in results)
Console.WriteLine(r.Manufacturer);
}
[Test]
public void MoreLikeThis() {
Add_then_query();
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
solr.Add(new Product {
Id = "apache-cocoon",
Categories = new[] {"framework", "java"},
Name = "Apache Cocoon",
Manufacturer = "Apache",
});
solr.Add(new Product {
Id = "apache-hadoop",
Categories = new[] { "framework", "java", "mapreduce" },
Name = "Apache Hadoop",
Manufacturer = "Apache",
});
solr.Commit();
var results = solr.Query(new SolrQuery("apache"), new QueryOptions {
MoreLikeThis = new MoreLikeThisParameters(new[] {"cat", "manu"}) {
MinDocFreq = 1,
MinTermFreq = 1,
//Count = 1,
},
});
Assert.GreaterThan(results.SimilarResults.Count, 0);
foreach (var r in results.SimilarResults) {
Console.WriteLine("Similar documents to {0}", r.Key);
foreach (var similar in r.Value)
Console.WriteLine(similar.Id);
Console.WriteLine();
}
}
[Test]
public void Stats() {
Add_then_query();
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var results = solr.Query(SolrQuery.All, new QueryOptions {
Rows = 0,
Stats = new StatsParameters {
Facets = new[] { "inStock" },
// stats facet currently broken in Solr: https://issues.apache.org/jira/browse/SOLR-2976
//FieldsWithFacets = new Dictionary<string, ICollection<string>> {
// {"popularity", new List<string> {"weight"}}
//}
}
});
Assert.IsNotNull(results.Stats);
foreach (var kv in results.Stats) {
Console.WriteLine("Field {0}: ", kv.Key);
DumpStats(kv.Value, 1);
}
}
public void print(int tabs, string format, params object[] args) {
Console.Write(new string('\t', tabs));
Console.WriteLine(format, args);
}
public void DumpStats(StatsResult s, int tabs) {
print(tabs, "Min: {0}", s.Min);
print(tabs, "Max: {0}", s.Max);
print(tabs, "Sum of squares: {0}", s.SumOfSquares);
foreach (var f in s.FacetResults) {
print(tabs, "Facet: {0}", f.Key);
foreach (var fv in f.Value) {
print(tabs+1, "Facet value: {0}", fv.Key);
DumpStats(fv.Value, tabs+2);
}
}
}
[Test]
public void LocalParams() {
Add_then_query();
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
var results = solr.Query(new LocalParams {{"q.op", "AND"}} + "solr ipod");
Assert.AreEqual(0, results.Count);
}
[Test]
public void LocalParams2() {
Add_then_query();
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
solr.Query(new LocalParams { { "tag", "pp" } } + new SolrQueryByField("cat", "bla"));
}
[Test]
public void LocalParams3() {
Add_then_query();
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
solr.Query(new LocalParams { { "tag", "pp" } } + new SolrQuery("cat:bla"));
}
[Test]
public void LooseMapping() {
Add_then_query();
var _ = initDict.Value;
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Dictionary<string, object>>>();
var results = solr.Query(SolrQuery.All);
Assert.IsInstanceOfType<ArrayList>(results[0]["cat"]);
Assert.IsInstanceOfType<string>(results[0]["id"]);
Assert.IsInstanceOfType<bool>(results[0]["inStock"]);
Assert.IsInstanceOfType<int>(results[0]["popularity"]);
Assert.IsInstanceOfType<float>(results[0]["price"]);
Assert.IsInstanceOfType<DateTime>(results[0]["timestamp"]);
Assert.IsInstanceOfType<string>(((IList) results[0]["cat"])[0]);
foreach (var r in results)
foreach (var kv in r) {
Console.WriteLine("{0} ({1}): {2}", kv.Key, TypeOrNull(kv.Value), kv.Value);
if (kv.Value is IList) {
foreach (var e in (IList)kv.Value)
Console.WriteLine("\t{0} ({1})", e, TypeOrNull(e));
}
}
}
[Test]
[Ignore("Registering the connection in the container causes a side effect.")]
public void LooseMappingAdd() {
var _ = initDict.Value;
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Dictionary<string, object>>>();
solr.Add(new Dictionary<string, object> {
{"id", "id1234"},
{"manu", "pepe"},
{"popularity", 6},
});
}
public Type TypeOrNull(object o) {
if (o == null)
return null;
return o.GetType();
}
[Test]
public void FieldCollapsing() {
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var results = solr.Query(SolrQuery.All, new QueryOptions {
Collapse = new CollapseParameters("manu_exact") {
Type = CollapseType.Adjacent,
MaxDocs = 1,
}
});
Console.WriteLine("CollapsedDocuments.Count {0}", results.Collapsing.CollapsedDocuments.Count);
}
[Test]
public void FieldGrouping()
{
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var results = solr.Query(SolrQuery.All, new QueryOptions
{
Grouping = new GroupingParameters()
{
Fields = new [] { "manu_exact" },
Format = GroupingFormat.Grouped,
Limit = 1,
}
});
Console.WriteLine("Group.Count {0}", results.Grouping.Count);
Assert.AreEqual(1, results.Grouping.Count);
Assert.AreEqual(true, results.Grouping.ContainsKey("manu_exact"));
Assert.GreaterThanOrEqualTo(results.Grouping["manu_exact"].Groups.Count,1);
}
[Test]
public void QueryGrouping()
{
var solr = ServiceLocator.Current.GetInstance<ISolrBasicOperations<Product>>();
var results = solr.Query(SolrQuery.All, new QueryOptions
{
Grouping = new GroupingParameters()
{
Query = new[] { new SolrQuery("manu_exact"), new SolrQuery("name") },
Format = GroupingFormat.Grouped,
Limit = 1,
}
});
Console.WriteLine("Group.Count {0}", results.Grouping.Count);
Assert.AreEqual(2, results.Grouping.Count);
Assert.AreEqual(true, results.Grouping.ContainsKey("manu_exact"));
Assert.AreEqual(true, results.Grouping.ContainsKey("name"));
Assert.GreaterThanOrEqualTo(results.Grouping["manu_exact"].Groups.Count, 1);
Assert.GreaterThanOrEqualTo(results.Grouping["name"].Groups.Count, 1);
}
private static readonly Lazy<object> initLoose = new Lazy<object>(() => {
Startup.Init<ProductLoose>(new LoggingConnection(new SolrConnection(serverURL)));
return null;
});
[Test]
public void SemiLooseMapping() {
Add_then_query();
var _ = initLoose.Value;
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<ProductLoose>>();
var products = solr.Query(SolrQuery.All, new QueryOptions {Fields = new[] {"*", "score"}});
Assert.AreEqual(1, products.Count);
var product = products[0];
Assert.AreEqual("SP2514N", product.Id);
Assert.IsTrue(product.Score.HasValue);
Assert.IsFalse(product.OtherFields.ContainsKey("score"));
Assert.IsNull(product.SKU);
Assert.IsNotNull(product.Name);
Assert.IsNotNull(product.OtherFields);
Console.WriteLine(product.OtherFields.Count);
foreach (var field in product.OtherFields)
Console.WriteLine("{0}: {1} ({2})", field.Key, field.Value, TypeOrNull(field.Value));
Assert.IsInstanceOfType(typeof(DateTime), product.OtherFields["timestamp"]);
Assert.AreEqual(new DateTime(1,1,1), product.OtherFields["timestamp"]);
Assert.IsInstanceOfType(typeof(ICollection), product.OtherFields["features"]);
product.OtherFields["timestamp"] = new DateTime(2010, 1, 1);
product.OtherFields["features"] = new[] {"a", "b", "c"};
product.OtherFields.Remove("_version_"); // avoid optimistic locking for now https://issues.apache.org/jira/browse/SOLR-3178
product.Score = null;
solr.Add(product);
}
[Test]
public void ExtractRequestHandler() {
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
using (var file = File.OpenRead(@"..\..\test.pdf")) {
var response = solr.Extract(new ExtractParameters(file, "abcd") {
ExtractOnly = true,
ExtractFormat = ExtractFormat.Text,
});
Console.WriteLine(response.Content);
Assert.AreEqual("Your PDF viewing software works!\n\n\n", response.Content);
}
}
public void AddSampleDocs() {
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
var connection = ServiceLocator.Current.GetInstance<ISolrConnection>();
var files = Directory.GetFiles(@"..\..\..\SampleSolrApp\exampledocs", "*.xml");
foreach (var file in files) {
connection.Post("/update", File.ReadAllText(file, Encoding.UTF8));
}
solr.Commit();
}
[Test]
public void MoreLikeThisHandler() {
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
solr.Delete(SolrQuery.All);
solr.Commit();
AddSampleDocs();
var mltParams = new MoreLikeThisHandlerParameters(new[] {"cat", "name"}) {
MatchInclude = true,
MinTermFreq = 1,
MinDocFreq = 1,
ShowTerms = InterestingTerms.List,
};
var q = SolrMLTQuery.FromQuery(new SolrQuery("id:UTF8TEST"));
var results = solr.MoreLikeThis(q, new MoreLikeThisHandlerQueryOptions(mltParams));
Assert.AreEqual(2, results.Count);
Assert.IsNotNull(results.Match);
Assert.AreEqual("UTF8TEST", results.Match.Id);
Assert.GreaterThan(results.InterestingTerms.Count, 0);
foreach (var t in results.InterestingTerms) {
Console.WriteLine("Interesting term: {0} ({1})", t.Key, t.Value);
}
}
}
}