0
{
    "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"
            ]
        },

    }
}

This is JSON data I got from solr when i set hl=true and hl.fl=description. Here I got docs tag and highlighting tag. I need to parse highlighting tag to highlight "elegant" in description field which is in <em> tag...one more thing is UID_PK's(8252,8142) in <highlighting> are generated dynamically each time. Please suggest how can I do this if I am getting JSON data as

$.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=20&q="+newquery+"&sort=price asc&hl=true&hl.fl=description&hl.usePhraseHighlighter=true&json.wrf=?", function(newresult){

and parsing it as

$.each(newresult.response.docs, function(i,item){

and

 $.each(newresult.highlighting, function(i,hitem){
5
  • Your JSON is invalid: "docs": [ 1.{. docs and highlighting are no tags, they are properties. What is what you want to do now? Please clarify your problem. Jun 23, 2011 at 6:28
  • @Felix Kling :i have edited json, now it is valid. my problem is i want to get "elegant" highlighted in my search results, which i suppose i could get if i parse highlighting property upto <em>elegant<em>
    – Romi
    Jun 23, 2011 at 6:43
  • @Felix Kling: Now my problem is how to go upto <em>
    – Romi
    Jun 23, 2011 at 6:44
  • And the search results are which onces? Those in docs? Jun 23, 2011 at 6:46
  • @Felix Kling: yeah, search results are in docs
    – Romi
    Jun 23, 2011 at 6:47

1 Answer 1

1

Assuming the response is always text and only the to be highlighted elements are enclosed in <em> tags and there is only one of them, you could do this:

var highlight = {};

$.each(newresult.highlighting, function(i, hitem){
    var match = hitem.description[0].match(/<em>(.*?)<\/em>/);
    highlight[i] = match[1];
});

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

This only works if word does not contain nay special regular expression characters (in which case you'd have to escape them). If you know that the to be highlighted word only occurs once in the search results, you don't have to use regex:

item.description.replace(word, '<em>' + word + '</em>');

DEMO

16
  • Felix Kling: Can i use hitem.description[0] because description is inside "8252", i am unable to get this description :(
    – Romi
    Jun 23, 2011 at 7:15
  • 1
    @Romi: That should actually work. i should be 8252 and hitem the corresponding element. See: jsfiddle.net/fkling/bhXbh Jun 23, 2011 at 7:28
  • you used JSON.stringify((hitem.description[0])), what is this.
    – Romi
    Jun 23, 2011 at 7:39
  • Felix Kling:when i put alert(word) in $.each(newresult.highlighting, function(i, hitem){, then my jsp page is not working. script does not show any error but it is not running
    – Romi
    Jun 23, 2011 at 7:40
  • @Romi: I just tried something. Works also without it: jsfiddle.net/fkling/bhXbh/2 . You have to put alert(word) after it was defined. Jun 23, 2011 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.