{
"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"
]
},
}
}这是我在设置hl=true和hl.fl=description时从solr获得的JSON数据。这里我得到了docs标签和highlighting标签。我需要解析highlighting标签来突出显示“优雅”的描述字段,这是在<em> tag...one更多的东西是UID_PK's(8252,8142)在<highlighting>是动态生成的每一次。如果我以如下方式获取JSON数据,请建议我如何做
$.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){并将其解析为
$.each(newresult.response.docs, function(i,item){和
$.each(newresult.highlighting, function(i,hitem){发布于 2011-06-23 14:56:08
假设响应始终是文本,并且只有要突出显示的元素包含在<em>标记中,并且只有一个元素,您可以这样做:
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
});这仅在word不包含特殊正则表达式字符(in which case you'd have to escape them)时有效。如果您知道要突出显示的单词在搜索结果中只出现一次,则不必使用regex:
item.description.replace(word, '<em>' + word + '</em>');https://stackoverflow.com/questions/6450102
复制相似问题