public void AutomaticallyClosesDanglingQuotes() { // arrange var queryText = "title:\"dot NET version:1.2.3"; var phraseQuery = new PhraseQuery(); phraseQuery.Add(new Term("Title", "dot")); phraseQuery.Add(new Term("Title", "net")); phraseQuery.Add(new Term("Title", "version")); phraseQuery.Add(new Term("Title", "1")); phraseQuery.Add(new Term("Title", "2")); phraseQuery.Add(new Term("Title", "3")); var expected = new BooleanQuery { new BooleanClause(new BooleanQuery { new BooleanClause(new BooleanQuery { new BooleanClause(phraseQuery, Occur.SHOULD) }, Occur.SHOULD) }, Occur.MUST) }; // act var actual = NuGetQuery.MakeQuery(queryText); // assert Assert.Equal(expected, actual); }
public void MakesQueriesWithProperPhrasing(string input, Query expected) { // arrange, act var actual = NuGetQuery.MakeQuery(input); // assert Assert.Equal(expected, actual); }
public void MakesQueriesSupportingSupportedFields(string input, Query expected) { // act var actual = NuGetQuery.MakeQuery(input); // assert Assert.Equal(expected, actual); }
public void MakesQueriesSupportingFieldAliases(string inputField, string expectedField) { // arrange var queryText = $"{inputField}:dot"; // act var actual = NuGetQuery.MakeQuery(queryText); // assert Assert.Contains($"{expectedField}:dot", actual.ToString()); }
public void EmptyQueryMatchesAllDocuments() { // arrange var queryText = string.Empty; var expected = new MatchAllDocsQuery(); // act var actual = NuGetQuery.MakeQuery(queryText); // assert Assert.Equal(expected, actual); }
public void CanMixTermsWithAndWithoutFieldLabels() { // arrange var owners = CreateOwnersResult(new Dictionary <string, HashSet <string> > { { "dot", new HashSet <string> { "microsoft" } } }); var queryText = "dot owner:Microsoft"; var expected = new FilteredQuery( new BooleanQuery { new BooleanClause(new BooleanQuery { Clauses = { new BooleanClause(new TermQuery(new Term("Id", "dot")), Occur.SHOULD) }, Boost = 8 }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("ShingledId", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("TokenizedId", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("Version", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("Title", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("Description", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("Summary", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { Clauses = { new BooleanClause(new TermQuery(new Term("Tags", "dot")), Occur.SHOULD) }, Boost = 2 }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("Authors", "dot")), Occur.SHOULD) }, Occur.SHOULD) }, new OwnersFilter(owners, "Microsoft")); // act var actual = NuGetQuery.MakeQuery(queryText, owners); // assert Assert.Equal(expected.ToString(), actual.ToString()); }
static void Test4() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < 10000; i++) { //Query query = LuceneQueryCreator.Parse("DotNetZip", false); Query query = NuGetQuery.MakeQuery("DotNetZip"); } stopwatch.Stop(); Console.WriteLine("{0} seconds", stopwatch.Elapsed.TotalSeconds); }
public void MakesFilteredQueriesSupportingFieldAliases(string inputField, string expectedField) { // arrange var owners = CreateOwnersResult(new Dictionary <string, HashSet <string> > { { "dot", new HashSet <string> { "dot" } } }); var queryText = $"{inputField}:dot"; // act var actual = NuGetQuery.MakeQuery(queryText, owners); // assert Assert.Contains("filtered(*:*)->NuGet.Indexing.OwnersFilter", actual.ToString()); }
public void TreatsUnrecognizedFieldAsAnyField() { // arrange var queryText = "invalid:dot"; var expected = new BooleanQuery { new BooleanClause(new BooleanQuery { Clauses = { new BooleanClause(new TermQuery(new Term("Id", "dot")), Occur.SHOULD) }, Boost = 8 }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("ShingledId", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("TokenizedId", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("Version", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("Title", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("Description", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("Summary", "dot")), Occur.SHOULD) }, Occur.SHOULD), new BooleanClause(new BooleanQuery { Clauses = { new BooleanClause(new TermQuery(new Term("Tags", "dot")), Occur.SHOULD) }, Boost = 2 }, Occur.SHOULD), new BooleanClause(new BooleanQuery { new BooleanClause(new TermQuery(new Term("Authors", "dot")), Occur.SHOULD) }, Occur.SHOULD) }; // act var actual = NuGetQuery.MakeQuery(queryText); // assert Assert.Equal(expected, actual); }
static Queue <KeyValuePair <string, JObject> > Merge(IEnumerable <Queue <KeyValuePair <string, JObject> > > results, string q) { // (1) create a combined dictionary in order to merge results that have same id IDictionary <string, JObject> combined = new Dictionary <string, JObject>(StringComparer.OrdinalIgnoreCase); foreach (var result in results) { foreach (var item in result) { JObject value; if (combined.TryGetValue(item.Key, out value)) { combined[item.Key] = MergeResult(value, item.Value); } else { combined.Add(item); } } } if (combined.Count == 0) { return(new Queue <KeyValuePair <string, JObject> >()); } // (2) create an in-memory Lucene index // **** NOTE: PackageAnalyzer is a class from NuGet.Indexing **** var directory = new RAMDirectory(); using (var writer = new IndexWriter(directory, new PackageAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED)) { foreach (var item in combined.Values) { writer.AddDocument(CreateDocument(item)); } writer.Commit(); } // (3) re-query the in-memory index // **** NOTE: NuGetQuery is a class from NuGet.Indexing **** var searcher = new IndexSearcher(directory); var query = NuGetQuery.MakeQuery(q); var topDocs = searcher.Search(query, 100); // (4) build a lookup of the local re-calculated ranking var combinedRanking = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase); int i = 0; foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs) { string id = searcher.Doc(scoreDoc.Doc).Get("Id"); combined[id]["localRank"] = i; // this is a diagnostic aid and not used in the algorithm combinedRanking[id] = i++; } // (5) now combine the results from the individual sources with a merged sort - the hashset helps filter duplicates var combinedResult = new Queue <KeyValuePair <string, JObject> >(); var distinctCombinedIds = new HashSet <string>(StringComparer.OrdinalIgnoreCase); string winner = null; do { winner = null; foreach (var result in results) { if (result.Count == 0) { continue; } var current = result.Peek().Key; if (winner == null) { winner = current; } else { if (combinedRanking[current] < combinedRanking[winner]) { winner = current; } } } if (winner != null) { if (!distinctCombinedIds.Contains(winner)) { combinedResult.Enqueue(new KeyValuePair <string, JObject>(winner, combined[winner])); distinctCombinedIds.Add(winner); } foreach (var result in results) { if (result.Count == 0) { continue; } if (distinctCombinedIds.Contains(result.Peek().Key)) { result.Dequeue(); } } } }while (winner != null); return(combinedResult); }
public void WriteAutoCompleteResultTest( string indexName, int numDocs, Dictionary <string, string> commitUserData, int topDocsTotalHits, float topDocsMaxScore, int skip, int take, bool includeExplanation, string expected) { var searcher = new MockSearcher(indexName, numDocs, commitUserData); var topDocs = new TopDocs(topDocsTotalHits, Constants.ScoreDocs, topDocsMaxScore); var sb = new StringBuilder(); var sw = new StringWriter(sb); using (var writer = new JsonTextWriter(sw)) { ResponseFormatter.WriteAutoCompleteResult(writer, searcher, topDocs, skip, take, includeExplanation, NuGetQuery.MakeQuery("test")); Assert.Equal(string.Format(expected, searcher.LastReopen, Constants.MockBase, Constants.LucenePropertyId, Constants.MockExplanationBase), sb.ToString()); } }
public void WriteSearchResultTest( string indexName, int numDocs, Dictionary <string, string> commitUserData, int topDocsTotalHits, float topDocsMaxScore, int skip, int take, bool includePrerelease, bool includeExplanation, NuGetVersion semVerlLevel, string scheme, string expectedBaseUrl, string expected) { var searcher = new MockSearcher(indexName, numDocs, commitUserData, versions: Constants.VersionResults); var topDocs = new TopDocs(topDocsTotalHits, Constants.ScoreDocs, topDocsMaxScore); var sb = new StringBuilder(); var sw = new StringWriter(sb); using (var writer = new JsonTextWriter(sw)) { ResponseFormatter.WriteSearchResult(writer, searcher, scheme, topDocs, skip, take, includePrerelease, includeExplanation, semVerlLevel, NuGetQuery.MakeQuery("test")); Assert.Equal(string.Format(expected, expectedBaseUrl, searcher.LastReopen, Constants.MockBase.ToLower(), Constants.LucenePropertyId.ToLower(), Constants.MockBase, Constants.LucenePropertyId, Constants.LucenePropertyFullVersion, Constants.LucenePropertyDescription, Constants.LucenePropertySummary, Constants.LucenePropertyTitle, Constants.LucenePropertyIconUrl, Constants.LucenePropertyLicenseUrl, Constants.LucenePropertyProjectUrl), sb.ToString()); } }