-1

I have a simple Examine search like so;

var results = Umbraco.Search(Request.QueryString["query"], true, "MySearcher");
foreach (var result in results)
{
<h2>@result.Name</h2>
<p>Content from 'contentgrid'?</p>
}

My question is, how do I get a snippet of text from the Grid? Propertyname is contentgrid. Viewing the index, I can see there is a property named contentgrid containing the text, stripped from formatting etc.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
brother
  • 7,651
  • 9
  • 34
  • 58
  • [This post](https://gist.github.com/abjerner/bdd89e0788d274ec5a33) looks promising as it shows how you can add the pure text to the Examine index when an index occurs. – harvzor Jun 26 '17 at 13:08

2 Answers2

1

Hi I wrote some code to allow you to do a more advanced search in umbraco.

This article gives you that code. http://www.codeshare.co.uk/blog/how-to-search-by-document-type-and-property-in-umbraco/

I use it for the search on my website. The word mismatched only appears in the content grid for one article of my site. The below search url proves that it works.

http://www.codeshare.co.uk/search/?query=mismatched

Kind regards

Paul

prjseal
  • 246
  • 1
  • 5
0

I think there are two approaches you could take.

One is to add a custom field in the Examine index and then using the GatheringNodeData event, index the text that you want to display. That way you will be able to access it from the SearchResult object (@result.Fields["customFieldName"]). The GatheringNodeData event handler will have to parse the grid data to extract the text snippet you want and then add it to the Examine document (e.Fields["content"] = textSnippet).

The other approach would be to get the text snippet from the node when displaying the results.

var helper = new UmbracoHelper(UmbracoContext.Current);
foreach (var result in results)
{
  var node = helper.TypedContent(result.Id);
  var gridData = node.GetPropertyValue("contentgrid");
  // some code for extracting the text snippet from the grid data
}

Note in both techniques, you need to figure out how to extract the text snippet you want from the grid data. You could use Skybrud.Umbraco.GridData or just parse the JSON yourself (using JSON.NET). I think the post @Harvey mentioned in a comment yesterday would be helpful for this (and more details on handling the GatheringNodeData event).

Alex
  • 2,795
  • 3
  • 21
  • 21