如何在Solr查询分组中访问groupedValue [英] How to access groupedValue in Solr Query Grouping

查看:69
本文介绍了如何在Solr查询分组中访问groupedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Solr查询中访问groupValue时遇到一些问题.我想在jquery自动完成标签中显示groupValue.但是我做不到.谁能帮我做到这一点?

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

这是我的jquery代码

$(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
    })
});

有什么想法可以在html标签中显示吗?还是可以访问文档列表的文档?这是我项目的最后一部分,如果我正确地做到这一点,我会变得更快乐的人:)

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 :)

推荐答案

您不需要使用stringify方法,因为它可以将javascript对象转换为字符串.只需确保您从ajax响应中获取一个JSON对象即可.

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}
});

我的测试示例:

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>

这篇关于如何在Solr查询分组中访问groupedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆