highlight query string in more than one field using solr search feature

Posted by Romi on Stack Overflow See other posts from Stack Overflow or by Romi
Published on 2011-06-24T07:35:37Z Indexed on 2011/06/24 8:22 UTC
Read the original article Hit count: 232

Filed under:
|
|

i am using solr indexes for showing my search results. to show serch results i am parsing json data received from solr. i am able to highlight a query string in search result but only in a single field. for this i set hl=true and hl.fl="field1". i did it as

    $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+lowerCaseQuery+"&hl=true&hl.fl=description,name&hl.usePhraseHighlighter=true&sort=price asc&json.wrf=?", function(result){
                                        var n=result.response.numFound
                        var highlight = new Array(n);
                        $.each(result.highlighting, function(i, hitem){

                            var match = hitem.text[0].match(/<em>(.*?)<\/em>/);
                             highlight[i]=match[1];

                        });

                            $.each(newresult.response.docs, function(i,item){
                    var word=highlight[item["UID_PK"]];
                    var result = item.text[0].replace(new RegExp(word,'g'), '<em>' + word + '</em>');
                            });

for this json object is as :

{
    "responseHeader": {
        "status": 0,
        "QTime": 32
    },
    "response": {
        "numFound": 21,
        "start": 0,
        "docs": [
            {
                "description": "The matte finish waves on this wedding band contrast with the high polish borders. This sharp and elegant design was finely crafted in Japan.",
                "UID_PK": "8252",

            },
            {
                "description": "This elegant ring has an Akoya cultured pearl with a band of bezel-set round diamonds making it perfect for her to wear to work or the night out.",
                "UID_PK": "8142",

            },

        ]
    },
    "highlighting": {
        "8252": {
            "description": [
                " and <em>elegant</em> design was finely crafted in Japan."
            ]
        },
        "8142": {
            "description": [
                "This <em>elegant</em> ring has an Akoya cultured pearl with a band of bezel-set round diamonds making"
            ]
        },

    }
}

Now if i want to highlight query string in two fields i did as hl=true hl.fl=descrption, name my json is as:

{

     "responseHeader":{
           "status":0,
           "QTime":16
      },
     "response":{
           "numFound":1904,
           "start":0,
           "docs":[
                {
                       "description":"",
                       "UID_PK":"7780",
                       "name":[
                             "Diamond bracelet with Milgrain Bezel1"
                             ]

                },
               {
                      "description":"This pendant is sure to win hearts. Round diamonds form a simple and graceful line.",
                      "UID_PK":"8121",
                      "name":[
                              "Heartline Diamond Pendant"
                             ]
               },
            "highlighting":{

            "7780":{
           "name":[
                "<em>Diamond</em> bracelet with Milgrain Bezel1"
                 ]
               },
 "8121":{

        "description":[
          "This pendant is sure to win hearts. Round <em>diamonds</em> form a simple and graceful line."
      ],
     "name":[
          "Heartline <em>Diamond</em> Pendant"
      ]
}
}

}

Now how should i parse it to get the result. suggest me some general technique, so if i want to highlight query in more fields then i could do so.

Thanks

© Stack Overflow or respective owner

Related posts about JSON

Related posts about solr