I am applying search functionality in my system using Sitecore 7.0 (actually I am converting my code from Sitecore 6.5 to sitecore 7.0). When I try to get index using Sitecore.Search.SearchManager.GetIndex method, I find configuration property with null value.
My sample code in 6.5 is as below
webDb = Sitecore.Context.Database;
Sitecore.Data.Indexing.Index indx = webDb.Indexes["system"]; //Getting warning - deprecated method
Item bucketItem = Sitecore.Context.Item;
IndexSearcher indexSearcher = indx.GetSearcher(webDb);
topDocs = GetContent(keyWords, webDb, indexSearcher, year, regionName);
if (topDocs != null)
{
int totalMatchItemCount = topDocs.TotalHits;
if (totalMatchItemCount > 0)
{
returnValues = new Item[totalMatchItemCount];
int i = 0;
foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs)
{
Document doc = indexSearcher.Doc(scoreDoc.Doc);
Item item = Index.GetItem(doc, webDb);//Getting warning - deprecated method
returnValues[i++] = item;
}
}
}
Its works fine but gives error of deprecated method on below lines,
Sitecore.Data.Indexing.Index indx = webDb.Indexes["system"];
and
Item item = Index.GetItem(doc, webDb);
My converted code of Sitecore 7.0 is as below,
var children = new List<Item>();
Sitecore.Search.Index searchIndx = Sitecore.Search.SearchManager.GetIndex("system");//Shows SearchManager._Configuration with NULL value, hence all methods and property getting with exception.
using (var searchContext = searchIndx.CreateSearchContext())
{
var ftQuery = new Sitecore.Search.FullTextQuery(keyWords);
var hits = searchContext.Search(ftQuery);
var results = hits.FetchResults(0, hits.Length);
foreach (Sitecore.Search.SearchResult result in results)
{
//My stuff
}
}
When I am trying to fetch value using Sitecore 7.0, get the below exception
Could not create instance of type: Lucene.Net.Analysis.Standard.StandardAnalyzer. No matching constructor was found.
Thanks.
I have a feeling this is because you're still using the pre-Sitecore 7 Sitecore.Search API rather than the newer Sitecore.ContentSearch.
Try this for some further help: Searching with the new Sitecore 7 API
Use below code to get index in Sitecore 7.
// Index
public static string IndexName
{
get
{
return (Sitecore.Context.Database.Name.ToLower()) == "master" ? "sitecore_master_index" : "sitecore_web_index";
}
}
public static ISearchIndex _index;
public static ISearchIndex Index
{
get
{
if (_index == null) { _index = ContentSearchManager.GetIndex(IndexName); }
return _index;
}
}
Could not create instance of type:
Lucene.Net.Analysis.Standard.StandardAnalyzer. No matching constructor
was found.
I've seen this error occur when there are DLLs from multiple Sitecore versions in your /bin folder, so I'd agree with #TwentyGotoTen: it sounds like you either missed out a step in the upgrade process, or you need to check your deploy process to make sure your solution isn't referencing older versions of Sitecore assemblies.
Related
Not sure if anyone has come across this, but currently porting a K13 MVC project over to K13 .Net core (5.0) - both using the same CMS just different Kentico libraries packages but of same hotfix version.
standard Search API functionality being used in both with exact same parameters for a search query but finding that .net core instance will return a TreeNode in its searchResults.Items Data object where the MVC would return its stronglyTyped object
Function
protected ItemsAndCount<News> GetItemsAndCount(string nodeAliasPath, int pageSize = 12, string searchText = "", int currentPage = 0, int category = 0, int year = 0)
{
var culture = LocalizationContext.CurrentCulture.CultureCode;
var defaultCulture = CultureHelper.GetDefaultCultureCode(SiteContext.CurrentSiteName);
var combineWithDefaultCulture = SiteInfoProvider.CombineWithDefaultCulture(SiteContext.CurrentSiteName);
var documentTypes = News.CLASS_NAME;
var docCondition = new DocumentSearchCondition(documentTypes, culture, defaultCulture, combineWithDefaultCulture);
var condition = new SearchCondition(GetExtraConditions(category, year), SearchModeEnum.ExactPhrase, SearchOptionsEnum.NoneSearch, docCondition, false);
var query = ValidationHelper.GetString(searchText, string.Empty);
var path = nodeAliasPath + "/";
var parameters = new SearchParameters
{
Path = path,
SearchFor = SearchSyntaxHelper.CombineSearchCondition(query, condition),
SearchSort = OrderBy,
CurrentCulture = LocalizationContext.CurrentCulture.CultureCode,
DefaultCulture = CultureHelper.GetDefaultCultureCode(SiteContext.CurrentSiteName),
CombineWithDefaultCulture = SiteInfoProvider.CombineWithDefaultCulture(SiteContext.CurrentSiteName),
User = MembershipContext.AuthenticatedUser,
SearchIndexes = SearchIndexHelper.SEARCHINDEX_NEWS,
DisplayResults = pageSize,
StartingPosition = currentPage == 0 ? 0 : (currentPage - 1) * pageSize,
NumberOfProcessedResults = 10000
};
var searchResult = SearchHelper.Search(parameters);
//TotalNoOfRecords = searchResult.TotalNumberOfResults;
return new ItemsAndCount<News>
{
Items = GetItems(searchResult.Items).ToList(),
Count = searchResult.TotalNumberOfResults
};
}
So according to the API docs for SearchHelper, this does look like intended behavior. But you don't actually have to use SearchHelper. You could call the Azure Search client directly and work with the SearchDocument object it returns. An example of this is in the LearningKit for 13.
So this turned out to be an issue due to having all generated code files in a separate library
https://docs.xperience.io/custom-development/adding-custom-assemblies#Addingcustomassemblies-Enablingclassdiscovery
what was throwing me is that all the rest of the site was actually running as expected without this, only searchitems & customtableitems where not returning as strongly typed.
I have to register a plugin on Quick Find search on "Artilce" entity. When user enter any thing in quick find text box on Article entity at that time my plugin execute and return filter the data based on our business logic.
1.What event is fired when we find using quick find.
2.What message passes when this event is fired.
I have tried registering the plugin on RetrieveMultiple message but this is not triggered when we click on search in quick find.
Please help.
We have a Plugin registered on the RetrieveMultiple. We had a business requirement to search for the records, using WildCard by default.
Plugin Registration Details:
Message: RetrieveMultiple
Primary Entity:None
Secondary Entity:None
Pre-Operation
Code:
public const String QueryLiteral = "Query";
public const String LIKE = "%";
public void Execute(IServiceProvider serviceProvider)
{
String ParentEntity = String.Empty;
String OriginalSearch = String.Empty;
// Obtain the execution context from the service provider.
var ContextInstance = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Get a reference to the Organization service.
IOrganizationService ServiceInstance =
((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).
CreateOrganizationService(ContextInstance.InitiatingUserId);
// Critical Point here - NOTICE that the InputParameters Contains the word Query
if (ContextInstance.Depth < 2 && ContextInstance.InputParameters.Contains(QueryLiteral) &&
ContextInstance.InputParameters[QueryLiteral] is QueryExpression)
{
QueryExpression QueryPointer = (ContextInstance.InputParameters[QueryLiteral] as QueryExpression);
//Verify the conversion worked as expected - if not, everything else is useless
if (null != QueryPointer)
{
// Check if the request is coming from any Search View
// We know this b/c Criteria isn't null and the Filters Count > 1
if (null != QueryPointer.Criteria && QueryPointer.Criteria.Filters.Count > 1)
{
ParentEntity = ContextInstance.PrimaryEntityName;
OriginalSearch = QueryPointer.Criteria.Filters[1].Conditions[0].Values[0].ToString();
OriginalSearch = String.Format(CultureInfo.CurrentCulture,
"{0}{1}", LIKE, OriginalSearch);
}
ConditionExpression NewCondition = null;
FilterExpression NewFilter = null;
if (null != QueryPointer.Criteria)
{
//Change the default 'BeginsWith'Operator to 'Contains/Like' operator in the basic search query
foreach (FilterExpression FilterSet in QueryPointer.Criteria.Filters)
{
foreach (ConditionExpression ConditionSet in FilterSet.Conditions)
{
if (ConditionSet.Operator == ConditionOperator.Like)
{
if (OriginalSearch != "")
ConditionSet.Values[0] = OriginalSearch;
else
{
OriginalSearch = QueryPointer.Criteria.Filters[0].Conditions[0].Values[0].ToString();
OriginalSearch = String.Format(CultureInfo.CurrentCulture,
"{0}{1}", LIKE, OriginalSearch);
ConditionSet.Values[0] = OriginalSearch;
}
}
}
}
}
}
ContextInstance.InputParameters[QueryLiteral] = QueryPointer;
}
}
Check details on this Post: http://www.williamgryan.mobi/?p=596
We have raised a ticket with Microsoft to address this situaation.
The solution they provided was to modify the Database to make the message
SearchByTitleKbArticleRequest available in the plugin registration tool.
I currently dont remember the table that we updated a flag against these messages.
After updating the table we were able to register the plugin against the message
SearchByTitleKbArticleRequest
Then the plugin triggered and we modified the entity collection returned from there.
I'm successfully creating a Sharepoint List (named "XMLToPDFTestList"), which I can see via Site Actions > View All Site Content, but my attempts to add columns to the list has so far proven fruitless.
Here is how I'm trying to do it:
private void ProvisionallyCreateList()
{
SPWeb mySite = SPContext.Current.Web;
// Check to see if list already exists; if so, exit
if (mySite.Lists.TryGetList(listTitle) != null) return;
SPListCollection lists = mySite.Lists;
SPListTemplateType listTemplateType = new SPListTemplateType();
listTemplateType = SPListTemplateType.GenericList;
string listDescription = "This list is to hold inputted vals";
lists.Add(listTitle, listDescription, listTemplateType);
// Now add a couple of columns
SPList list = lists["XMLToPDFTestList"];
string faveNum = list.Fields.Add("favoriteNumber", SPFieldType.Text, false);
list.Fields[faveNum].Description = "favorite number";
list.Fields[faveNum].Update();
string faveCol = list.Fields.Add("favoriteColor", SPFieldType.Text, false);
list.Fields[faveCol].Description = "favorite color";
list.Fields[faveCol].Update();
}
This is all I see when I click "XMLToPDFTestList":
My "gut feeling" is that this line:
SPList list = lists["XMLToPDFTestList"];
...is not right/not specific enough. Instead of "XMLToPDFTestList" it should be something else/prepend something, or so. But what, exactly?
It was, as is so often the case, "my bad" (YMMV?).
The problem is in my list item creation code which, because I was not assigning anything to the default/inherited "Title" field, made it appear to me (as in the scream shot above) that no item was being added.
Once I fixed the code, by changing this:
private void SaveInputToList()
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists[listTitle];
SPListItem SPListItemFaveNum = list.Items.Add();
SPListItemFaveNum["favoriteNumber"] = "7"; //inputtedNumber; TODO: Once 7 and teal are being saved and retrieved successfully, assign the var vals - will need to declare the controls created in CreateChildControls() globally
SPListItemFaveNum.Update();
SPListItem SPListItemFaveHue = list.Items.Add();
SPListItemFaveHue["favoriteColor"] = "teal";
SPListItemFaveHue.Update();
}
}
}
...to this:
private void SaveInputToList()
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists[listTitle];
SPListItem spli = list.Items.Add();
spli["Title"] = "Write the Title";
spli["favoriteNumber"] = "7";
//SPListItemFaveNum.Update();
//SPListItem SPListItemFaveHue = list.Items.Add();
spli["favoriteColor"] = "teal";
//SPListItemFaveHue.Update();
spli.Update();
}
}
}
...it works fine: an item is added with all three values (Title, favoriteNumber, and favoriteColor).
I was assuming the item wasn't being created because "Title" was blank, and I was calling update on each SPListItem, whereas all I really need to do is call Update once, and on one SPListItem, not multiple.
We recently upgraded to Sitecore 6.6 and are running into issues with the search and crawl functionality from Lucene since 6.6 uses a newer version and has some of the methods/functions have been made obsolete.
The code below used to work fine with the previous version of Lucene.NET 2.3 but isn't working in 2.9. Can you tell us what we are doing wrong and help us rectify this piece of code? The error we get while compiling is
`Lucene.Net.Search.IndexSearcher` does not contain a definition for 'Search'
and no extension method 'Search' accepting a first argument of type
`Lucene.Net.Search.IndexSearcher` could be found (are you missing a using
directive or an assembly reference?)
This error happens on this line - Sitecore.Search.SearchHits hits = new SearchHits(context.Searcher.Search(query,sort));. I'm guessing that it will be a simple fix but I'm unsure of how to go about fixing it.
private static SearchResultCollection GetSearchResults(Query query, Sort sort, int startingIndex, int getCount, out int totalHits)
{
SearchResultCollection retVal = new SearchResultCollection();
Sitecore.Search.Index searchIndex = Sitecore.Search.SearchManager.GetIndex("content");
using (Sitecore.Search.IndexSearchContext context = searchIndex.CreateSearchContext())
{
Sitecore.Search.SearchHits hits = new SearchHits(context.Searcher.Search(query,sort));
totalHits = hits.Length;
//since index is zero based... adjust the numbers
startingIndex = (startingIndex - 1) * getCount;
getCount = (getCount > totalHits || totalHits < startingIndex + getCount)
? hits.Length - startingIndex : getCount;
retVal = hits.FetchResults(startingIndex, getCount);
}
return retVal;
}
Thanks
Sitecore 6.6 uses Lucene 2.9. The code below is your code updated to support the newer version of Lucene. There are 2 major changes:
Search method is executed with 2 additional parameters (Filter which is set to null and maxDocs which is set to int.MaxValue).
SearchHits constructor takes IndexReader instance as the second parameter.
Code below should work exactly as you expect.
using (Sitecore.Search.IndexSearchContext context = searchIndex.CreateSearchContext())
{
TopFieldDocs docs = context.Searcher.Search(query, null, int.MaxValue, sort);
Sitecore.Search.SearchHits hits = new SearchHits(docs, context.Searcher.GetIndexReader());
totalHits = hits.Length;
startingIndex = (startingIndex - 1) * getCount;
getCount = (getCount > totalHits || totalHits < startingIndex + getCount) ? hits.Length - startingIndex : getCount;
retVal = hits.FetchResults(startingIndex, getCount);
}
Not terribly familiar with Sitecore, but Searcher.search(Query, Sort) was deprecated in Lucene 2.9, and looks like wasn't present at all in Lucene.Net. Instead, call Searcher.search(Query, Filter, int, Sort). The second argument (Filter) can be null, and the third (int) indicates the number of documents to return from the search.
What I want to do is to get the available branches that I can merge to. Pretty much the same as the dropdown when you say merge in TFS2008 and you select the destination branch.
However, it's been very hard to find how.
Below code is a merge between some of the resources I've found in the web, but none seem to work. My guess is that if VS2008 can do it, I can do it thru the SDK, right?.
The below code always give me the same result all the time.
My repository is something like this:
Development
Version1
Code
Version2
Code
Version3
Code
Main
Code
And normally I branch Main > Version X. So the merging can be done Main > Version X and Version X to Main.
The below code always gives me the children of the Main, even if I'm querying (tfsParentBranchPath) with Version3 folder.
Is this because perhaps I used TFS2010 web service but pointing to TFS2008 (that's why I marked some methods that do not work in the code)?
Well please let me know, if someone knows the answer.
Thanks!
public string[] GetChildBranchesToMerge(string tfsParentBranchPath)
{
var versionControl = teamFoundationServer.GetService<VersionControlServer>();
//not supported by tfs2008
//ItemIdentifier[] identifiers = versionControl.QueryMergeRelationships(tfsParentBranchPath);
//var allBranches = versionControl.QueryBranchObjects(new ItemIdentifier(tfsParentBranchPath), RecursionType.OneLevel);
List<string> childs = new List<string>();
ItemSpec[] specs = new ItemSpec[] { new ItemSpec(tfsParentBranchPath, RecursionType.OneLevel) };
BranchHistoryTreeItem[][] branchHistoryTree = versionControl.GetBranchHistory(specs, VersionSpec.Latest);
if (branchHistoryTree.Length > 0 && branchHistoryTree[0].Length > 0)
{
var treeItem = branchHistoryTree[0][0];
if (treeItem.Children.Count > 0)
{
foreach (BranchHistoryTreeItem tia in treeItem.Children)
{
childs.Add(tia.Relative.BranchToItem.ServerItem);
}
}
}
return childs.OrderBy((s) =>
{
return s;
}).ToArray();
}
Finally, this is the last version that works!.
First, I stopped using VS2010 assemblies and it's much faster now.
Second, the big difference is this line:
var treeItem = branchHistoryTree[0][0].GetRequestedItem();
I really dont know why the difference, but it seems without that method, the item returned is a generic one.
public string[] GetChildBranchesToMerge(string tfsParentBranchPath)
{
DoLog("Getting child branches for {0} ...", tfsParentBranchPath);
VersionControlServer vcs = (VersionControlServer)teamFoundationServer.GetService(typeof(VersionControlServer));
List<string> childs = new List<string>();
ItemSpec[] specs = new ItemSpec[] { new ItemSpec(tfsParentBranchPath, RecursionType.OneLevel) };
BranchHistoryTreeItem[][] branchHistoryTree = vcs.GetBranchHistory(specs, VersionSpec.Latest);
if (branchHistoryTree.Length > 0 && branchHistoryTree[0].Length > 0)
{
var treeItem = branchHistoryTree[0][0].GetRequestedItem();
AddChildBranch(childs, treeItem, tfsParentBranchPath);
if (treeItem.Children != null && treeItem.Children.Count > 0)
{
foreach (BranchHistoryTreeItem tia in treeItem.Children)
{
AddChildBranch(childs, tia, tfsParentBranchPath);
}
}
}
DoLog("{0} child branches found", childs.Count);
return childs.OrderBy((s) =>
{
return s;
}).ToArray();
}
private void AddChildBranch(List<string> list, BranchHistoryTreeItem itemToCheck, string tfsParentBranchPath)
{
if (itemToCheck.Relative.BranchFromItem != null && itemToCheck.Relative.BranchFromItem.DeletionId == 0 && itemToCheck.Relative.BranchFromItem.ServerItem != tfsParentBranchPath)
list.Add(itemToCheck.Relative.BranchFromItem.ServerItem);
if (itemToCheck.Relative.BranchToItem != null && itemToCheck.Relative.BranchToItem.DeletionId == 0 && itemToCheck.Relative.BranchToItem.ServerItem != tfsParentBranchPath)
list.Add(itemToCheck.Relative.BranchToItem.ServerItem);
}