# Getting the highligted text from query sent from .Net

**URL:** <https://discuss.elastic.co/t/getting-the-highligted-text-from-query-sent-from-net/107313>\
**Category:** Elasticsearch\
**Created:** [November 12, 2017, 5:31pm UTC](https://discuss.elastic.co/t/getting-the-highligted-text-from-query-sent-from-net/107313 "2017-11-12T17:31:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akshay\_Deep\_Lamba](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Akshay\_Deep\_Lamba](https://discuss.elastic.co/u/Akshay_Deep_Lamba)\
**Post date:** [November 12, 2017, 5:31pm UTC](https://discuss.elastic.co/t/getting-the-highligted-text-from-query-sent-from-net/107313/1 "2017-11-12T17:31:56Z")

</div>

hi  
I am using the below code to get the output..

```
var node = new Uri("http://192.168.0.3");

            var settings1 = new ConnectionSettings(node).RequestTimeout(TimeSpan.FromMinutes(2))
                             .DefaultIndex("books");

            var client1 = new ElasticClient(settings1);

            var searchResponse1 = client1.Search<book>(s => s
             .Query(q => q.QueryString((m => m.Query(TextBox1.Text))))
             .Highlight(h => h.RequireFieldMatch(false).PostTags("</b>").PreTags("<b>").Fields((l => l.Field(f => f.title).FragmentSize(15000)))
                       .Fields((l1 => l1.Field(f1 => f1.summary).FragmentSize(15000))))
            .Aggregations(ag => ag.Terms("language", la => la.Field(ff => ff.language)))
             );

```

How can I get the highlighted text .....like  
I used the below code to get the documents \>\>

> var books = searchResponse1.Documents;

I used the below code to get the aggregation \>\>

> var books\_agg = searchResponse1.Aggs.Terms("language").Buckets;

Pls guide..

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 13, 2017, 12:30am UTC](https://discuss.elastic.co/t/getting-the-highligted-text-from-query-sent-from-net/107313/2 "2017-11-13T00:30:55Z")

</div>

[Check out the documentation on Highlighting usage](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/highlighting-usage.html).

The documentation demonstrates highlighting on three different fields with three different highlighter types, with examples for both the fluent lambda syntax, object initializer syntax, and how to get to the highlight fields in the response .

---

<div class="post-metadata">

**Author:** ![Akshay\_Deep\_Lamba](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Akshay\_Deep\_Lamba](https://discuss.elastic.co/u/Akshay_Deep_Lamba)\
**Post date:** [November 13, 2017, 4:50pm UTC](https://discuss.elastic.co/t/getting-the-highligted-text-from-query-sent-from-net/107313/3 "2017-11-13T16:50:37Z")

</div>

@forloop THanks for the reply...

I Tried doing so.. and it worked... the only problem is the `<b>` does render like html but it renders like text ..as Show Below :-

as your can see on the Image the Scala keywork gets rendered like text...

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/b/3b99374c402ef5d79a44131073be16f425ddf7b3.png)

below is the c# function i am using

```
   private void getDataOnGridView()
        {
            lst = new List<string>();
            sst = new List<string>();
            var node = new Uri("http://192.168.0.3");

            var settings1 = new ConnectionSettings(node).RequestTimeout(TimeSpan.FromMinutes(2))
                             .DefaultIndex("books");

            var client1 = new ElasticClient(settings1);

            //my implementation
            var searchResponse1 = client1.Search<book>(s => s
           .Query(q => q.QueryString((m => m.Query(TextBox1.Text))))

             .Highlight(h => h.RequireFieldMatch(false)
             .PostTags("</b>")
             .PreTags("<b>")
             .Fields(l => l.Field(f => f.title).
                               FragmentSize(15000).NumberOfFragments(1)
                               ,
                     l1 => l1.Field(f1 => f1.summary).
                               FragmentSize(15000).NumberOfFragments(1)
                               ))
             .Aggregations(ag => ag.Terms("language", la => la.Field(ff => ff.language)))
             );

            var books = searchResponse1.Documents;
            var books_agg = searchResponse1.Aggs.Terms("language").Buckets;
            //var book_high = searchResponse1.HitsMetaData.Hits;

            
            Label1.Text = "Found "+searchResponse1.HitsMetaData.Total.ToString()+" Results in "+searchResponse1.Took+" ms" ;
            Label1.Font.Size = 8;

            //searchResponse1.ShouldBeValid();

            foreach (var highlightsInEachHit in searchResponse1.Hits.Select(d => d.Highlights))
            {
                foreach (var highlightField in highlightsInEachHit)
                {
                    if (highlightField.Key == "title")
                    {
                        
                        foreach (var highlight in highlightField.Value.Highlights)
                        {
                           
                                lst.Add(highlight.ToString());
                            
                        }
                        //+;
                    }
                    else if (highlightField.Key == "summary")
                    {
                        foreach (var highlight in highlightField.Value.Highlights)
                        {
                            
                                sst.Add(highlight.ToString());
                            
                        }
                       //+;//
                    }
                    else
                    {
                     
                    }
                }
            }

            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("Language");
            dt1.Columns.Add("Count");

            dt.Columns.Add("Title");

            dt.Columns.Add("Author");

            dt.Columns.Add("Language");

            dt.Columns.Add("Publish Year");
            dt.Columns.Add("Summary");

            foreach (KeyedBucket<string> ke in books_agg)
            {

                DataRow oItem1 = dt1.NewRow();

                oItem1[0] = ke.Key.ToUpper().ToString();
                oItem1[1] = ke.DocCount;
                dt1.Rows.Add(oItem1);

            }

            i = 0;
            List<string> mod = new List<string>();

            foreach (var st in sst)
            {
                mod.Add(st.Replace("<b>", "").Replace("</b>", "").ToString());
            }

            foreach (book value in books)
            {

           
                DataRow oItem = dt.NewRow();
               
                oItem[0] = value.title;

                oItem[1] = value.author;

                oItem[2] = value.language;

                oItem[3] = value.publishYear;

                if (sst.Count != 0 && mod.Contains(value.summary))
                {
                        oItem[4] = sst[i];
                    i++; 
                }
                else
                {
                    oItem[4] = value.summary;
                }

                dt.Rows.Add(oItem);
                

            }
            GridView1.DataSource = dt;
            GridView1.DataBind();

            GridView2.DataSource = dt1;
            GridView2.DataBind();
        }

```

Pls Guide..

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 14, 2017, 11:21pm UTC](https://discuss.elastic.co/t/getting-the-highligted-text-from-query-sent-from-net/107313/4 "2017-11-14T23:21:27Z")

</div>

That's your web framework attempting to mitigate XSS attacks by HTML encoding the text rendered rather than rendering it verbatim.

You may want to use non-HTML highlighting tags in the hightlighting request to Elasticsearch, then replace these when HTML rendering with `<b>` tags.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 12, 2017, 11:22pm UTC](https://discuss.elastic.co/t/getting-the-highligted-text-from-query-sent-from-net/107313/5 "2017-12-12T23:22:00Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
