solrQuery.setFilteQueries() 方法中的问题

发布于 2024-12-04 00:55:21 字数 1498 浏览 1 评论 0原文

我有以下查询,我从我的 URL 中获取

public static String query="pen&mq=pen&f=owners%5B%22abc%22%5D&f=application_type%5B%22cde%22%5D";

public static String q="pen";

我解析了查询字符串,并从中获取每个方面名称和方面值并将其存储在地图中

String querydec = URLDecoder.decode(query, "UTF-8");
String[] facetswithval = querydec.split("&f=");
Map<String, String> facetMap = new HashMap<String, String>();
for (int i = 1; i < facetswithval.length; i++) {

        String[] fsplit = facetswithval[i].split("\\[\"");
        String[] value = fsplit[1].split("\"\\]");

        facetMap.put(fsplit[0], value[0]);
    }

然后我使用以下代码使用 solrj 在 solr 中查询

CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr/");

        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery(q);

        for (Iterator<String> iter = facetMap.keySet().iterator(); iter.hasNext();){
              String key=iter.next();
              System.out.println("key="+key+"::value="+facetMap.get(key));
              solrQuery.setFilterQueries(key+":"+facetMap.get(key));
          }
        solrQuery.setRows(MAX_ROW_NUM);
        QueryResponse qr = server.query(solrQuery);
        SolrDocumentList sdl = qr.getResults();

但是在运行我的代码后我发现solrQuery.setFilterQuery 方法仅为最后设置的方面设置过滤器。这意味着如果我运行循环并使用此函数三次,则仅采用最后设置的过滤器值。 有人可以澄清这一点并告诉我更好的方法吗?我也在解码网址。因此,如果我的方面在中间包含一些特殊字符,那么我不会得到任何结果。我也尝试在不编码的情况下使用它,但它不起作用。 :(

I have the following query which I took from my URL

public static String query="pen&mq=pen&f=owners%5B%22abc%22%5D&f=application_type%5B%22cde%22%5D";

public static String q="pen";

I parsed my query string and took each facetname and facet value from it and stored in a map

String querydec = URLDecoder.decode(query, "UTF-8");
String[] facetswithval = querydec.split("&f=");
Map<String, String> facetMap = new HashMap<String, String>();
for (int i = 1; i < facetswithval.length; i++) {

        String[] fsplit = facetswithval[i].split("\\[\"");
        String[] value = fsplit[1].split("\"\\]");

        facetMap.put(fsplit[0], value[0]);
    }

Then i use the following code to query in solr using solrj

CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr/");

        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery(q);

        for (Iterator<String> iter = facetMap.keySet().iterator(); iter.hasNext();){
              String key=iter.next();
              System.out.println("key="+key+"::value="+facetMap.get(key));
              solrQuery.setFilterQueries(key+":"+facetMap.get(key));
          }
        solrQuery.setRows(MAX_ROW_NUM);
        QueryResponse qr = server.query(solrQuery);
        SolrDocumentList sdl = qr.getResults();

But after running my code I found out that solrQuery.setFilterQuery method is setting filter for only last set facet. That means if i m running the loop and using this function three times it is taking the last set filter values only.
Can somebody please clarify this and tell me better approach for doing this. Also I am decoding url. So, if my facet contains some special character in the middle then i am not getting any result for that. I tried using it without encoding also but it didnt work. :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2024-12-11 00:55:21

还有一个 addFilterQuery 方法,我会调用它,因为您在 for 循环中单独设置过滤器查询。

另外,请参阅这篇文章 使用 SolrJ 客户端过滤带有特殊字符的查询,来自 Solr 用户邮件列表,了解仍然需要转义查询中的特殊字符。

There is also a addFilterQuery method, I would call that since you are setting the filter queries individually in your for loop.

Also, please see this post Filter query with special character using SolrJ client from the Solr Users Mailing List about the need to still escape special characters in queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文