Search code examples
solrjsonphighlighting

Solr json response needs all highlighted docs


I need to be able to type in a query in the search textfield in my web app and return highlighted results from each document that has the specific text. The problem is, I can't get the response to show all documents, it only shows one.

Lets say I have 4 documents with ID of doc1, doc2, doc3, doc4 etc. How can I get the code to loop through to display content from all 4 documents rather than just one. I've hardcoded doc2 into my program to get it to work, I'm having trouble looping through it.

Ext.data.JsonP.request({
url: 'http://localhost:8983/solr/collection1/select?q='+searchValue+'&wt=json&indent=true&hl=true&hl.fl=content&hl.simple.pre=%3Cem%3E&hl.simple.post=%3C%2Fem%3E',
callbackKey: "json.wrf",

success: function( res, req ) {
    for (i=0; i<res.response.numFound; i++) {
        var docId = res.response.docs[i].id;

        //This returns all ids. ex. doc1, doc2 etc.
        alert(docId);


        htmlCode += "<h4>Your search term: "+searchValue+"</h4><p>"+res.highlighting.doc2.content+"</p>";

    }

    //Print code below -- irrelevant for the question.
} 

Solution

  • Figured it out from this post: How do I access properties of a javascript object if I don't know the names?

    I included this in my for loop to get it working:

     var hl = res.highlighting;
     var content="";
     Object.keys(hl).forEach(function (key) {
         if(key == docId) {
             content = hl[key].content;
         }
     });