Reputation: 133
I've got some issue to access groupValue in Solr query. I want to show groupValue in jquery autocomplete label. But I can't do it. Can anyone help me to do this?
{
"responseHeader":{
"status":0,
"QTime":29,
"params":{
"q":"strSO:*",
"indent":"true",
"fl":"strSO",
"start":"0",
"rows":"2147483647",
"wt":"json",
"group.field":"strSO",
"group":"true"}},
"grouped":{
"strSO":{
"matches":112559,
"groups":[{
"groupValue":"EV11777-01",
"doclist":{"numFound":53,"start":0,"docs":[
{
"strSO":"EV11777-01"}]
}},
{
"groupValue":"EV15872-01",
"doclist":{"numFound":1829,"start":0,"docs":[
{
"strSO":"EV15872-01"}]
}},
{
"groupValue":"EV16143-02",
"doclist":{"numFound":929,"start":0,"docs":[
{
"strSO":"EV16143-02"}]
}},
$(function() {
var URL_PREFIX = "http://localhost:8983/solr/archiveCore/select?group=true&group.field=strSO&rows=2147483647&q=strSO:";
var URL_SUFFIX = "&wt=json"; // facet.field=strSO&facet=on&rows=0";
$("#searchBoxstrSO").autocomplete({
source: function(request, response) {
var URL = URL_PREFIX + $("#searchBoxstrSO").val() + URL_SUFFIX;
$.ajax({
url: URL,
success: function(data) {
var docs = JSON.stringify(data.grouped.strSO.groups.groupValue);
var jsonData = JSON.parse(docs);
response($.map(jsonData, function(value, key) {
return {
label: value
}
}));
},
dataType: 'jsonp',
jsonp: 'json.wrf'
});
},
minLength: 0
})
});
$(function() {
var URL_PREFIX = "http://localhost:8983/solr/archiveCore/select?group=true&group.field=strSO&rows=2147483647&q=strSO:";
var URL_MIDDLE = "OR strSO_ngram:";
var URL_SUFFIX = "&wt=json"; // &facet.field=strSO&facet=on&rows=0"; // &facet.field=strSO&facet=on&rows=0 added
$("#ngramBoxstrSO").autocomplete({
source: function(request, response) {
var searchString = "\"" + $("#ngramBoxstrSO").val() + "\"";
var URL = URL_PREFIX + searchString + URL_MIDDLE +
searchString + URL_SUFFIX;
$.ajax({
url: URL,
success: function(data) {
var docs = JSON.stringify(data.grouped.strSO.groups.groupValue);;
var jsonData = JSON.parse(docs);
response($.map(jsonData, function(value, key) {
return {
label: value
}
}));
},
dataType: 'jsonp',
jsonp: 'json.wrf'
});
},
minLength: 0
})
});
Is there any idea to show it in html label? Or Can I access doclist's docs? It's last part of my project and if I do this correctly, I will be more happy man :)
Upvotes: 0
Views: 45
Reputation: 11225
You don't need to use stringify method as it converts a javascript object into a string. Just make sure that you get a JSON object from ajax response.
var docs = data.grouped.strSO.groups;
$.map(docs, function(value, key) {
return { label: value.groupValue}
});
My test example:
var data = {
"responseHeader":{
"status":0,
"QTime":29,
"params":{
"q":"strSO:*",
"indent":"true",
"fl":"strSO",
"start":"0",
"rows":"2147483647",
"wt":"json",
"group.field":"strSO",
"group":"true"}},
"grouped":{
"strSO":{
"matches":112559,
"groups":[{
"groupValue":"EV11777-01",
"doclist":{"numFound":53,"start":0,"docs":[
{
"strSO":"EV11777-01"}]
}},
{
"groupValue":"EV15872-01",
"doclist":{"numFound":1829,"start":0,"docs":[
{
"strSO":"EV15872-01"}]
}},
{
"groupValue":"EV16143-02",
"doclist":{"numFound":929,"start":0,"docs":[
{
"strSO":"EV16143-02"}]
}} ]}}};
var docs = data.grouped.strSO.groups;
var result = $.map(docs, function(value, key) {
return { label: value.groupValue}
});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Upvotes: 1