Solr Custom RequestHandler - 注入查询参数 [英] Solr Custom RequestHandler - injecting query parameters

查看:131
本文介绍了Solr Custom RequestHandler - 注入查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的问题:
我正在寻找一种方法(java)拦截对Solr的查询并注入我的业务逻辑提供的一些额外的过滤参数。我应该使用什么结构?

Short question: I'm looking for a way (java) to intercept a query to Solr and inject a few extra filtering parameters provided by my business logic. What structures should I use?

背景:
首先,有点坦白:我是Solr的新秀。对我来说,设置服务器,定义模式,编写功能索引管理器,然后实际看到服务器返回正确的结果 - 完全按照预期! - 对自己来说已经是一项成就。我也好!

Context: First of all, a little confession: I'm such a rookie regarding Solr. For me, setting up a server, defining a schema, coding a functional indexmanager and afterwards actually seeing the server returning the right results - exactly as intended! - was already much of an achievement for itself. Yay me!

但是我目前正在一个需要更多的企业项目中工作。粗略地说,solr实例将由数千名用户通过同一个requestHandler进行查询,即返回的文档会根据用户的权限级别自动过滤。例如,如果用户A和超级用户B都尝试了相同的搜索参数(即使是相同的URL),则用户B将获得所有用户A的文件,然后获得更多。为了实现这一目标,文档已经使用必要的权限级别信息编制索引。

However I'm currently working in an enterprise project that requires a little more than that. Roughly speaking, the solr instance is to be queried by several thousands of users through the very same requestHandler, being that the documents returned are automatically filtered according to a user's permission level. For example, if both the user A and the super-user B tried the very same search parameters (even the very same url), the user B would get all of user A's files and then some more. In order to accomplish this the documents are already indexed with the necessary permission level information.

嗯,考虑到这一点并利用Solr为新手开发人员提供的大量文档想出一个简单的自定义requestHandler来覆盖handleRequest函数,以便在SolrQueryRequest中注入必要的额外参数。一切都很好和花花公子 - 除了我在QueryResponse中看不到任何差别,服务器粗鲁地忽略了我的小操作。如果这是最好的方法,在没有太多暗示天气的情况下搜索网络几天后,最终决定在StackOverflow上找到好心人。

Well, with this in mind and making use of Solr's extensive documentation for newb developers I tried to come up with a simple custom requestHandler that overrides the handleRequest function in order to inject the necessary extra parameters in the SolrQueryRequest. All is fine and dandy - except that I never see any difference at all in the QueryResponse, the server rudely ignoring my little manipulation. After a couple of days searching the web without so much of a hint weather if this the best approach, finally decided to come up and bother the fine folks here at StackOverflow.

所以,简而言之,我的问题是:

So, in short, my questions are:


  • 这是正确的做法吗?还有其他选择吗?我已经可以掌握Solr的一些概念,但不可否认的是,它有很多缺失,而且完全有可能缺少一些东西。

  • Is this a correct approach? Are there other alternatives? I can already grasp some of Solr's concepts, but admittedly there is much lacking and its entirely possible that am missing something.

如果是这样,在修改查询参数之后我有什么办法可以强制更新QueryResponse吗?据我所知,这些仅仅是封装http请求,并且在修改后我无法嗅探查询服务器的任何内容。

If so, after modifying the query parameters is there anything I should do to force the QueryResponse to be updated? As far as I can tell these are merely encapsulating http requests, and I fail to sniff anything querying the server after the modifications are made.

提前致谢,非常抱歉这篇长篇文章!

Thanks in advance and so very sorry for the long post!

经过大量阅读API和特别多的试验和错误后,我设法获得了功能性解决方案。然而,我仍然不太了解索尔的内部,因此仍然会欣赏一些启发。随意随意打击,我仍然非常清楚自己的开玩笑。

After a lot of reading APIs and specially much trial and error I've managed to get a functional solution. However I still fail to understand much of Solr's internals, therefore would still appreciate some enlightening. Feel free to bash at will, am still very aware of my rookiness.

解决方案的相关部分是这个函数,它通过覆盖handleRequestBody来调用:

The relevant part of the solution is this function which is called from by overriden handleRequestBody:

private void SearchDocumentsTypeII(SolrDocumentList results,
        SolrIndexSearcher searcher, String q, 
        UserPermissions up, int ndocs, SolrQueryRequest req,
        Map<String, SchemaField> fields, Set<Integer> alreadyFound)
        throws IOException, ParseException {


         BooleanQuery bq = new BooleanQuery();
         String permLvl = "PermissionLevel:" + up.getPermissionLevel();
         QParser parser = QParser.getParser(permLvl, null, req);
         bq.add(parser.getQuery(), Occur.MUST);

         Filter filter = CachingWrapperFilter(new QueryWrapperFilter(bq));   

         QueryParser qp = new QueryParser(q, new StandardAnalyzer());
         Query query =  qp.parse(q);                        

         append (results, searcher.search(
          query, filter, 50).scoreDocs,
          alreadyFound, fields, new HashMap<String,Object>(), 0,
          searcher.getReader(), true);

}

基本上搜索查询是没有以任何方式修改,而是应用包含用户的PermissionLevel的过滤器。即便如此,为什么以下替代方案不起作用?搜索查询在标准requestHandler中应用时效果很好,而在这种情况下,它根本不会访问任何文档。

Basically the search query is not modified in any way, and instead a filter is applied containing the PermissionLevel of the user. Even so, why doesn't the following alternative work? The search query works perfectly when applied in the standard requestHandler, while in this case it simply doesn't hit any document.

private void SearchDocumentsTypeII(SolrDocumentList results,
        SolrIndexSearcher searcher, String q, 
        UserPermissions up, int ndocs, SolrQueryRequest req,
        Map<String, SchemaField> fields, Set<Integer> alreadyFound)
        throws IOException, ParseException {

         String qFiltered = q + " AND " + "PermissionLevel:" + up.getPermissionLevel();                              

         QueryParser qp = new QueryParser(qFiltered, new StandardAnalyzer());
         Query query =  qp.parse(qFiltered);                        

         append (results, searcher.search(
          query, null, 50).scoreDocs,
          alreadyFound, fields, new HashMap<String,Object>(), 0,
          searcher.getReader(), true);

}

推荐答案

哦,好吧。如前所述,答案对我有用。随意评论或打击!

Oh well. As previously stated, the answer that worked for me. Feel free to comment or bash!

   private void SearchDocumentsTypeII(SolrDocumentList results,
            SolrIndexSearcher searcher, String q, 
            UserPermissions up, int ndocs, SolrQueryRequest req,
            Map<String, SchemaField> fields, Set<Integer> alreadyFound)
            throws IOException, ParseException {


             BooleanQuery bq = new BooleanQuery();
             String permLvl = "PermissionLevel:" + up.getPermissionLevel();
             QParser parser = QParser.getParser(permLvl, null, req);
             bq.add(parser.getQuery(), Occur.MUST);

             Filter filter = CachingWrapperFilter(new QueryWrapperFilter(bq));   

             QueryParser qp = new QueryParser(q, new StandardAnalyzer());
             Query query =  qp.parse(q);                        

             append (results, searcher.search(
              query, filter, 50).scoreDocs,
              alreadyFound, fields, new HashMap<String,Object>(), 0,
              searcher.getReader(), true);
        }

这篇关于Solr Custom RequestHandler - 注入查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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