public static List <SearchVm2> Query(string keyWord, int rowNum, int count) { //定义solr //ISolrOperations<SearchVm2> solr= ServiceLocator.Current.GetInstance<ISolrOperations<SearchVm2>>(); //建立排序,条件. QueryOptions options = new QueryOptions(); options.Rows = count; //数据条数 options.Start = 1; //开始项 //创建查询条件 var qTB = new SolrQueryByField("text", keyWord); //创建条件集合 List <ISolrQuery> query = new List <ISolrQuery>(); //添加条件 query.Add(qTB); //按照时间倒排序. options.AddOrder(new SortOrder("CreateTime", Order.DESC)); //条件集合之间的关系 var qTBO = new SolrMultipleCriteriaQuery(query, "OR"); //执行查询,有5个重载 //SolrQueryResults<SearchVm2> results = solr.Query(qTBO,options); return(null);//results; }
public void QueryByField() { var q = new SolrQueryByField("desc", "samsung"); var notq = new SolrNotQuery(q); Assert.AreEqual("-desc:(samsung)", Serialize(notq)); }
public IActionResult Update(int id, Posts model, string Solr) { if (ModelState.IsValid) { var b = _db.Posts.Where(b => b.PostID == id).FirstOrDefault(); b.PostContent = model.PostContent; b.PostExcerpt = model.PostExcerpt; b.PostTitle = model.PostTitle; b.PostAuthor = model.PostAuthor; b.PostLocation = model.PostLocation; b.CategoryID = model.CategoryID; _db.Update(b); _db.SaveChanges(); if (Solr == "Yes") { var s = new PersonalWebsiteMVC.Models.SolrModel(); s.ID = model.PostID.ToString(); s.Title = model.PostTitle; s.Url = "http://www.douglasmcgregor.co.uk/Blog?q=" + model.PostID; s.Body = model.PostContent; _solr.Add(s); _solr.Commit(); } if (Solr == "No") { SolrQueryByField results = new SolrQueryByField("ID", model.PostID.ToString()); _solr.Delete(results.FieldValue); _solr.Commit(); } return(RedirectToAction("Index")); } return(View(model)); }
public void Concat_different_types() { var q1 = new SolrQuery("1"); var q2 = new SolrQueryByField("f", "v"); var qm = new SolrMultipleCriteriaQuery(new ISolrQuery[] {q1, q2}); Console.WriteLine(Serialize(qm)); Assert.AreEqual("(1 f:(v))", Serialize(qm)); }
/// <summary> /// This method removes all documents of the given type from a specific Solr Core /// </summary> /// <returns>IndexResponse</returns> public IndexResponse RemoveAllDocumentsOfType(string content_type) { GenericStorageExtensionLogger.WriteLog(ELogLevel.INFO, "Entering SolrIndexManager.RemoveAllDocumentsOfType, Content Type: " + content_type); IndexResponse response = new IndexResponse(); OperationResult result = OperationResult.Failure; try { AbstractSolrQuery query = new SolrQueryByField(GenericStorageExtensionServicesConstants.ContentType, content_type); RemoveFromSOLR(query); CommitToSOLR(); result = OperationResult.Success; } catch (Exception ex) { string logString = GenericStorageExtensionServiceConstants.LOG_MESSAGE + Environment.NewLine; logString = string.Concat(logString, string.Format("{0}{1}", ex.Message, ex.StackTrace)); GenericStorageExtensionLogger.WriteLog(ELogLevel.ERROR, logString); result = OperationResult.Failure; } response.Result = (int)result; GenericStorageExtensionLogger.WriteLog(ELogLevel.INFO, "Exiting SolrIndexManager.RemoveAllDocumentsOfType, Result: " + result.ToString()); return(response); }
public void QueryByField() { var q = new SolrQueryByField("desc", "samsung"); var requiredq = new SolrRequiredQuery(q); Assert.AreEqual("+desc:(samsung)", Serialize(requiredq)); }
/// <summary> /// 分页查询 /// </summary> /// <param name="startPage">页码</param> /// <param name="pageSize">单页数据数</param> /// <param name="rowCount">行数</param> /// <param name="where">条件</param> /// <param name="order">排序</param> /// <returns></returns> public List <TEntity> LoadPageList(int startPage, int pageSize, out int rowCount, ICollection <KeyValuePair <string, string> > wheres = null, ICollection <KeyValuePair <string, string> > orders = null) { QueryOptions options = new QueryOptions(); //分页参数 options.Rows = pageSize; //数据条数 options.Start = startPage; //开始项 // 拼接相关查询条件 SolrQueryByField[] conds = new SolrQueryByField[wheres.Count]; int i = 0; foreach (var param in wheres) { if (!string.IsNullOrWhiteSpace(param.Key)) { conds[i] = new SolrQueryByField(param.Key, param.Value); } i++; } options.AddFilterQueries(conds); //执行查询 var results = _solrOperations.Query(SolrQuery.All, options); // 得到返回的数据总条数和total和 总页数 用于分页显示, rowCount = results.NumFound; return(results); }
/// <summary> /// 根据lambda表达式条件获取实体集合 /// </summary> /// <param name="predicate">lambda表达式条件</param> /// <returns></returns> public List <TEntity> GetAllList(ICollection <KeyValuePair <string, string> > wheres) { if (wheres.Count > 1) { SolrQueryByField[] conds = new SolrQueryByField[wheres.Count]; int i = 0; foreach (var param in wheres) { if (!string.IsNullOrWhiteSpace(param.Key)) { conds[i] = new SolrQueryByField(param.Key, param.Value); } i++; } QueryOptions qo = new QueryOptions(); qo.AddFilterQueries(conds); return(_solrOperations.Query(SolrQuery.All, qo)); } else { SolrQueryByField qf = null; foreach (var param in wheres) { qf = new SolrQueryByField(param.Key, param.Value); } return(_solrOperations.Query(qf)); } }
public void OperatorPlus() { var p = new LocalParams { {"type", "spatial"}, }; var q = new SolrQueryByField("field", "value"); var qq = p + q; Assert.AreEqual("{!type=spatial}field:(value)", SerializeQuery(qq)); }
public void Concat_different_types() { var q1 = new SolrQuery("1"); var q2 = new SolrQueryByField("f", "v"); var qm = new SolrMultipleCriteriaQuery(new ISolrQuery[] { q1, q2 }); Console.WriteLine(Serialize(qm)); Assert.AreEqual("(1 f:v)", Serialize(qm)); }
public void QuotedFalse() { var q = new SolrQueryByField("id", "hello?wor/ld*") { Quoted = false }; Assert.AreEqual("id:(hello?wor/ld*)", Serialize(q)); }
public void Concat_different_types() { var q1 = new SolrQuery("1"); var q2 = new SolrQueryByField("f", "v"); var qm = new SolrMultipleCriteriaQuery(new ISolrQuery[] { q1, q2 }); testOutputHelper.WriteLine(Serialize(qm)); Assert.Equal("(1 f:(v))", Serialize(qm)); }
public void OperatorPlus_multiple_queries() { var p = new LocalParams { {"type", "spatial"}, }; var q = new SolrQueryByField("field", "value"); var q2 = new SolrQueryByRange<decimal>("price", 100m, 200m); var qq = p + (q + q2); Assert.AreEqual("{!type=spatial}(field:(value) price:[100 TO 200])", SerializeQuery(qq)); }
public void OperatorPlus() { var p = new LocalParams { { "type", "spatial" }, }; var q = new SolrQueryByField("field", "value"); var qq = p + q; Assert.AreEqual("{!type=spatial}field:(value)", SerializeQuery(qq)); }
protected virtual AbstractSolrQuery VisitInsidePolygon(InsidePolygonNode node, AbstractSolrQuery query) { AbstractSolrQuery abstractSolrQuery = new SolrQueryByField(node.Field, $"Intersects({node.Coordinate.Latitude} {node.Coordinate.Longitude})"); if (!abstractSolrQuery) { return(abstractSolrQuery & query); } return(abstractSolrQuery); }
protected virtual AbstractSolrQuery VisitInsidePolygon(InsidePolygonNode node, SolrQueryMapperState state) { AbstractSolrQuery abstractSolrQuery = new SolrQueryByField(node.Field, $"Intersects({node.Coordinate.Latitude} {node.Coordinate.Longitude})"); if (!abstractSolrQuery) { return(abstractSolrQuery & this.Visit(node.SourceNode, state)); } return(abstractSolrQuery); }
public void OperatorPlus_multiple_queries() { var p = new LocalParams { { "type", "spatial" }, }; var q = new SolrQueryByField("field", "value"); var q2 = new SolrQueryByRange <decimal>("price", 100m, 200m); var qq = p + (q + q2); Assert.AreEqual("{!type=spatial}(field:(value) price:[100 TO 200])", SerializeQuery(qq)); }
public IActionResult Delete() { var options = new QueryOptions(); options.ExtraParams = new KeyValuePair <string, string>[] { //new KeyValuePair<string, string>("wt", "xml"); }; string id = HttpContext.Request.Query["q"]; SolrQueryByField results = new SolrQueryByField("ID", id); solr.Delete(results.FieldValue); solr.Commit(); return(RedirectToAction("Index")); }
/// <summary> /// Removes all content for the given Publication ID /// </summary> /// <param name="publicationID">Publication ID</param> private void RemoveALLFromSOLR(int publicationID) { GenericStorageExtensionLogger.WriteLog(ELogLevel.DEBUG, "Entering SolrIndexManager.RemoveALLFromSOLR for Publication ID: " + publicationID.ToString()); string publication = "tcm:{0}-*"; publication = string.Format(publication, publicationID.ToString()); AbstractSolrQuery query = new SolrQueryByField(GenericStorageExtensionServicesConstants.TCM_URI, publication); RemoveFromSOLR(query); GenericStorageExtensionLogger.WriteLog(ELogLevel.DEBUG, "Exiting SolrIndexManager.RemoveALLFromSOLR"); }
/// <summary> /// 根据lambda表达式条件获取单个实体 /// </summary> /// <param name="predicate">lambda表达式条件</param> /// <returns></returns> public TEntity FirstOrDefault(ICollection <KeyValuePair <string, string> > wheres) { if (wheres.Count > 1) { SolrQueryByField[] conds = new SolrQueryByField[wheres.Count]; int i = 0; foreach (var param in wheres) { if (!string.IsNullOrWhiteSpace(param.Key)) { conds[i] = new SolrQueryByField(param.Key, param.Value); } i++; } QueryOptions qo = new QueryOptions(); qo.AddFilterQueries(conds); var res = _solrOperations.Query(SolrQuery.All, qo); if (res?.Count > 0) { return(res[0]); } else { return(null); } } else { SolrQueryByField qf = null; foreach (var param in wheres) { qf = new SolrQueryByField(param.Key, param.Value); } var res = _solrOperations.Query(qf); if (res?.Count > 0) { return(res[0]); } else { return(null); } } }
/// <summary> /// 根据条件删除实体 /// </summary> /// <param name="where">lambda表达式</param> /// <param name="autoSave">是否自动保存</param> public void Delete(ICollection <KeyValuePair <string, string> > wheres, bool autoSave = true) { //_solrOperations.Set<TEntity>().RemoveRange(_solrOperations.Set<TEntity>().Where(where).ToList()); if (wheres.Count > 1) { SolrQueryByField[] conds = new SolrQueryByField[wheres.Count]; int i = 0; foreach (var param in wheres) { if (string.IsNullOrWhiteSpace(param.Key)) { conds[i] = new SolrQueryByField(param.Key, param.Value); } } QueryOptions qo = new QueryOptions(); qo.AddFilterQueries(conds); var res = _solrOperations.Query(SolrQuery.All, qo); if (res?.Count > 0) { _solrOperations.Delete(res); if (autoSave) { Save(); } } } else { SolrQueryByField qf = null; foreach (var param in wheres) { qf = new SolrQueryByField(param.Key, param.Value); } if (qf != null) { _solrOperations.Query(qf); if (autoSave) { Save(); } } } }
public void Fun() { Startup.Init <Article>("http://localhost:8983/solr/test"); ISolrOperations <Article> solr = ServiceLocator.Current.GetInstance <ISolrOperations <Article> >(); //添加 Article t = new Article { id = 3, title = "mm", }; var res = solr.Add(t); bool success = res.Status == 0; //查询 var qTB = new SolrQueryByField("id", "1"); // var results = solr.Query(qTB); //删除 // var result=solr.Delete(qTB); Console.WriteLine("end"); }
public async Task <List <BasicSongSearchResult> > Search(string input) { if (string.IsNullOrWhiteSpace(input)) { return(null); } var terms = GetStringFuzzySearchTerms(input); AbstractSolrQuery query = null; foreach (var term in terms) { if (query == null) { query = new SolrQueryByField(SolrSearchConstants.SongName, term) { Quoted = false }; } else { query = query || new SolrQueryByField(SolrSearchConstants.SongName, term) { Quoted = false }; } query = query || new SolrQueryByField(SolrSearchConstants.ArtistName, term) { Quoted = false }; } var result = await _songSearchOperations.QueryAsync(query, new QueryOptions { Rows = 50 }); var resultList = result.ToList(); var basicSongSearchResults = _getSongsFromSearchResultsQuery.Get(resultList); return(basicSongSearchResults); }
private void AddFilterQuery(List <ISolrQuery> filters, IEnumerable <string> filterValues, string solrFieldName, string defaultOperator = "AND", bool quoted = false, bool isRange = false) { if (!filterValues.Any()) { return; } var list = new List <SolrQueryByField>(); foreach (var filterValue in filterValues) { var normalizedFilterValue = isRange ? filterValue : filterValue.RemoveSpecialCharacters(); if (!string.IsNullOrWhiteSpace(normalizedFilterValue)) { var queryByField = new SolrQueryByField(solrFieldName, normalizedFilterValue); queryByField.Quoted = quoted;// || filterValues.ToList().Any(c => c.Contains(" ")); list.Add(queryByField); } } if (list.Any()) { filters.Add(new SolrMultipleCriteriaQuery(list, defaultOperator)); } }
public static SolrQueryResults <Blog> Query(string keyword, int?page, out int pageCount) { int pageNumber = (page ?? 1); int pageSize = 20; var solr = ServiceLocator.Current.GetInstance <ISolrOperations <Blog> >(); AbstractSolrQuery query = new SolrQueryByField("titlecontent", keyword); QueryOptions options = new QueryOptions { Start = (pageNumber - 1) * pageSize, Rows = pageSize, Highlight = new HighlightingParameters { Fields = new[] { "title", "content" }, BeforeTerm = "<em>", AfterTerm = "</em>" }, OrderBy = new [] { new SortOrder("score", Order.DESC) } }; var result = solr.Query(query, options); foreach (var b in result) { foreach (var h in result.Highlights[b.Id.ToString()]) { if (h.Key == "title") { b.Title = string.Join(",", h.Value.ToArray()); } if (h.Key == "content") { b.Content = string.Join(", ", h.Value.ToArray()); } } } pageCount = (int)Math.Ceiling((float)result.NumFound / pageSize); return(result); }
/// <summary> /// This method removes an index from Solr /// </summary> /// <param name="query">IndexRequest containing delete criteria</param> /// <returns>IndexResponse indicating success or failure</returns> public IndexResponse RemoveDocument(IndexRequest query) { GenericStorageExtensionLogger.WriteLog(ELogLevel.INFO, "Entering SolrIndexManager.RemoveDocument for TCM URI: " + query.ItemURI); IndexResponse response = new IndexResponse(); OperationResult result = OperationResult.Failure; try { // To Remove element and child elements from the SOLR //AbstractSolrQuery deleteQuery = new SolrQueryByField(SolrServicesConstants.BaseParent, query.ItemURI); AbstractSolrQuery deleteQuery = new SolrQueryByField(GenericStorageExtensionServicesConstants.TCM_URI, query.ItemURI); RemoveFromSOLR(deleteQuery); CommitToSOLR(); result = OperationResult.Success; } catch (Exception ex) { string logString = GenericStorageExtensionServiceConstants.LOG_MESSAGE + Environment.NewLine; logString = string.Concat(logString, string.Format("Item URI : {0}", query.ItemURI), Environment.NewLine, string.Format("{0}{1}", ex.Message, ex.StackTrace)); GenericStorageExtensionLogger.WriteLog(ELogLevel.ERROR, logString); result = OperationResult.Failure; } response.Result = (int)result; GenericStorageExtensionLogger.WriteLog(ELogLevel.INFO, "Exiting SolrIndexManager.RemoveDocument, Result: " + result.ToString()); return(response); }
public void ShouldQuoteSpaces() { var q = new SolrQueryByField("id", "hello world"); Assert.AreEqual("id:\"hello world\"", q.Query); }
public void ShouldQuoteSpaces() { var q = new SolrQueryByField("id", "hello world"); Assert.AreEqual("id:(\"hello world\")", Serialize(q)); }
public void NullField_yields_null_query() { var q = new SolrQueryByField(null, "123456"); Assert.IsNull(Serialize(q)); }
public void NullValue_yields_null_query() { var q = new SolrQueryByField("id", null); Assert.IsNull(Serialize(q)); }
public void Basic() { var q = new SolrQueryByField("id", "123456"); Assert.AreEqual("id:(123456)", Serialize(q)); }
public void EmptyValue() { var q = new SolrQueryByField("id", ""); Assert.AreEqual("id:(\"\")", Serialize(q)); }
public void Basic() { var q = new SolrQueryByField("id", "123456"); Assert.AreEqual("id:123456", q.Query); }
public void Solr4RegexEscape() { var q = new SolrQueryByField("id", "/value/a"); Assert.AreEqual("id:(\"/value/a\")", Serialize(q)); }
public void ShouldQuoteSpecialChar() { var q = new SolrQueryByField("id", "hello+world-bye&&q||w!e(r)t{y}[u]^i\"o~p:a\\s+d;;?*/"); Assert.AreEqual(@"id:(hello\+world\-bye\&&q\||w\!e\(r\)t\{y\}\[u\]\^i\""o\~p\:a\\s\+d\;\;\?\*\/)", Serialize(q)); }
public void ShouldNotQuoteWildcard() { var q = new SolrQueryByField("id", "h?llo*"); Assert.AreEqual("id:h?llo*", Serialize(q)); }
public void QueryByField() { var q = new SolrQueryByField("desc", "samsung"); var notq = new SolrNotQuery(q); Assert.AreEqual("-desc:samsung", Serialize(notq)); }
public void FieldNameWithSpaces() { var q = new SolrQueryByField("field with spaces", "hello"); Assert.AreEqual(@"field\ with\ spaces:(hello)", Serialize(q)); }
//private ISolrq private void RunQuery(string query) { ISolrOperations <SolrDocument> solr = ServiceLocator.Current.GetInstance <ISolrOperations <SolrDocument> >(); var q = new SolrQueryByField("text", query); var results = solr.Query(q); }