private static void search(Guid applicationId, ref List <SearchDoc> retDocs, ref List <SearchDoc> toBeRemoved, Guid?currentUserId, ref int?lowerBoundary, int count, ref Query query, ref IndexSearcher searcher, string phrase, List <SearchDocType> docTypes, List <Guid> typeIds, List <string> types, bool additionalId, bool title, bool description, bool content, bool tags, bool fileContent, bool forceHasContent, bool highlight, ref int totalCount) { if (!lowerBoundary.HasValue) { lowerBoundary = 0; } int newBoundary = lowerBoundary.Value; List <SearchDoc> listDocs = new List <SearchDoc>(); if (RaaiVanSettings.Solr.Enabled) { listDocs = SolrAPI.search(applicationId, phrase, docTypes, typeIds, types, additionalId, title, description, tags, content, fileContent, forceHasContent, count, newBoundary, highlight, ref totalCount) .Select(d => SearchDoc.ToSearchDoc(d)).Where(d => d != null).ToList(); } else { if (query == null || searcher == null) { create_lucene_searcher(applicationId, docTypes, typeIds, types, additionalId, title, description, content, tags, fileContent, forceHasContent, phrase, ref query, ref searcher); if (query == null || searcher == null) { return; } } listDocs = lucene_search(applicationId, newBoundary, count, ref query, ref searcher, additionalId, title, description, content, tags, fileContent); } retDocs.AddRange(process_search_results(applicationId, listDocs, currentUserId, ref toBeRemoved, count)); newBoundary += listDocs.Count; if (lowerBoundary != newBoundary) { lowerBoundary = newBoundary; if (retDocs.Count < count) { search(applicationId, ref retDocs, ref toBeRemoved, currentUserId, ref lowerBoundary, count - retDocs.Count, ref query, ref searcher, phrase, docTypes, typeIds, types, additionalId, title, description, content, tags, fileContent, forceHasContent, highlight, ref totalCount); } } }
public static SearchDoc ToSearchDoc(Guid id, Guid?typeID, string content, string additionalID, bool deleted, string type, SearchDocType docType, string title = null, string tags = null, string description = null, string fileContect = null) { SearchDoc sd = new SearchDoc(); sd.ID = id; sd.Deleted = deleted; sd.TypeID = typeID; sd.Type = type; sd.AdditionalID = additionalID; sd.Title = title; sd.Description = description; sd.Tags = tags; sd.Content = content; sd.FileContent = fileContect; sd.SearchDocType = docType; return(sd); }
private static void _parse_search_docs(Guid applicationId, ref IDataReader reader, ref List <SearchDoc> _list, string itemType) { while (reader.Read()) { try { SearchDoc sd = new SearchDoc(); switch (itemType) { case "Node": if (!string.IsNullOrEmpty(reader["ID"].ToString())) { sd.ID = (Guid)reader["ID"]; } if (!string.IsNullOrEmpty(reader["Deleted"].ToString())) { sd.Deleted = (bool)reader["Deleted"]; } if (!string.IsNullOrEmpty(reader["TypeID"].ToString())) { sd.TypeID = (Guid)reader["TypeID"]; } if (!string.IsNullOrEmpty(reader["Type"].ToString())) { sd.Type = (string)reader["Type"]; } if (!string.IsNullOrEmpty(reader["AdditionalID"].ToString())) { sd.AdditionalID = (string)reader["AdditionalID"]; } if (!string.IsNullOrEmpty(reader["Title"].ToString())) { sd.Title = (string)reader["Title"]; } if (!string.IsNullOrEmpty(reader["Description"].ToString())) { sd.Description = (string)reader["Description"]; } if (!string.IsNullOrEmpty(reader["Tags"].ToString())) { sd.Tags = (string)reader["Tags"]; } if (!string.IsNullOrEmpty(reader["Content"].ToString())) { sd.Content = (string)reader["Content"]; } if (!string.IsNullOrEmpty(reader["FileContent"].ToString())) { sd.FileContent = (string)reader["FileContent"]; } sd.SearchDocType = SearchDocType.Node; break; case "NodeType": if (!string.IsNullOrEmpty(reader["ID"].ToString())) { sd.ID = (Guid)reader["ID"]; } if (!string.IsNullOrEmpty(reader["Deleted"].ToString())) { sd.Deleted = (bool)reader["Deleted"]; } if (!string.IsNullOrEmpty(reader["Title"].ToString())) { sd.Title = (string)reader["Title"]; } if (!string.IsNullOrEmpty(reader["Description"].ToString())) { sd.Description = (string)reader["Description"]; } sd.SearchDocType = SearchDocType.NodeType; break; case "Question": if (!string.IsNullOrEmpty(reader["ID"].ToString())) { sd.ID = (Guid)reader["ID"]; } if (!string.IsNullOrEmpty(reader["Deleted"].ToString())) { sd.Deleted = (bool)reader["Deleted"]; } if (!string.IsNullOrEmpty(reader["Title"].ToString())) { sd.Title = (string)reader["Title"]; } if (!string.IsNullOrEmpty(reader["Description"].ToString())) { sd.Description = (string)reader["Description"]; } if (!string.IsNullOrEmpty(reader["Content"].ToString())) { sd.Content = (string)reader["Content"]; } sd.SearchDocType = SearchDocType.Question; break; case "File": if (!string.IsNullOrEmpty(reader["ID"].ToString())) { sd.ID = (Guid)reader["ID"]; } if (!string.IsNullOrEmpty(reader["Deleted"].ToString())) { sd.Deleted = (bool)reader["Deleted"]; } if (!string.IsNullOrEmpty(reader["Type"].ToString())) { sd.Type = (string)reader["Type"]; } if (!string.IsNullOrEmpty(reader["Title"].ToString())) { sd.Title = (string)reader["Title"]; } if (!string.IsNullOrEmpty(reader["FileContent"].ToString())) { sd.FileContent = (string)reader["FileContent"]; } sd.SearchDocType = SearchDocType.File; break; case "User": if (!string.IsNullOrEmpty(reader["ID"].ToString())) { sd.ID = (Guid)reader["ID"]; } if (!string.IsNullOrEmpty(reader["Deleted"].ToString())) { sd.Deleted = (bool)reader["Deleted"]; } if (!string.IsNullOrEmpty(reader["AdditionalID"].ToString())) { sd.AdditionalID = (string)reader["AdditionalID"]; } if (!string.IsNullOrEmpty(reader["Title"].ToString())) { sd.Title = (string)reader["Title"]; } sd.SearchDocType = SearchDocType.User; break; } if (!string.IsNullOrEmpty(sd.Description)) { sd.Description = PublicMethods.markup2plaintext(applicationId, Expressions.replace(sd.Description, Expressions.Patterns.HTMLTag, " ")); } if (!string.IsNullOrEmpty(sd.Content)) { sd.Content = PublicMethods.markup2plaintext(applicationId, Expressions.replace(sd.Content, Expressions.Patterns.HTMLTag, " ")); } _list.Add(sd); } catch { }; } if (!reader.IsClosed) { reader.Close(); } }
private static List <SearchDoc> lucene_search(Guid applicationId, int lowerBoundary, int count, ref Query query, ref IndexSearcher searcher, bool additionalId, bool title, bool description, bool content, bool tags, bool fileContent) { try { List <SearchDoc> listDocs = new List <SearchDoc>(); TopDocs hits = searcher.Search(query, lowerBoundary + count + (count / 2)); FastVectorHighlighter fvHighlighter = new FastVectorHighlighter(true, true); for (int i = lowerBoundary, lnt = hits.ScoreDocs.Length; i < lnt; ++i) { ScoreDoc sd = hits.ScoreDocs[i]; string addIdFr = !additionalId ? string.Empty : fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query), searcher.IndexReader, docId: sd.Doc, fieldName: "AdditionalID", fragCharSize: 200); string titleFr = !title ? string.Empty : fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query), searcher.IndexReader, docId: sd.Doc, fieldName: "Title", fragCharSize: 200); string descFr = !description ? string.Empty : fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query), searcher.IndexReader, docId: sd.Doc, fieldName: "Description", fragCharSize: 200); string contentFr = !content ? string.Empty : fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query), searcher.IndexReader, docId: sd.Doc, fieldName: "Content", fragCharSize: 200); string tagsFr = !tags ? string.Empty : fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query), searcher.IndexReader, docId: sd.Doc, fieldName: "Tags", fragCharSize: 200); string fileFr = !fileContent ? string.Empty : fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query), searcher.IndexReader, docId: sd.Doc, fieldName: "FileContent", fragCharSize: 200); if (!string.IsNullOrEmpty(titleFr)) { titleFr = titleFr.Trim(); } if (!string.IsNullOrEmpty(addIdFr)) { addIdFr = addIdFr.Trim(); } string highlightedText = ((string.IsNullOrEmpty(descFr) ? string.Empty : descFr + " ") + (string.IsNullOrEmpty(contentFr) ? string.Empty : contentFr + " ") + (string.IsNullOrEmpty(tagsFr) ? string.Empty : tagsFr + " ") + (string.IsNullOrEmpty(fileFr) ? string.Empty : fileFr)).Trim(); if (string.IsNullOrEmpty(addIdFr) && string.IsNullOrEmpty(titleFr) && string.IsNullOrEmpty(highlightedText)) { break; } Document doc = searcher.Doc(sd.Doc); SearchDoc item = SearchDoc.ToSearchDoc(doc); item.Description = highlightedText; listDocs.Add(item); } return(listDocs); } catch (Exception ex) { LogController.save_error_log(applicationId, null, "SearchIndexDocuments", ex, ModuleIdentifier.SRCH); return(new List <SearchDoc>()); } }
private static List <SearchDoc> process_search_results(Guid applicationId, List <SearchDoc> listDocs, Guid?currentUserId, ref List <SearchDoc> toBeRemoved, int count) { List <DocFileInfo> files = new List <DocFileInfo>(); Dictionary <SearchDocType, List <Guid> > existingObjs = get_existing_ids(applicationId, listDocs, ref files); listDocs.Where(doc => files.Any(u => u.FileID == doc.ID)).ToList().ForEach(doc => { doc.FileInfo = files.Where(u => u.FileID == doc.ID).FirstOrDefault(); }); List <Guid> existingIds = new List <Guid>(); //Remove not existing docs foreach (SearchDoc sd in listDocs) { if (!existingObjs.Any(x => x.Value.Any(z => z == sd.ID))) { toBeRemoved.Add(sd); } } //end of Remove not existing docs List <Guid> granted = new List <Guid>(); //Check access to nodes List <Guid> nodeIdsToCheckAccess = new List <Guid>(); List <Guid> idsToCheckAccess = new List <Guid>(); if (existingObjs.ContainsKey(SearchDocType.Node)) { nodeIdsToCheckAccess.AddRange(existingObjs[SearchDocType.Node]); } if (existingObjs.ContainsKey(SearchDocType.File)) { existingObjs[SearchDocType.File].ForEach(f => { SearchDoc fl = listDocs.Where(x => x.ID == f && x.FileInfo != null && x.FileInfo.OwnerNodeID.HasValue).FirstOrDefault(); if (fl == null) { return; } if (!nodeIdsToCheckAccess.Any(a => a == fl.FileInfo.OwnerNodeID)) { nodeIdsToCheckAccess.Add(fl.FileInfo.OwnerNodeID.Value); } if (fl.FileInfo.OwnerID.HasValue && fl.FileInfo.OwnerID != fl.FileInfo.OwnerNodeID) { if (!idsToCheckAccess.Any(a => a == fl.FileInfo.OwnerID)) { idsToCheckAccess.Add(fl.FileInfo.OwnerID.Value); } } if (fl.FileInfo.OwnerID.HasValue && fl.FileInfo.OwnerID != fl.FileInfo.OwnerNodeID && !idsToCheckAccess.Any(a => a == fl.FileInfo.OwnerID)) { idsToCheckAccess.Add(fl.FileInfo.OwnerID.Value); } }); } List <PermissionType> pts = new List <PermissionType>(); pts.Add(PermissionType.View); pts.Add(PermissionType.ViewAbstract); pts.Add(PermissionType.Download); Dictionary <Guid, List <PermissionType> > ps = PrivacyController.check_access(applicationId, currentUserId, nodeIdsToCheckAccess, PrivacyObjectType.Node, pts); granted.AddRange(ps.Keys.Where( k => ps[k].Any(p => p == PermissionType.ViewAbstract || p == PermissionType.View))); List <Guid> grantedFileOwners = PrivacyController.check_access(applicationId, currentUserId, idsToCheckAccess, PrivacyObjectType.None, PermissionType.View); listDocs.Where(d => d.SearchDocType == SearchDocType.File && d.FileInfo != null && d.FileInfo.OwnerNodeID.HasValue).ToList().ForEach(doc => { Guid ndId = doc.FileInfo.OwnerNodeID.Value; bool isGranted = ps.ContainsKey(ndId) && ps[ndId].Any(u => u == PermissionType.View) && ps[ndId].Any(u => u == PermissionType.Download); if (isGranted && doc.FileInfo.OwnerID.HasValue && doc.FileInfo.OwnerID != ndId && !grantedFileOwners.Any(o => o == doc.FileInfo.OwnerID)) { isGranted = false; } doc.AccessIsDenied = !isGranted; }); //end of Check access to nodes //Check access to other objects List <Guid> ids = new List <Guid>(); existingObjs.Keys.Where(x => x != SearchDocType.Node).ToList() .ForEach(u => ids.AddRange(existingObjs[u])); granted.AddRange(PrivacyController.check_access(applicationId, currentUserId, ids, PrivacyObjectType.None, PermissionType.View)); //end of Check access to other objects //Check permissions bool forceCheckPermission = RaaiVanSettings.IndexUpdate.CheckPermissions(applicationId); existingObjs.Keys.ToList().ForEach(k => { existingObjs[k].ForEach(id => { SearchDoc doc = listDocs.Where(d => d.ID == id).FirstOrDefault(); if (doc == null) { return; } bool isGranted = doc.AccessIsDenied.HasValue && doc.AccessIsDenied.Value ? false : granted.Any(x => x == id); if (!isGranted) { doc.AccessIsDenied = true; } if (isGranted || !forceCheckPermission) { existingIds.Add(id); } }); }); //end of Check permissions return(listDocs.Where(doc => existingIds.Any(x => x == doc.ID)) .Take(Math.Min(count, listDocs.Count)).ToList()); }
public static SearchDoc ToSearchDoc(Document doc) { SearchDoc retSD = new SearchDoc(); switch (doc.GetField("SearchDocType").StringValue) { case "Node": if (doc.GetField("ID") != null) { retSD.ID = Guid.Parse(doc.GetField("ID").StringValue); } if (doc.GetField("Deleted") != null) { retSD.Deleted = Convert.ToBoolean(doc.GetField("Deleted").StringValue); } if (doc.GetField("TypeID") != null) { retSD.TypeID = Guid.Parse(doc.GetField("TypeID").StringValue); } if (doc.GetField("Type") != null) { retSD.Type = doc.GetField("Type").StringValue; } if (doc.GetField("AdditionalID") != null) { retSD.AdditionalID = doc.GetField("AdditionalID").StringValue; } if (doc.GetField("Title") != null) { retSD.Title = doc.GetField("Title").StringValue; } if (doc.GetField("Description") != null) { retSD.Description = doc.GetField("Description").StringValue; } if (doc.GetField("Tags") != null) { retSD.Tags = doc.GetField("Tags").StringValue; } if (doc.GetField("Content") != null) { retSD.Content = doc.GetField("Content").StringValue; } if (doc.GetField("FileContent") != null) { retSD.FileContent = doc.GetField("FileContent").StringValue; } retSD.SearchDocType = SearchDocType.Node; break; case "NodeType": if (doc.GetField("ID") != null) { retSD.ID = Guid.Parse(doc.GetField("ID").StringValue); } if (doc.GetField("Deleted") != null) { retSD.Deleted = Convert.ToBoolean(doc.GetField("Deleted").StringValue); } if (doc.GetField("Title") != null) { retSD.Title = doc.GetField("Title").StringValue; } if (doc.GetField("Description") != null) { retSD.Description = doc.GetField("Description").StringValue; } retSD.SearchDocType = SearchDocType.NodeType; break; case "Question": if (doc.GetField("ID") != null) { retSD.ID = Guid.Parse(doc.GetField("ID").StringValue); } if (doc.GetField("Deleted") != null) { retSD.Deleted = Convert.ToBoolean(doc.GetField("Deleted").StringValue); } if (doc.GetField("Title") != null) { retSD.Title = doc.GetField("Title").StringValue; } if (doc.GetField("Description") != null) { retSD.Description = doc.GetField("Description").StringValue; } if (doc.GetField("Content") != null) { retSD.Content = doc.GetField("Content").StringValue; } retSD.SearchDocType = SearchDocType.Question; break; case "File": if (doc.GetField("ID") != null) { retSD.ID = Guid.Parse(doc.GetField("ID").StringValue); } if (doc.GetField("Type") != null) { retSD.Type = doc.GetField("Type").StringValue; } if (doc.GetField("Title") != null) { retSD.Title = doc.GetField("Title").StringValue; } if (doc.GetField("FileContent") != null) { retSD.FileContent = doc.GetField("FileContent").StringValue; } retSD.SearchDocType = SearchDocType.File; break; case "User": if (doc.GetField("ID") != null) { retSD.ID = Guid.Parse(doc.GetField("ID").StringValue); } if (doc.GetField("Deleted") != null) { retSD.Deleted = Convert.ToBoolean(doc.GetField("Deleted").StringValue); } if (doc.GetField("AdditionalID") != null) { retSD.AdditionalID = doc.GetField("AdditionalID").StringValue; } if (doc.GetField("Title") != null) { retSD.Title = doc.GetField("Title").StringValue; } retSD.SearchDocType = SearchDocType.User; break; } return(retSD); }