I have a website that was produced in sitecore that i have inherited. The search does not seem to be working correctly. Basically documents do not seem to be returned correctly. I have noted that there is the default sitecore_web_index index and also a custom index that seems to index the same content more or less. Currently the search queries the custom index however I would like to change the query to the default index to see if the document is then returned. I was told you can specify which index to use but the person never told me how to do it. Does anyone know how i can change this?
Sitecore 8 content search uses Sitecore.ContentSearch.ContentSearchManager.GetIndex(...) method to retrieve chosen index.
You can pass either:
Index name as a string:
Sitecore.ContentSearch.ContentSearchManager.GetIndex("sitecore_web_index")
IIndexable item - in this case Sitecore will try to find first registered index for you:
Sitecore.ContentSearch.ContentSearchManager.GetIndex(iIndexable)
Just find where GetIndex is used in your code and replace it with a default index name.
One thing you should be aware of - there is a chance that your custom index has some customization added (like computed fields, list of fields indexed etc). Be careful with any changes. Maybe there are other reasons why your search doesn't work. Try using IndexingManager app to rebuild the indexes and see if it helps.
You also need to remember that on Content Manager environment, the "sitecore_master_index" will be used, and on CD Environment the "sitecore_web_index" will be used
so this can lead to test errors
You can try to get the index dynamically, in that case, the code will select the correct index use, According to their environment
var indexable = Sitecore.Context.Item as SitecoreIndexableItem;
ISearchIndex index = ContentSearchManager.GetIndex(indexable);
using (IProviderSearchContext context = index.CreateSearchContext())
{
//search code...
}
Had to do it this way ...
var indexable = (SitecoreIndexableItem)Sitecore.Context.Item;
Related
I'm working on a project which includes the following activated modules:
Drupal core 8.2.3
Database Search 8.x-1.0-beta4
Search API 8.x-1.0-beta4
Search API Term Handlers 8.x-1.0-beta4
Views 8.2.3
I have a list of nids which need to be excluded from the search result of the site-wide search. The search uses Search API and has been setup using Views.
The table in the database is: "search_api_db_default_index"
The field I wish to target is: "nid"
I wasn't able to get HOOK__search_api_query_alter or HOOK_search_api_results_alter to fire, so I am attempting to manipulate the query through HOOK_views_query_alter.
I have attempted to use both the "addWhere" and "addCondition" methods with the following syntax:
When using the addCondition method, I attempted
$query->addCondition('search_api_db_default_index.nid', $oneBadNid, '<>');
and
$query->addCondition('search_api_db_default_index.nid', $manyBadNids, 'NOT IN');
and when using the addWhere method, I attempted
$query->addWhere('AND', 'search_api_index_default_index.nid', $oneBadNid, '<>');
and
$query->addWhere('AND', 'search_api_index_default_index.nid', $manyBadNids, 'NOT IN');
Regardless of whether or not I prefix the field with the table name, searching always results in triggering the following notice:
Unknown field in filter clause: 'search_api_db_default_index.nid' .
It seems that the field name is always wrapped in an html encoded string representing a single quotation, but this occurs both when using double quotations or single quotations around the supplied table.field parameter.
I am not even sure that this is what is keeping me from altering my query, but it is the only thing close to an error which I have discovered in this process. It's also possible that I'm simply not supposed to be targeting the table in the manner written, but I did not find any documentation directing me to the proper methodology.
I would appreciate any insight into this issue! Thanks!
Generally you can use
$fields = $query->getIndex()->getFields();
on the query to get an array of fields you can use within the search_api query.
Piggy-backing off of Nebel54's comment, and attempting this on my own, you don't need to include the 'table' name when setting the addCondition. However, I did need to use hook_search_api_query_alter over a views-specific one.
function mymodule_search_api_query_alter(\Drupal\search_api\Query\QueryInterface &$query) {
// Ensure field_myfield is being indexed
$fields = $query->getIndex()->getFields();
if (isset($fields['field_myfield'])) {
$query->addCondition('field_myfield', 'myvalue', '<>');
}
}
I am having problem with a "Multilist with search" field. This is a Sitecore 8 instance. the field is using a query like this to fetch a list from a lucene search index named "agents_master_index" :
TemplateFilter={3EA2CB30-0D04-4D73-9282-0103D8F34074} & StartSearchLocation={95A07C68-36B6-4D0D-AAE3-A2BFBF40C2C6}&SortField=Agent Name
I have multiple issues:
1) When I open an Item based on this template, it is very slow but it ultimately returns some results on first page of the list, however the pagination and go-to-item buttons are not working and the field is not showing number of pages.
2) If I try template's standard values' item, the above problem doesn't happen but if I click on "next page" button, it returns nothing.
I looked into Search log file to see what's going on. Turns out on when it successfully returns the first page results, it is executing following query:
4832 12:32:34 INFO ExecuteQueryAgainstLucene (agents_master_index): +_datasource:sitecore +(+(+_path:11111111111111111111111111111111 +_latestversion:1) +(+_path:95a07c6836b64d0daae3a2bfbf40c2c6 +_template:3ea2cb300d044d7392820103d8f34074)) - Filter :
but to return the second page of results, the multilist runs this query:
http://localhost/sitecore/shell/Applications/Buckets/Services/Search.ashx?fromBucketListField=*&sort=Agent%20Name&template={3EA2CB30-0D04-4D73-9282-0103D8F34074}&location=95a07c6836b64d0daae3a2bfbf40c2c6&pageSize=10&pageNumber=2&sc_content=master
An returns this JSON-like result which is basically empty:
({"CurrentPage":1,"Location":"current item","PageNumbers":0,"SearchCount":"0","SearchTime":"04.3539","facets":null,"items":[],"launchType":"contenteditor:launchtab","ContextData":[],"ContextDataView":[]})
and in the search log file what is actually being executed is this:
4832 12:28:05 INFO Search Query : +(_content:* _name:* _displayname:*) +_template:3ea2cb300d044d7392820103d8f34074 +_path:95a07c6836b64d0daae3a2bfbf40c2c6
4832 12:28:05 INFO Search Index : sitecore_index
4832 12:28:05 INFO Search Took : 4346ms
I don't understand why to retrieve the second page its looking into sitecore_index instead of "agents_master_index". What is wrong here? Should i fix my query? How can I force it to pick the correct lucene index (if this is the reason behind all this confusing problem?
Any help or insight is greatly appreciated.
Edit
By the way, the StartSearchLocation is referring to an items bucket. The "agents_master_index" is referring to same location in its definition.
UPDATE
OK, so far I have managed to work around the second problem. After exchanging some comments with Richard, I concluded that (at least in Sitecore 8) content editor expects to find items which we want to search in multilist component in the same index which "Root" item exists ( {11111111-1111-1111-1111-111111111111} ) so I just added same crawler we had in "agents_master_index" to the "sitecore_index" and it worked!
However, this is still working only on template's standard values item. So the first problem has not been solved yet. In other words, the multilist doesn't work when clicking on second page or do any search on the items which have been created based on that template, it only works properly on the template itself (the standard values item)
I came across this Fix for Sitecore Multilist and TreeList with Search Bug which looks very similar to my problem, I tried it but it didn't worked for me :(
Well I'm facing an issue here:
I use Selenium to automate test on my application, and I use the following code to fill a textbox:
driver.find_element_by_id("form_widget_serial_number").send_keys("example", u'\u0009')
All the textboxes have the same id (form_widget_serial_number) so it's impossible to select a specific textbox by using selection by id (the code above selects the first textbox and u'\u0009' changes the cursor position to the next textbox).
Anyway, how can I send values to the "current" textbox without using find_element_by_id or another selector, given that the cursor is in the current textbox?
There are several selectors available to you - Xpath and css in particular are very powerful.
Xpath can even select by the text of an element and then access its second sibling1:
elem = driver.find_element(:xpath, "//div[contains(text(), 'match this text')]/following-sibling::*[2]")
If you need to find the current element that has focus:
focused_elem = driver.switch_to.active_element
And then to find the next one:
elem_sibling = focused_elem.find_element(:xpath, "following-sibling::*")
Using this example, you could easily traverse any sequence of sequential elements, which would appear to solve your problem.
Take a close look at the structure of the page and then see how you would navigate to the element you need from a reference you can get to. There is probably an Xpath or css selector that can make the traversal across the document.
1 FYI, I'm using an old version of Selenium in Ruby, in case your version's syntax is different
My approach would be:
Get a list of all contemplable elements by the known id
Loop over them until you find a focused element
Use send_keys to write into that element
To get the focused element something like
get_element_index('dom=document.activeElement')
might work. See this question for more information.
Edit: If the list you get from 1. is accidently sorted properly the index of an element in this list might even represent the number of u'\u0009' you have to send to reach that specific element.
Use ActionChains utility to do that. Use following code:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).send_keys("Hello").perform()
I have a lookup field in list in share point 2010, how to set a default value for that field at inserting operation
Using JavaScript, override the PreSaveAction method and push whatever you like into the lookup column. this happens just prior to the post to the web server, so you cna do all validation and checks.
Beware though, the value you insert must exist in the lookup list or the post will fail, ie don't push a value of "abc" if "abc" is not on the list of possible options fo rhte lookup column.
If you want to preselect some value in lookup control (dropdown) when dialog is open you need to do it by JS. You need to hook onload (JS) event of the page. There are some libraries that makes it easier to set SharePoint field values e.g. SPJS-utility. Common way is that you send id of item using URL parameter and get by JS and then set it to field. Speaking of lookup beware of the fact that SP renders lookup control in different way in IE for less than 20 items and for more than 20 item.
How to assign templates to the _standard values programatically?.There is one template under Size folder.I want to add the item in the Employees _Standard values through insert option.
Steps:
1.Select the _Standard values of Employees Template.
2.add TeamSize template as follows [Click on the "Configure" >>Assign>>TeamSize
The above is manual process.I want to achieve the same using programmatically.
Just tell some sample idea alone.Rough snippet is enough.
I see you didn't take the advice of everyone who replied to your previous question
How to assign templates to another template dynamically in sitecore?
This really does sound like a bad idea, but if you genuinely have a good reason for this approach then the answer is very similar to that of your other question. In this case you just use the Insert Options field:
Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master");
Item standardValItem = db.GetItem("/sitecore/templates/Insert Options/Employees/__Standard Values");
Item teamSizeTemplate = db.GetItem("/sitecore/templates/Insert Options/Size/TeamSize");
using (new Sitecore.SecurityModel.SecurityDisabler())
{
try
{
standardValItem.Editing.BeginEdit();
standardValItem[Sitecore.FieldIDs.Branches] = teamSizeTemplate.ID.ToString();
}
finally
{
standardValItem.Editing.EndEdit();
}
}
To assign TeamSize template to Insert options of Employees you have on the new window that is opened , you have to go on Templates tree and extend Insert Option folder and choose Team Size.
For settings dinamically Insert Options for an item you need to do :
yourItem.Template.StandardValues["__Master"] =yourTemplate.ID.ToString();
__Master is the name of the fields, you can find it here :
/sitecore/templates/System/Templates/Sections/Insert Options/Insert Options/__Masters
Why do you want to change insert options dinamically ? I think you need this kind of implementation just when you have a branch website, and you want to create from that branch other Sites. And depending on the site , on some site you need to set some Insert Options and on other no .
Please also check this link , Insert Options are explained very clear.