public void Parse() { var mapper = new AttributesMappingManager(); var parser = new CollapseExpandResponseParser<Doc>(new SolrDocumentResponseParser<Doc>(mapper, new DefaultDocumentVisitor(mapper, new DefaultFieldParser()), new SolrDocumentActivator<Doc>())); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.collapseWithoutExpandResponse.xml"); var results = new SolrQueryResults<Doc>(); parser.Parse(xml, results); Assert.IsNull(results.CollapseExpand); }
public void ParseResponseWithLocation() { var mapper = new AttributesMappingManager(); var parser = new DefaultResponseParser<Doc>(new SolrDocumentResponseParser<Doc>(mapper, new DefaultDocumentVisitor(mapper, new DefaultFieldParser()), new SolrDocumentActivator<Doc>())); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.response.xml"); var results = new SolrQueryResults<Doc>(); parser.Parse(xml, results); Assert.AreEqual(1, results.Count); Assert.AreEqual(new Location(51.5171, -0.1062), results[0].Loc); }
public void Parse_If_Both_Result_And_Groups_Are_Present() { var mapper = new AttributesMappingManager(); var parser = new DefaultResponseParser<TestDoc>(new SolrDocumentResponseParser<TestDoc>(mapper, new DefaultDocumentVisitor(mapper, new DefaultFieldParser()), new SolrDocumentActivator<TestDoc>())); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.responseWithResultAndGroup.xml"); var results = new SolrQueryResults<TestDoc>(); parser.Parse(xml, results); Assert.AreEqual(1, results.Count); Assert.AreEqual(1, results.Grouping["titleId"].Ngroups); }
public void ParseResponseWithSimpleDebugExplanation() { var parser = new DebugResponseParser<object>(); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.responseWithSimpleDebugDetails.xml"); var results = new SolrQueryResults<object>(); parser.Parse(xml, results); Assert.AreEqual(0, results.Count); Assert.IsTrue(results.Debug is DebugResults.PlainDebugResults); Assert.IsNotNull(results.Debug.Explanation); Assert.AreEqual(2, results.Debug.Explanation.Count); }
public void Parse2() { var parser = new CollapseResponseParser<TestDoc>(); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.collapseResponse2.xml"); var results = new SolrQueryResults<TestDoc>(); parser.Parse(xml, results); Assert.IsNotNull(results.Collapsing); Assert.AreEqual("manu_exact", results.Collapsing.Field); Assert.AreEqual(3, results.Collapsing.CollapsedDocuments.Count); var firstCollapse = results.Collapsing.CollapsedDocuments.ElementAt(0); Assert.AreEqual("F8V7067-APL-KIT", firstCollapse.Id); Assert.AreEqual(1, firstCollapse.CollapseCount); Assert.AreEqual("Belkin", firstCollapse.FieldValue); }
public void ParseDebugResponse() { var parser = new DebugResponseParser<object>(); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.responseWithSimpleDebugDetails.xml"); var results = new SolrQueryResults<object>(); parser.Parse(xml, results); Assert.AreEqual(0, results.Count); Assert.IsTrue(results.Debug is DebugResults.PlainDebugResults); Assert.IsNotNull(results.Debug.Timing); Assert.AreEqual(15, results.Debug.Timing.TotalTime); Assert.AreEqual(7, results.Debug.Timing.Prepare.Count); Assert.AreEqual(7, results.Debug.Timing.Process.Count); }
public void Parse2() { var mapper = new AttributesMappingManager(); var parser = new CollapseExpandResponseParser<Doc>(new SolrDocumentResponseParser<Doc>(mapper, new DefaultDocumentVisitor(mapper, new DefaultFieldParser()), new SolrDocumentActivator<Doc>())); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.collapseWithExpandResponse.xml"); var results = new SolrQueryResults<Doc>(); parser.Parse(xml, results); Assert.IsNotNull(results.CollapseExpand); Assert.AreEqual(4, results.CollapseExpand.Groups.Count); var group = results.CollapseExpand.Groups.ElementAt(0); Assert.AreEqual(group.Documents.Count, 2); Assert.AreEqual(group.GroupValue, "First"); Assert.AreEqual(group.NumFound, 2); }
public void ParseResponseWithStructuredDebugExplanation() { var parser = new DebugResponseParser<object>(); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.responseWithStructuredDebugDetails.xml"); var results = new SolrQueryResults<object>(); parser.Parse(xml, results); var debugData = results.Debug; Assert.AreEqual(0, results.Count); Assert.IsNotNull(results.Debug.Explanation); Assert.IsTrue(results.Debug is DebugResults.StructuredDebugResults); var structuredDebugData = debugData as DebugResults.StructuredDebugResults; Assert.IsNotNull(structuredDebugData); Assert.AreEqual(2, structuredDebugData.ExplanationStructured.Count); }
public void ParseResponseWithStructuredDebugExplanation() { var parser = new DebugResponseParser <object>(); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.responseWithStructuredDebugDetails.xml"); var results = new SolrQueryResults <object>(); parser.Parse(xml, results); var debugData = results.Debug; Assert.Equal(0, results.Count); Assert.NotNull(results.Debug.Explanation); Assert.True(results.Debug is DebugResults.StructuredDebugResults); var structuredDebugData = debugData as DebugResults.StructuredDebugResults; Assert.NotNull(structuredDebugData); Assert.Equal(2, structuredDebugData.ExplanationStructured.Count); }
public void TestSearchNonExistingDoc() { string prodtosearch = "NonExistingProduct"; int expectedrescount = 0; SearchCls s = new SearchCls(); SolrQueryResults <Product> res = s.searchProd("ProdName", prodtosearch); int actual = 0; if (res != null) { actual = res.Count; Assert.AreEqual(expectedrescount, actual); } else { Assert.Fail(); } }
private List <Facet> ParseFacetResults(SolrQueryResults <MetadataIndexDoc> queryResults) { List <Facet> facets = new List <Facet>(); if (queryResults != null) { foreach (var key in queryResults.FacetFields.Keys) { var facet = new Facet(key); foreach (var facetValueResult in queryResults.FacetFields[key]) { facet.FacetResults.Add(new Facet.FacetValue(facetValueResult)); } facets.Add(facet); } } return(facets); }
public EmployeeModel GetEmployeeDetail(ISolrOperations <SearchResultItem> solr, string id) { EmployeeModel empmodel = null; SolrQueryResults <SearchResultItem> searchItems = solr.Query(new SolrQueryByField("id", id)); if (searchItems != null) { empmodel = new EmployeeModel { Id = searchItems[0].Id, FirstName = searchItems[0].FirstName, LastName = searchItems[0].LastName, Address = searchItems[0].Address, Salary = searchItems[0].Salary, Department = searchItems[0].Department }; } return(empmodel); }
static void Main(string[] args) { Startup.Init <Product>("http://localhost:8983/solr/demo"); ISolrOperations <Product> solr = ServiceLocator.Current.GetInstance <ISolrOperations <Product> >(); //IQueryable<ISelectExpandWrapper> sr = solr.AsQueryable().OData().Filter("Id ne null and Price gt 0").OrderBy("Id desc").TopSkip("1", "1") // .SelectExpandAsQueryable("Id,Price,Categories"); //Console.WriteLine(sr.GetType()); //Console.WriteLine(sr.ToJson()); SolrQueryResults <ISelectExpandWrapper> results = solr.AsQueryable().OData().Filter("Id ne null").OrderBy("Id desc").TopSkip("1", "1") .SelectExpandAsQueryable("Id,Price,Categories").ToSolrQueryResults(); Console.WriteLine(results.NumFound); Console.WriteLine(results.ToJson()); }
public void ParseFacetResults() { var parser = new FacetsResponseParser <TestDocumentWithArrays>(); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.responseWithFacet.xml"); var r = new SolrQueryResults <TestDocumentWithArrays>(); parser.Parse(xml, r); Assert.IsNotNull(r.FacetFields); //Console.WriteLine(r.FacetFields.Count); Assert.IsTrue(r.FacetFields.ContainsKey("cat")); Assert.IsTrue(r.FacetFields.ContainsKey("inStock")); Assert.AreEqual(2, r.FacetFields["cat"].First(q => q.Key == "connector").Value); Assert.AreEqual(2, r.FacetFields["cat"].First(q => q.Key == "").Value); // facet.missing as empty string Assert.IsNotNull(r.FacetQueries); //Console.WriteLine(r.FacetQueries.Count); Assert.AreEqual(1, r.FacetQueries.Count); }
public void SelectDerivedWithCastToInterface() { IQueryable <DerivedProduct> derivedProducts = Product.SolrOperations.Value.AsQueryable() .Where(p => p.Id != null && p.Categories.Any()) .Select(p => new DerivedProduct { Id2 = p.Id, Categories = p.Categories }) .Where(p => p.Id2 != null); IQueryable <IProduct> q2 = derivedProducts.Cast <IProduct>(); SolrQueryResults <IProduct> t1 = q2.ToSolrQueryResults(); Assert.NotNull(t1); Assert.NotNull(t1[0].Categories); Assert.Null(t1[0].Id); Assert.True(t1.NumFound > 0); }
private List <SearchResultItem> ParseResultDocuments(SolrQueryResults <ServiceIndexDoc> queryResults) { register = new RegisterFetcher(); var items = new List <SearchResultItem>(); if (queryResults != null) { foreach (var doc in queryResults) { Log.Debug(doc.Score + " " + doc.Title + " " + doc.Uuid); var item = new SearchResultItem(doc); item.DistributionType = register.GetDistributionType(item.DistributionProtocol); items.Add(item); } } return(items); }
public SolrQueryResults <Product> searchProd(String fieldname, String val) { SolrQueryResults <Product> res = null; try { Console.WriteLine("Searching Product with " + fieldname + " = " + val); var solr = ServiceLocator.Current.GetInstance <ISolrOperations <Product> >(); res = solr.Query(new SolrQueryByField(fieldname, val)); ResponseHeader rh = res.Header; Console.WriteLine("Response Status =" + rh.Status + "\t Operation Elapsed time :" + rh.QTime); Console.WriteLine("Total Number of Documents in Search Result=" + res.Count); if (res.Count > 0) { for (int resultindex = 0; resultindex < res.Count; resultindex++) { foreach (Object o in res[resultindex].ProductName) { Console.WriteLine("ProductName=" + o); } foreach (Object o in res[resultindex].Id) { Console.WriteLine("id=" + o); } foreach (Object o in res[resultindex].InStock) { Console.WriteLine("InStock=" + o); } foreach (Object o in res[resultindex].Manufacturer) { Console.WriteLine("Manufacturer=" + o); } foreach (Object o in res[resultindex].price) { Console.WriteLine("Price=" + o); } } //for } //if } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine(e); } return(res); }
public void Product2() { var t1 = Product.SolrOperations.Value.AsQueryable(lo => lo.SetupQueryOptions = qo => { Assert.Equal(4, qo.Fields.Count); Assert.Equal("id", qo.Fields.ElementAt(0)); Assert.Equal("price", qo.Fields.ElementAt(1)); Assert.Equal("cat", qo.Fields.ElementAt(2)); Assert.Equal("v1731e0:score", qo.Fields.ElementAt(3)); Assert.Equal(2, qo.OrderBy.Count); Assert.Equal("id", qo.OrderBy.ElementAt(0).FieldName); Assert.Equal("score", qo.OrderBy.ElementAt(1).FieldName); Assert.Equal(2, qo.FilterQueries.Count); }).Where(p => p.Id != null) .Select(p => new Product2 { Id = p.Id, Price = p.Price, Categories = p.Categories, Score = SolrExpr.Fields.Score() }) .Where(arg => arg.Categories.Any(s => s == "electronics")) .OrderBy(arg => arg.Id).ThenBy(arg => arg.Score) .FirstOrDefault(); Assert.NotNull(t1); Assert.NotNull(t1.Id); var t2 = Product.SolrOperations.Value.AsQueryable().Where(p => p.Id != null) .Where(arg => arg.Categories.Any(s => s == "electronics")) .OrderBy(arg => arg.Id) .FirstOrDefault(); Assert.NotNull(t2); Assert.Equal(t2.Id, t1.Id); SolrQueryResults <Product2> t3 = Product.SolrOperations.Value.AsQueryable().Where(p => p.Id != null) .Select(p => new Product2 { Id = p.Id, Price = p.Price, Categories = p.Categories }) .Where(arg => arg.Categories.Any(s => s == "electronics")) .OrderBy(arg => arg.Id).Take(1).ToSolrQueryResults(); Assert.Single(t3); Assert.Equal(t1.Id, t3.Single().Id); }
/// <summary> /// Parses the grouped elements /// </summary> /// <param name="xml"></param> /// <param name="results"></param> public void Parse(XDocument xml, SolrQueryResults <T> results) { var mainGroupingNode = xml.Element("response") .Elements("lst") .FirstOrDefault(X.AttrEq("name", "grouped")); if (mainGroupingNode == null) { return; } var groupings = from groupNode in mainGroupingNode.Elements() let groupName = groupNode.Attribute("name").Value let groupResults = ParseGroupedResults(groupNode) select new { groupName, groupResults }; results.Grouping = groupings.ToDictionary(x => x.groupName, x => x.groupResults); }
public void ProfileTest(ProfilingContainer container) { var parser = container.Resolve <ISolrAbstractResponseParser <TestDocumentWithArrays> >(); var xml = EmbeddedResource.GetEmbeddedXml(GetType(), "Resources.responseWithArraysSimple.xml"); var results = new SolrQueryResults <TestDocumentWithArrays>(); for (var i = 0; i < 1000; i++) { parser.Parse(xml, results); } var profile = Flatten(container.GetProfile()); var q = from n in profile group n.Value by n.Key into x let kv = new { method = x.Key, count = x.Count(), total = x.Sum(t => t.TotalMilliseconds) } orderby kv.total descending select kv; }
public SearchResult Search(SearchParameters parameters) { ISolrQuery query = BuildQuery(parameters); var order = new[] { new SortOrder("score", Order.DESC) }; if (parameters.OrderBy == "name") { order = new[] { new SortOrder("RegisterItemName", Order.ASC) }; } else if (parameters.OrderBy == "date_updated") { order = new[] { new SortOrder("RegisterItemUpdated", Order.DESC) }; } else if (string.IsNullOrWhiteSpace(parameters.Text) && HasNoFacetvalue(parameters.Facets)) { order = new[] { new SortOrder("RegisterItemName", Order.ASC) }; } else if (parameters.OrderBy == "score") { order = new[] { new SortOrder("score", Order.DESC) }; } try { SolrQueryResults <RegisterIndexDoc> queryResults = _solrInstance.Query(query, new QueryOptions { FilterQueries = BuildFilterQueries(parameters), OrderBy = order, Rows = parameters.Limit, Start = parameters.Offset - 1, Facet = BuildFacetParameters(parameters), Fields = new[] { "SystemID", "RegisterName", "RegisterDescription", "RegisterItemName", "RegisterItemName_en", "RegisterItemDescription", "RegisterID", "Discriminator", "RegisterItemUpdated", "type", "ParentRegisterUrl", "RegisteItemUrl", "SubregisterUrl", "subregisterItemUrl", "theme", "organization" } }); return(CreateSearchResults(queryResults, parameters)); } catch (Exception ex) { Log.Error("Error in search", ex); return(CreateSearchResults(null, parameters)); } }
private static void GetValue(Query query, List <SitecoreItem> items) { if (Index is Index) { if ((Index as Index).Name == "itembuckets_templates") { var solr = ServiceLocator.Current.GetInstance <ISolrOperations <SolrTemplateItem> >(); SolrQueryResults <SolrTemplateItem> remoteSearch = solr.Query(new SolrQuery(query.ToString())); SearchHelper.GetItemsFromSearchResultFromSOLR(remoteSearch, items); } if ((Index as Index).Name == "itembuckets_buckets") { var solr = ServiceLocator.Current.GetInstance <ISolrOperations <SolrBucketItem> >(); SolrQueryResults <SolrBucketItem> remoteSearch = solr.Query(new SolrQuery(query.ToString())); SearchHelper.GetItemsFromSearchResultFromSOLR(remoteSearch, items); } if ((Index as Index).Name == "itembuckets_sitecore") { var solr = ServiceLocator.Current.GetInstance <ISolrOperations <SolrSitecoreItem> >(); SolrQueryResults <SolrSitecoreItem> remoteSearch = solr.Query(new SolrQuery(query.ToString())); SearchHelper.GetItemsFromSearchResultFromSOLR(remoteSearch, items); } if ((Index as Index).Name == "itembuckets_layoutsfolder") { var solr = ServiceLocator.Current.GetInstance <ISolrOperations <SolrLayoutItem> >(); SolrQueryResults <SolrLayoutItem> remoteSearch = solr.Query(new SolrQuery(query.ToString())); SearchHelper.GetItemsFromSearchResultFromSOLR(remoteSearch, items); } if ((Index as Index).Name == "itembuckets_systemfolder") { var solr = ServiceLocator.Current.GetInstance <ISolrOperations <SolrSystemItem> >(); SolrQueryResults <SolrSystemItem> remoteSearch = solr.Query(new SolrQuery(query.ToString())); SearchHelper.GetItemsFromSearchResultFromSOLR(remoteSearch, items); } if ((Index as Index).Name == "itembuckets_medialibrary") { var solr = ServiceLocator.Current.GetInstance <ISolrOperations <SolrMediaItem> >(); SolrQueryResults <SolrMediaItem> remoteSearch = solr.Query(new SolrQuery(query.ToString())); SearchHelper.GetItemsFromSearchResultFromSOLR(remoteSearch, items); } } }
public IActionResult Update() { string id = HttpContext.Request.Query["q"]; QueryOptions options = new QueryOptions(); // https://github.com/SolrNet/SolrNet/blob/master/Documentation/CRUD.md options.RequestHandler = new RequestHandlerParameters("/get"); options.ExtraParams = new Dictionary <string, string> { { "ids", id } }; SolrQueryResults <SolrModel> results = solr.Query(new SolrQuery("*:*"), options); var model = new PersonalWebsiteMVC.Models.SolrModel(); model.ID = results.Select(r => r.ID).FirstOrDefault(); model.Title = results.Select(r => r.Title).FirstOrDefault(); model.Url = results.Select(r => r.Url).FirstOrDefault(); model.Body = results.Select(r => r.Body).FirstOrDefault(); return(View(model)); }
public SearchResult CreateSearchResults(SolrQueryResults <MetadataIndexDoc> queryResults, SearchParameters parameters) { List <SearchResultItem> items = ParseResultDocuments(queryResults); List <Facet> facets = ParseFacetResults(queryResults); SearchResult searchResult = new SearchResult { Items = items, Facets = facets, Limit = parameters.Limit, Offset = parameters.Offset, Type = GetType(queryResults) }; if (queryResults != null) { searchResult.NumFound = queryResults.NumFound; } return(searchResult); }
private static void SetSearchModelMainResults(SolrQueryResults <Hitter> results, HitterSearchViewModel searchModel) { searchModel.SearchResults = results.Select(r => new HitterSearchResult { Average = r.Average, Doubles = r.Doubles, FirstName = r.FirstName, Hits = r.Hits, HomeRuns = r.HomeRuns, Id = r.Id, LahmanId = r.LahmanId, LastName = r.LastName, PlayerId = r.PlayerId, RunsBattedIn = r.RunsBattedIn, Salary = r.Salary, StrikeOuts = r.StrikeOuts, TeamName = r.TeamName, Triples = r.Triples, Year = r.Year }).ToList(); }
public QueryResult <T> GetRaw(string query) { var queryResults = new SolrQueryResults <T>(); query = query.RemoveSpecialCharacters(); if (!string.IsNullOrWhiteSpace(query)) { queryResults = base._solrInstance.Query(query.RemoveSpecialCharacters()); } var solrProds = queryResults; var searchResponse = new QueryResult <T> { NumberOfTotalResults = solrProds.NumFound, Results = solrProds, CurrentPage = 0, PageSize = 0 }; return(searchResponse); }
public Statistics GetStatistics() { Statistics statistics = new Statistics(); SolrQueryResults <CmsSearchResultItem> source = ServiceLocator.Current.GetInstance <ISolrOperations <CmsSearchResultItem> >().Query((ISolrQuery) new SolrQuery("*:*")); statistics.NumDocs = source.NumFound; foreach (string str in source.SelectMany <CmsSearchResultItem, string>((Func <CmsSearchResultItem, IEnumerable <string> >)(i => (IEnumerable <string>)i.ContentType)).Distinct <string>()) { string contenType = str; int num = source.Count <CmsSearchResultItem>((Func <CmsSearchResultItem, bool>)(i => { if (i.ContentType != null) { return(i.ContentType.Contains(contenType)); } return(false); })); statistics.RichTextDocuments.Add(contenType, num.ToString((IFormatProvider)CultureInfo.InvariantCulture)); } return(statistics); }
public MetadataIndexDoc GetMetadata(string uuid) { MetadataIndexDoc metadata = null; var solrInstance = MvcApplication.indexContainer.Resolve <ISolrOperations <MetadataIndexDoc> >(CultureHelper.GetIndexCore(SolrCores.Metadata)); ISolrQuery query = new SolrQuery("uuid:" + uuid); try { SolrQueryResults <MetadataIndexDoc> queryResults = solrInstance.Query(query, new SolrNet.Commands.Parameters.QueryOptions { Fields = new[] { "uuid", "title", "abstract", "purpose", "type", "theme", "organization", "organization_seo_lowercase", "placegroups", "organizationgroup", "topic_category", "organization_logo_url", "thumbnail_url", "distribution_url", "distribution_protocol", "distribution_name", "product_page_url", "date_published", "date_updated", "nationalinitiative", "score", "ServiceDistributionProtocolForDataset", "ServiceDistributionUrlForDataset", "ServiceDistributionNameForDataset", "DistributionProtocols", "legend_description_url", "product_sheet_url", "product_specification_url", "area", "datasetservice", "popularMetadata", "bundle", "servicelayers", "accessconstraint", "servicedataset", "otherconstraintsaccess", "dataaccess", "ServiceDistributionUuidForDataset", "ServiceDistributionAccessConstraint", "parentidentifier", "serie", "seriedatasets", "distributions" } }); metadata = queryResults.FirstOrDefault(); } catch (Exception) { } return(metadata); }
public void SetBody(QueryResponse queryResponse, SolrQueryResults <Course> solrResult) { queryResponse.Result = solrResult as List <Course>; foreach (var course in queryResponse.Result) { if (solrResult.Highlights.ContainsKey(course.CourseId)) { HighlightedSnippets snippets = solrResult.Highlights[course.CourseId]; if (snippets.ContainsKey("coursetitle")) { course.CourseTitle = snippets["coursetitle"].FirstOrDefault(); } if (snippets.ContainsKey("description")) { course.Description = snippets["description"].FirstOrDefault(); } } } }
public SearchResultForOrganization SearchByOrganization(SearchByOrganizationParameters parameters) { parameters.CreateFacetOfOrganizationSeoName(); parameters.AddComplexFacetsIfMissing(); ISolrQuery query = parameters.BuildQuery; SolrQueryResults <MetadataIndexDoc> queryResults = _solrInstance.Query(query, new QueryOptions { FilterQueries = parameters.BuildFilterQueries(), OrderBy = parameters.OrderBy(), Rows = parameters.Limit, Start = parameters.Offset - 1, //solr is zero-based - we use one-based indexing in api Facet = parameters.BuildFacetParameters() }); SearchResult searchResult = CreateSearchResults(queryResults, parameters); Task <Organization> getOrganizationTask = _organizationService.GetOrganizationByName(parameters.OrganizationSeoName); Organization organization = getOrganizationTask.Result; return(new SearchResultForOrganization(organization, searchResult)); }
public MovieResult AdvancedSearch(AdvancedSearch model) { FacetParameters parameters = new FacetParameters(); if (model.UseDate) { //parameters.Queries.Add(new SolrFacetDateQuery("")); } SolrQueryResults <Movie> results = _solr .Query(SolrQuery.All, new QueryOptions() { Rows = PageSize, Facet = parameters }); var result = new MovieResult { Movies = results.ToList(), }; return(result); }
public Kartverket.Metadatakatalog.Models.Article.SearchResult Search(Kartverket.Metadatakatalog.Models.Article.SearchParameters parameters) { ISolrQuery query = parameters.BuildQuery(); try { SolrQueryResults <ArticleIndexDoc> queryResults = _solrInstance.Query(query, new QueryOptions { OrderBy = parameters.OrderBy(), Rows = parameters.Limit, Start = parameters.Offset - 1, //solr is zero-based - we use one-based indexing in api Fields = new[] { "Id", "Type", "title", "LinkUrl", "MainIntro", "MainBody", "StartPublish", "Author", "LinkArea", "score" } }); return(CreateSearchResults(queryResults, parameters)); } catch (Exception ex) { Log.Error("Error in search", ex); return(CreateSearchResults(null, parameters)); } }
public static int[] QueryQuesIds(int courseId, int kpointId, int[] cpointIds, int typeId, int diffType, int paperYear, int pageIndex, int pageSize, out int totalCount) { List <int> list = new List <int>();; ISolrOperations <dto_SolrQues> solr = ServiceLocator.Current.GetInstance <ISolrOperations <dto_SolrQues> >(); //SolrMultipleCriteriaQuery sq = GetSolrQueryByField(courseId, kpointId, cpointIds, typeId, diffType, paperYear); string sq = GetSolrQueryByString(courseId, kpointId, cpointIds, typeId, diffType, paperYear); QueryOptions options = GetOptions(pageIndex, pageSize); SolrQueryResults <dto_SolrQues> solrResults = solr.Query(sq, options); //总数 totalCount = solrResults.NumFound; if (solrResults.Count != 0) { foreach (var solrQueryResult in solrResults) { list.Add(solrQueryResult.id); } } return(list.ToArray()); }
private static void SetSearchModelMainResults(SolrQueryResults<Hitter> results, HitterSearchViewModel searchModel) { searchModel.SearchResults = results.Select(r => new HitterSearchResult { Average = r.Average, Doubles = r.Doubles, FirstName = r.FirstName, Hits = r.Hits, HomeRuns = r.HomeRuns, Id = r.Id, LahmanId = r.LahmanId, LastName = r.LastName, PlayerId = r.PlayerId, RunsBattedIn = r.RunsBattedIn, Salary = r.Salary, StrikeOuts = r.StrikeOuts, TeamName = r.TeamName, Triples = r.Triples, Year = r.Year }).ToList(); }
public string DoSearch(string mQuery, string mStart, string mLength) { var mTimer = new Stopwatch(); mTimer.Start(); string strRV = string.Empty; ISolrOperations<Dictionary<string, object>> mSolr = default(ISolrOperations<Dictionary<string, object>>); // This method must be improved at some stage to remove the unecessary exception try { mSolr = ServiceLocator.Current.GetInstance<ISolrOperations<Dictionary<string, object>>>(); } catch (Exception) { Startup.Init<Dictionary<string, object>>(Utility.solrInstance); mSolr = ServiceLocator.Current.GetInstance<ISolrOperations<Dictionary<string, object>>>(); } SolrQueryResults<Dictionary<string, object>> mSolrResults = new SolrQueryResults<Dictionary<string, object>>(); SolrNet.Commands.Parameters.QueryOptions mQueryOptions = new SolrNet.Commands.Parameters.QueryOptions(); //Set offset from start (used to load page number "N") if (Utility.IsNumeric(mStart)) { mQueryOptions.Start = Convert.ToInt32(mStart); } else { mQueryOptions.Start = 0; } //Set pagesize if (Utility.IsNumeric(mLength)) { mQueryOptions.Rows = Convert.ToInt32(mLength); } else { mQueryOptions.Rows = 10; } //Set default query if (string.IsNullOrEmpty(mQuery)) { mQuery = "*:*"; } //Execute query try { mSolrResults = mSolr.Query(mQuery, mQueryOptions); //Create result container Dictionary<string, object> mDictionary = new Dictionary<string, object>(); mDictionary.Add("code", Utility.msgStatusCodes.OperationCompletedSuccessfully); mDictionary.Add("data", mSolrResults); //Create JSON and return JavaScriptSerializer serializer = new JavaScriptSerializer(); Utility.LogRequest(strRV, false, mTimer); strRV = serializer.Serialize(mDictionary); } catch (Exception ex) { Utility.LogRequest(strRV, true, mTimer); strRV = Utility.GetMsg(Utility.msgStatusCodes.OperationFailed, "SOLR error: " + ex.Message); } mSolr = null; mSolrResults = null; return strRV; }
private static void GenerateFacetedSearchCategories(SolrQueryResults<Hitter> results, HitterSearchViewModel searchModel) { searchModel.Categories = new Dictionary<String, HitterCategory>(); foreach (var category in results.FacetQueries) { var categoryinfo = category.Key.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (searchModel.Categories.ContainsKey(categoryinfo[0])) { //key exists, just add the item info searchModel.Categories[categoryinfo[0]].Items.Add(new CategoryItem() { Count = category.Value, Item = categoryinfo[1].TrimStart('[').TrimEnd(']'), ItemCriteria = categoryinfo[1] }); } else { //key doesn't exist...create it searchModel.Categories.Add(categoryinfo[0], new HitterCategory() { CategoryField = categoryinfo[0], Items = new List<CategoryItem>() { new CategoryItem() { Count = category.Value, Item = categoryinfo[1].TrimStart('[').TrimEnd(']'), ItemCriteria = categoryinfo[1] }} }); } } }
public void FacetFields_NotNullByDefault() { var r = new SolrQueryResults<Entity>(); Assert.IsNotNull(r.FacetFields); }
public FacetViewModel BuildViewModel(SolrQueryResults<Product> results) { var queryStringBuilder = HttpUtility.ParseQueryString(string.Empty); foreach (var facet in _facets.Where(facet => facet != this)) { facet.GetAppliedConstraints(queryStringBuilder); } Constraint[] constraints; if (RangeQuery.HasRuledOutConstraints(results)) { constraints = (from range in _ranges let ruledOutFacetQueryResult = results.FacetQueries.Single(query => RangeQuery.IsRuledOutConstraint(query, range)) let ruledInFacetQueryResult = results.FacetQueries.SingleOrDefault( query => RangeQuery.IsRuledInConstraint(query, range) && query.Value > 0) let isRuledOut = ruledInFacetQueryResult.Equals(default(KeyValuePair<string, int>)) let facetQueryResult = isRuledOut ? ruledOutFacetQueryResult : ruledInFacetQueryResult select new Constraint(BuildConstraintName(range.SolrMin, range.SolrMax), range, _appliedRanges, queryStringBuilder, _smallRangeQuerystringKey, _largeRangeQuerystringKey, facetQueryResult, isRuledOut)) .ToArray(); } else { constraints = (from range in _ranges let ruledInFacetQueryResult = results.FacetQueries.Single(query => RangeQuery.IsRuledInConstraint(query, range)) select new Constraint(BuildConstraintName(range.SolrMin, range.SolrMax), range, _appliedRanges, queryStringBuilder, _smallRangeQuerystringKey, _largeRangeQuerystringKey, ruledInFacetQueryResult)) .ToArray(); } if (_freeFormSearch) { return new FacetViewModel(_appliedRanges.Single(), constraints); } if (!_singleSelection || !constraints.Any(constraint => constraint.IsApplied)) { return new FacetViewModel(constraints); } foreach (var constraint in constraints.Where(constraint => !constraint.IsApplied)) { constraint.Disable(); } return new FacetViewModel(constraints); }
public FacetViewModel BuildViewModel(SolrQueryResults<Product> results) { var queryStringBuilder = HttpUtility.ParseQueryString(string.Empty); foreach (var facet in _facets.Where(facet => facet != this)) { facet.GetAppliedConstraints(queryStringBuilder); } Constraint[] constraints; if (results.FacetFields.Keys.Contains(_constraintKeyRuledOut)) { constraints = (from ruledOutConstraint in results.FacetFields[_constraintKeyRuledOut].OrderBy(facetfield => facetfield.Key) let ruledInConstraint = results.FacetFields[_constraintKeyRuledIn].OrderBy(facetfield => facetfield.Key) .SingleOrDefault( constraint => constraint.Key == ruledOutConstraint.Key && constraint.Value > 0) let isRuledOut = ruledInConstraint.Equals(default(KeyValuePair<string, int>)) let constraint = isRuledOut ? ruledOutConstraint : ruledInConstraint select new Constraint(constraint, _searchParameters, queryStringBuilder, _queryStringKey, isRuledOut)) .ToArray(); } else { constraints = results .FacetFields[_constraintKeyRuledIn] .OrderBy(facetField => facetField.Key) .Select( facetField => new Constraint(facetField, _searchParameters, queryStringBuilder, _queryStringKey)) .ToArray(); } if (!_singleSelection || !constraints.Any(constraint => constraint.IsApplied)) { return new FacetViewModel(constraints); } foreach (var constraint in constraints.Where(constraint => !constraint.IsApplied)) { constraint.Disable(); } return new FacetViewModel(constraints); }
public static void GetItemsFromSearchResultFromSOLR(SolrQueryResults<SolrSystemItem> searchResults, List<SitecoreItem> items) { foreach (var result in searchResults) { var uriField = result.Url; if (uriField.IsNotNull() && !uriField.IsNullOrEmpty()) { AssignFieldValues(result, uriField, items); } } }
private string GetSpellCheckingResult(SolrQueryResults<SolrResultSet> Provider) { return string.Join(" ", Provider.SpellChecking .Select(c => c.Suggestions.FirstOrDefault()) .Where(c => !string.IsNullOrEmpty(c)) .ToArray()); }
public void SearchResults_ShouldBeIterable() { var results = new SolrQueryResults<string>(); Assert.IsInstanceOfType(typeof(IEnumerable<string>), results); }
/// <summary> Perform an search for documents with matching parameters </summary> /// <param name="AggregationCode"> Aggregation code within which to search </param> /// <param name="QueryString"> Quert string for the actual search to perform aggainst the Solr/Lucene engine </param> /// <param name="ResultsPerPage"> Number of results to display per a "page" of results </param> /// <param name="Page_Number"> Which page of results to return ( one-based, so the first page is page number of one )</param> /// <param name="Sort"> Sort to apply before returning the results of the search </param> /// <param name="Tracer"> Trace object keeps a list of each method executed and important milestones in rendering </param> /// <param name="Complete_Result_Set_Info"> [OUT] Information about the entire set of results </param> /// <param name="Paged_Results"> [OUT] List of search results for the requested page of results </param> /// <returns> Page search result object with all relevant result information </returns> public static bool Search(string AggregationCode, string QueryString, int ResultsPerPage, int Page_Number, ushort Sort, Custom_Tracer Tracer, out Search_Results_Statistics Complete_Result_Set_Info, out List <iSearch_Title_Result> Paged_Results) { if (Tracer != null) { Tracer.Add_Trace("Solr_Documents_Searcher.Search", String.Empty); } // Set output initially to null Paged_Results = new List <iSearch_Title_Result>(); Complete_Result_Set_Info = null; try { // Ensure page is not erroneously set to zero or negative if (Page_Number <= 0) { Page_Number = 1; } // Create the solr worker to query the document index var solrWorker = Solr_Operations_Cache <Solr_Document_Result> .GetSolrOperations(Engine_ApplicationCache_Gateway.Settings.Servers.Document_Solr_Index_URL); // Create the query options QueryOptions options = new QueryOptions { Rows = ResultsPerPage, Start = (Page_Number - 1) * ResultsPerPage, Fields = new[] { "did", "score", "url", "aleph", "donor", "edition", "format", "holdinglocation", "sourceinstitution", "maintitle", "materialtype", "oclc", "pubdate_display", "author_display", "publisher_display", "mainthumbnail" }, Highlight = new HighlightingParameters { Fields = new[] { "fulltext" }, }, ExtraParams = new Dictionary <string, string> { { "hl.useFastVectorHighlighter", "true" } } }; // Set the sort value if (Sort != 0) { options.OrderBy.Clear(); switch (Sort) { case 1: options.OrderBy.Add(new SortOrder("maintitle_sort")); break; case 2: options.OrderBy.Add(new SortOrder("bibid", Order.ASC)); break; case 3: options.OrderBy.Add(new SortOrder("bibid", Order.DESC)); break; case 10: options.OrderBy.Add(new SortOrder("pubdate", Order.ASC)); break; case 11: options.OrderBy.Add(new SortOrder("pubdate", Order.DESC)); break; } } // If there was an aggregation code included, put that at the beginning of the search if ((AggregationCode.Length > 0) && (AggregationCode.ToUpper() != "ALL")) { QueryString = "(aggregation_code:" + AggregationCode.ToUpper() + ")AND(" + QueryString + ")"; } // Perform this search SolrQueryResults <Solr_Document_Result> results = solrWorker.Query(QueryString, options); // Create the search statistcs List <string> metadataLabels = new List <string> { "Author", "Publisher", "Format", "Edition", "Institution", "Donor" }; Complete_Result_Set_Info = new Search_Results_Statistics(metadataLabels) { Total_Titles = results.NumFound, Total_Items = results.NumFound, QueryTime = results.Header.QTime }; // Pass all the results into the List and add the highlighted text to each result as well foreach (Solr_Document_Result thisResult in results) { // Add the highlight snipper if ((results.Highlights.ContainsKey(thisResult.DID)) && (results.Highlights[thisResult.DID].Count > 0) && (results.Highlights[thisResult.DID].ElementAt(0).Value.Count > 0)) { thisResult.Snippet = results.Highlights[thisResult.DID].ElementAt(0).Value.ElementAt(0); } // Add this results Paged_Results.Add(thisResult); } return(true); } catch { return(false); } }
private TResult ApplyScalarMethods <TResult, TDocument>(SolrCompositeQuery compositeQuery, SolrSearchResults <TDocument> processedResults, SolrQueryResults <Dictionary <string, object> > results) { var queryMethod = compositeQuery.Methods.First(); object obj; switch (queryMethod.MethodType) { case QueryMethodType.All: obj = true; break; case QueryMethodType.Any: obj = processedResults.Any() ? 1 : 0; break; case QueryMethodType.Count: obj = !compositeQuery.Methods.Any(i => { if (i.MethodType != QueryMethodType.Take) { return(i.MethodType == QueryMethodType.Skip); } return(true); }) ? processedResults.Count() : (object)results.Count(); break; case QueryMethodType.ElementAt: obj = !((ElementAtMethod)queryMethod).AllowDefaultValue ? processedResults.ElementAt(((ElementAtMethod)queryMethod).Index) : (object)processedResults.ElementAtOrDefault(((ElementAtMethod)queryMethod).Index); break; case QueryMethodType.First: obj = !((FirstMethod)queryMethod).AllowDefaultValue ? processedResults.First() : (object)processedResults.FirstOrDefault(); break; case QueryMethodType.Last: obj = !((LastMethod)queryMethod).AllowDefaultValue ? processedResults.Last() : (object)processedResults.LastOrDefault(); break; case QueryMethodType.Single: obj = !((SingleMethod)queryMethod).AllowDefaultValue ? processedResults.Single() : (object)processedResults.SingleOrDefault(); break; case QueryMethodType.GetResults: var searchHits = processedResults.GetSearchHits(); var facetResults = this.FormatFacetResults(processedResults.GetFacets(), compositeQuery.FacetQueries); obj = ReflectionUtility.CreateInstance(typeof(TResult), (object)searchHits, (object)processedResults.NumberFound, (object)facetResults); break; case QueryMethodType.GetFacets: obj = this.FormatFacetResults(processedResults.GetFacets(), compositeQuery.FacetQueries); break; default: throw new InvalidOperationException("Invalid query method"); } return((TResult)Convert.ChangeType(obj, typeof(TResult))); }
private static SolrQueryResults <Dictionary <string, object> > ApplySecurity(SolrQueryResults <Dictionary <string, object> > solrQueryResults, SearchSecurityOptions options, BaseCorePipelineManager pipeline, IAccessRight accessRight, ref int numberFound) { if (!options.HasFlag(SearchSecurityOptions.DisableSecurityCheck)) { var hashSet = new HashSet <Dictionary <string, object> >(); foreach (var dictionary in solrQueryResults.Where(searchResult => searchResult != null)) { object obj1; if (dictionary.TryGetValue("_uniqueid", out obj1)) { object obj2; dictionary.TryGetValue("_datasource", out obj2); var args = new OutboundIndexFilterArgs((string)obj1, (string)obj2) { AccessRight = accessRight.ItemRead() }; pipeline.Run("indexing.filterIndex.outbound", args); if (args.IsExcluded) { hashSet.Add(dictionary); numberFound = numberFound - 1; } } } foreach (var dictionary in hashSet) { solrQueryResults.Remove(dictionary); } } return(solrQueryResults); }
public Result NewsItems(string fullText, string since, int page, int itemsPerPage, string source) { int splitedItemsPerPage; if (itemsPerPage == 0) { itemsPerPage = 20; } if (source == "all" || source == null) { source = string.Empty; splitedItemsPerPage = itemsPerPage / 2; } else { splitedItemsPerPage = itemsPerPage; } DateTime sinceDate = DateTime.MinValue; if (!string.IsNullOrEmpty(since)) { sinceDate = Convert.ToDateTime(since); } Result result = new Result(); result.Articles = new List<Article>(); List<Article> articles = new List<Article>(); SolrNet.SolrQueryResults<ArchiveArticle> archiveArticles = new SolrQueryResults<ArchiveArticle>(); SolrNet.SolrQueryResults<SolrWISEdoc> wiseArticles = new SolrQueryResults<SolrWISEdoc>(); switch (source.ToLower()) { case "wise": wiseArticles = GetWiseSolr(fullText, sinceDate, page, itemsPerPage); break; case "s3": archiveArticles = GetArchiveSearchSolr(fullText, sinceDate, page, itemsPerPage); break; default: archiveArticles = GetArchiveSearchSolr(fullText, sinceDate, page, splitedItemsPerPage); wiseArticles = GetWiseSolr(fullText, sinceDate, page, splitedItemsPerPage); break; } foreach (ArchiveArticle aa in archiveArticles) { Article a = new Article(); a.AuthorName = aa.author; a.BodyText = aa.fulltext; a.HeadLine = aa.title; a.Id = aa.Id; a.Language = aa.language; a.PublicationDate = aa.pub_date; a.SourceName = aa.outlet_name; a.Topics = aa.topics != null ? aa.topics.ToList() : null; a.UrlAddress = aa.url; a.CreateDate = aa.created_date; a.ServiceName = "S3"; articles.Add(a); } foreach (SolrWISEdoc aa in wiseArticles) { Article a = new Article(); a.AuthorName = aa.AuthorName; a.BodyText = aa.BodyText; a.HeadLine = aa.HeadLine; a.Id = aa.Id; a.Language = aa.Language; a.PublicationDate = aa.PublicationDate; a.SourceName = aa.SourceName; a.Topics = aa.Topics != null ? aa.Topics.ToList() : null; a.UrlAddress = aa.UrlAddress; a.CreateDate = aa.PublicationDate; a.ServiceName = "WISE"; articles.Add(a); } result.Articles = articles.OrderByDescending(d => d.PublicationDate).Take(itemsPerPage).ToList(); result.ResultCount = archiveArticles.NumFound + wiseArticles.NumFound; return result; }
protected void AssertMatchesSolrInformationFromUrl() { // get id from url string url = Driver.Url; const string urlPattern = @".+\/(\d+)"; const string keyValuePattern = @"^value_.+_txts_\w{2}$"; const string associationsPattern = @"^(?:parents|related)_ss$"; Regex urlRegex = new Regex(urlPattern); Regex keyValyeRegex = new Regex(keyValuePattern); Regex associationsRegex = new Regex(associationsPattern); Match urlMatch = urlRegex.Match(url); if (urlMatch.Success && urlMatch.Groups.Count == 2) { Int64 id = Convert.ToInt64(urlMatch.Groups[1].Value); CatfishDbContext db = new CatfishDbContext(); CFEntity model = db.Entities.Find(id); Dictionary <string, object> result = model.ToSolrDictionary(); SolrQuery q = new SolrQuery($@"id:{model.MappedGuid}"); SolrQueryResults <Dictionary <string, object> > solrResults = SolrService.SolrOperations.Query(q); if (solrResults.Count == 1) { Dictionary <string, object> fromSolr = solrResults[0]; foreach (KeyValuePair <string, object> entry in result) { // first we need to make sure the entry value is not empty, // otherwise is not indexed in solr //if (entry.Value.ToString().Length > 0 && entry.Value != fromSolr[entry.Key]) //{ // return false; //} //if (entry.Value.ToString().Length > 0) //{ // // check if key has value pattern // Match keyValueMatch = keyValyeRegex.Match(entry.Key); // if (keyValueMatch.Success) // { // // compare as list of values // CollectionAssert.AreEqual((List<string>)entry.Value, // (System.Collections.ArrayList)fromSolr[entry.Key]); // } else // { // Assert.AreEqual(entry.Value, fromSolr[entry.Key]); // } //} Match keyValueMatch = keyValyeRegex.Match(entry.Key); Match associationsMatch = associationsRegex.Match(entry.Key); if (keyValueMatch.Success || associationsMatch.Success) { // treat as multi values List <string> test = (List <string>)entry.Value; if (test.Count > 0 && test[0] != "") { CollectionAssert.AreEqual(test, (System.Collections.ArrayList)fromSolr[entry.Key]); } //XXX Should it fail if the previous test is not passed? } else { // treat as regular values if (!string.IsNullOrEmpty(entry.Value.ToString())) { Assert.AreEqual(entry.Value, fromSolr[entry.Key]); } } } //return true; } } //return false; }
private string GetSpellCheckingResult(SolrQueryResults<Product> products) { return string.Join(" ", products.SpellChecking .Select(c => c.Suggestions.FirstOrDefault()) .Where(c => !string.IsNullOrEmpty(c)) .ToArray()); }
public void Parse(XDocument xml, SolrQueryResults <T> results) { throw new NotImplementedException(); }
public static bool HasRuledOutConstraints(SolrQueryResults<Product> results) { return results.FacetQueries.Any(query => query.Key.Contains(RuledOutTag)); }