Getting the highligted text from query sent from .Net

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..

Check out the documentation on Highlighting usage.

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 .

@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...

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..

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.

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