if (ExamineManager.Instance.TryGetIndex("ExternalIndex", out var index))
{
var searcher = index.GetSearcher();
//var results = searcher.CreateQuery("content").NodeTypeAlias("home").And().Field("nodeName", searchTerm).Execute();
//var results = searcher.CreateQuery().NativeQuery("+__IndexType:content +nodeName:" + searchTerm).Execute();
//var results = searcher.CreateQuery().NativeQuery("+__IndexType:content " + searchTerm).Execute();
var results = searcher.CreateQuery("content").ManagedQuery(searchTerm).Execute();
if (results.Any())
{
<ul>
@foreach (var result in results)
{
if (result.Id != null)
{
var node = Umbraco.Content(result.Id);
<li>
<a href="@node.Url">@node.Name</a>
</li>
}
}
</ul>
}
else
{
<p>No results found for query @searchTerm</p>
}
}
The results correctly include a page that contains my search term. But, it's also returning an image with my search term in its file name. How do I exclude media items from my searcher?
Using examine, it is possible to exclude media from your search.
To do this use the fluent API to add the following to your query:
.And().Field("__IndexType", "content")
This will ensure results given back are only for content.
I believe that this is what searcher.CreateQuery("content") is meant to do but there is a bug in it. You could try upgrading Examine and it might fix that issue, although I can't recall if it does or not.
v8 Search Exclude MediaItem
I'm using this for my searcher:
The results correctly include a page that contains my search term. But, it's also returning an image with my search term in its file name. How do I exclude media items from my searcher?
This works...
Credit: https://our.umbraco.com/forum/umbraco-8/96004-simple-search-in-v8#comment-305886
Hi BH,
Using examine, it is possible to exclude media from your search.
To do this use the fluent API to add the following to your query:
This will ensure results given back are only for content.
I believe that this is what
searcher.CreateQuery("content")
is meant to do but there is a bug in it. You could try upgrading Examine and it might fix that issue, although I can't recall if it does or not.Nik
I ended up using @Nik's code.
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.