热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Solr(J)中搜索嵌套文档

如何解决《在Solr(J)中搜索嵌套文档》经验,为你挑选了1个好方法。

我为PDF文档实现了一个简单的添加工具.我首先创建所有文档的主文档(SolrInputDocument).它获取了诸如author,filehash,keywords,'content_type = document'等字段.

之后我为每个页面生成一个SolrInputDocument,该对象获得一个像'parentID_p01'的id,该页面作为字段值,'content_type = page'usw.

最后,我使用addChildDocument()将所有页面文档添加到我的主文档中.

现在我的问题是,如何在所有文档的所有页面中搜索给定的单词,例如:

Document1.pdf 'this is my doc1 title' [2 matches]
    [Page 14] 'Example phrase for special a word given by.... 
    [Page 55] 'another word for this test

Document2.pdf 'doc2 title' [X matches]
    [Page 1] 'given word in this text 
    [Page 2] 'words hit more than fists
    [Page 99] 'some words of wisdom

我的第一个想法是用' text:word~ ' 进行简单搜索然后按父文档分组,但我找不到一个好方法:-(

似乎嵌套文档对solr来说有点新,我没有找到SolrJ的简单解决方案.

提前致谢



1> ManojP..:

我已经使用父子关系以下面的格式创建了solr数据.凡一个保险账户有多人的车辆保险.一个人可以有多辆车,如汽车,自行车等.我作为父母和车辆作为儿童文件.


            
                John
                Jones
                ...other details...
                
                    
                        honda
                        accord
                    
                    
                        Nissan
                        Maxima
                    
                
                
                    
                        yamaha
                        passion
                    
                    
                        Peugeot
                        Vivacity
                    
                
            
            
                ...
            
          

在下面的java代码中,我使用solrj 4.9来创建文档并在SOLR上运行搜索查询.我已经处理了QueryResponse以显示所需的结果,并且还给出了solr查询URL.

您可以从给定的代码段中获取帮助,并告诉我它是否有效.

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.LinkedHashMap;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

public class SolrNestedSearch {

    static final String SOLR_URL = "http://localhost:8983/solr/collection1";

    public static void main ( String[] args ) throws Exception {

        HttpSolrServer solr = new HttpSolrServer( SOLR_URL );

        // Docs to submit
        Collection batch = new ArrayList();

        // Parent Doc 1, a person mamed John Jones
        SolrInputDocument person1 = new SolrInputDocument();
        person1.addField( "id",            "john_jones" );
        person1.addField( "content_type",  "person"     );
        // "_t" suffix tells Solr that it's text
        person1.addField( "first_name_t",  "John"       );
        person1.addField( "last_name_t",   "Jones"      );
        // states and history used in edismax examples
        person1.addField( "states_t",      "California Nevada Idaho Maine" );
        person1.addField( "history_t",     "safe accident accident accident accident accident" );

        // child docs, the vehicles he owns
        SolrInputDocument p1_car1 = new SolrInputDocument();
        p1_car1.addField( "id",            "jj_car1"    );
        p1_car1.addField( "content_type",  "car"        );
        // For cars "make" is an alias for "manufacturer"
        p1_car1.addField( "make_t",        "Honda"      );
        p1_car1.addField( "model_t",       "Accord"     );

        SolrInputDocument p1_car2 = new SolrInputDocument();
        p1_car2.addField( "id",            "jj_car2"    );
        p1_car2.addField( "content_type",  "car"        );
        p1_car2.addField( "make_t",        "Nissan"     );
        p1_car2.addField( "model_t",       "Maxima"     );

        SolrInputDocument p1_bike1 = new SolrInputDocument();
        p1_bike1.addField( "id",           "jj_bike1"   );
        p1_bike1.addField( "content_type", "bike"       );
        p1_bike1.addField( "make_t",       "Yamaha"     );
        p1_bike1.addField( "model_t",      "Passion"    );

        SolrInputDocument p1_bike2 = new SolrInputDocument();
        p1_bike2.addField( "id",           "jj_bike2"   );
        p1_bike2.addField( "content_type", "bike"       );
        p1_bike2.addField( "make_t",       "Peugeot"    );
        p1_bike2.addField( "model_t",      "Vivacity"   );

        // Add children to parent
        person1.addChildDocument( p1_car1  );
        person1.addChildDocument( p1_car2  );
        person1.addChildDocument( p1_bike1 );
        person1.addChildDocument( p1_bike2 );

        // Add parent to batch
        batch.add( person1 );


        // Parent Doc 2, person mamed Satish Smith
        SolrInputDocument person2 = new SolrInputDocument();
        person2.addField( "id",           "satish_smith" );
        person2.addField( "content_type", "person"       );
        person2.addField( "first_name_t", "Satish"       );
        person2.addField( "last_name_t",  "Smith"        );
        person2.addField( "states_t",     "California Texas California Maine Vermont Connecticut" );
        person2.addField( "history_t",    "safe safe safe safe safe safe safe safe accident" );

        // Vehicles (child docs)
        SolrInputDocument p2_car1 = new SolrInputDocument();
        p2_car1.addField( "id",            "ss_car1"     );
        p2_car1.addField( "content_type",  "car"         );
        p2_car1.addField( "make_t",        "Peugeot"     );
        p2_car1.addField( "model_t",       "iOn"         );
        SolrInputDocument p2_bike1 = new SolrInputDocument();
        p2_bike1.addField( "id",           "ss_bike1"    );
        p2_bike1.addField( "content_type", "bike"        );
        p2_bike1.addField( "make_t",       "Honda"       );
        p2_bike1.addField( "model_t",      "Spree"       );
        // link objects and add to batch
        person2.addChildDocument( p2_car1  );
        person2.addChildDocument( p2_bike1 );
        batch.add( person2 );

        System.out.println( "Adding batch of " + batch.size() + " parent docs" );

        // Submit as a group
        solr.add( batch );
        solr.commit();



        Map params = new LinkedHashMap<>();
        params.put( "parent_filter", "content_type:person" );
        params.put( "defType", "edismax" );
        params.put( "qf", "history_t states_t^100" );
        doQuery(solr,
                "eDismax3: Drivers matching accidents and/or California, and all of their vehicles, boost on State",
                "California accident report",
                "{!parent which=$parent_filter}",
                "*,[child parentFilter=$parent_filter]",
                params );

    }


    static void doQuery( HttpSolrServer solr, String description, String queryStr, String optFilter,
            String optFields, MapextraParams ) throws Exception
    {
        SolrQuery q = new SolrQuery( queryStr );
        if ( null!=optFilter ) {
            q.addFilterQuery( optFilter );
        }
        if ( null!=optFields ) { 
            q.setParam( "fl", optFields );   
        }
        else {
            q.addField( "*" );  
        }
        if ( null!=extraParams ) {
            for ( Entry param : extraParams.entrySet() ) {
                q.set( param.getKey(), param.getValue() );
            }
        }

        // Run and show results
        QueryResponse rsp = solr.query( q );
        SolrDocumentList docs = rsp.getResults();
        long numFound = docs.getNumFound();
        System.out.println( "Matched: " + numFound );
        int docCounter = 0;
        for (SolrDocument doc : docs) {
            docCounter++;
            System.out.println( "Doc # " + docCounter );
            for ( Entry field : doc.entrySet() ) {
                String name = field.getKey();
                Object value = field.getValue();
                System.out.println( "\t" + name + "=" + value );
            }
            List childDocs = doc.getChildDocuments();
            if ( null!=childDocs ) {
                for ( SolrDocument child : childDocs ) {
                    System.out.println( "\tChild doc:" );
                    for ( Entry field : child.entrySet() ) {
                        String name = field.getKey();
                        Object value = field.getValue();
                        System.out.println( "\t\t" + name + "=" + value );
                    }
                }
            }
        }

        System.out.println( "Query URL:" );
        System.out.println( SOLR_URL + "/select?" + q );
    }
}


如何在嵌套文档中配置schema.xml?
推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文介绍了绕过WAF的XSS检测机制的方法,包括确定payload结构、测试和混淆。同时提出了一种构建XSS payload的方法,该payload与安全机制使用的正则表达式不匹配。通过清理用户输入、转义输出、使用文档对象模型(DOM)接收器和源、实施适当的跨域资源共享(CORS)策略和其他安全策略,可以有效阻止XSS漏洞。但是,WAF或自定义过滤器仍然被广泛使用来增加安全性。本文的方法可以绕过这种安全机制,构建与正则表达式不匹配的XSS payload。 ... [详细]
  • 本文介绍了Python字典视图对象的示例和用法。通过对示例代码的解释,展示了字典视图对象的基本操作和特点。字典视图对象可以通过迭代或转换为列表来获取字典的键或值。同时,字典视图对象也是动态的,可以反映字典的变化。通过学习字典视图对象的用法,可以更好地理解和处理字典数据。 ... [详细]
  • 初探PLC 的ST 语言转换成C++ 的方法
    自动控制软件绕不开ST(StructureText)语言。它是IEC61131-3标准中唯一的一个高级语言。目前,大多数PLC产品支持ST ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • angular.element使用方法及总结
    2019独角兽企业重金招聘Python工程师标准在线查询:http:each.sinaapp.comangularapielement.html使用方法 ... [详细]
  • ①页面初始化----------收到客户端的请求,产生相应页面的Page对象,通过Page_Init事件进行page对象及其控件的初始化.②加载视图状态-------ViewSta ... [详细]
  • PRML读书会第十四章 Combining Models(committees,Boosting,AdaBoost,决策树,条件混合模型)...
    主讲人网神(新浪微博:豆角茄子麻酱凉面)网神(66707180)18:57:18大家好,今天我们讲一下第14章combiningmodel ... [详细]
  • 3年半巨亏242亿!商汤高估了深度学习,下错了棋?
    转自:新智元三年半研发开支近70亿,累计亏损242亿。AI这门生意好像越来越不好做了。近日,商汤科技已向港交所递交IPO申请。招股书显示& ... [详细]
author-avatar
丫头片子ZXH
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有