solr - 在solr中查询具有不同字段的多个集合

标签 solr

鉴于以下(单核)查询:

http://localhost/solr/a/select?indent=true&q=*:*&rows=100&start=0&wt=json
http://localhost/solr/b/select?indent=true&q=*:*&rows=100&start=0&wt=json

第一个查询返回 "numFound":40000"
第二个查询返回 "numFound":10000"

我尝试通过以下方式将这些放在一起:
   http://localhost/solr/a/select?indent=true&shards=localhost/solr/a,localhost/solr/b&q=*:*&rows=100&start=0&wt=json

现在我得到“numFound”:50000”。
唯一的问题是“a”的列比“b”多。所以多个集合请求只返回a的值。

是否可以查询具有不同字段的多个集合?或者它们必须相同?我应该如何更改我的第三个 url 以获得此结果?

最佳答案

你需要的是——我称之为——统一核心。该架构本身将没有内容,它仅用作一种包装器来统一您想要从两个核心显示的那些字段。在那里你需要

  • 一个 schema.xml,它包含了您希望在统一结果中包含的所有字段
  • 为您组合两个不同核心的查询处理程序

  • 事先取自 the Solr Wiki page about DistributedSearch 的重要限制

    Documents must have a unique key and the unique key must be stored (stored="true" in schema.xml) The unique key field must be unique across all shards. If docs with duplicate unique keys are encountered, Solr will make an attempt to return valid results, but the behavior may be non-deterministic.



    例如,我有带有字段 id、title、description 的 shard-1 和带有字段 id、title、abstractText 的 shard-2。所以我有这些模式

    分片 1 的架构

    <schema name="shard-1" version="1.5">
    
      <fields>
        <field name="id"
              type="int" indexed="true" stored="true" multiValued="false" />
        <field name="title" 
              type="text" indexed="true" stored="true" multiValued="false" />
        <field name="description"
              type="text" indexed="true" stored="true" multiValued="false" />
      </fields>
      <!-- type definition left out, have a look in github -->
    </schema>
    

    分片 2 的架构

    <schema name="shard-2" version="1.5">
    
      <fields>
        <field name="id" 
          type="int" indexed="true" stored="true" multiValued="false" />
        <field name="title" 
          type="text" indexed="true" stored="true" multiValued="false" />
        <field name="abstractText" 
          type="text" indexed="true" stored="true" multiValued="false" />
      </fields>
      <!-- type definition left out, have a look in github -->
    </schema>
    

    为了统一这些模式,我创建了第三个模式,我称之为分片统一,它包含所有四个字段。

    <schema name="shard-unification" version="1.5">
    
      <fields>
        <field name="id" 
          type="int" indexed="true" stored="true" multiValued="false" />
        <field name="title" 
          type="text" indexed="true" stored="true" multiValued="false" />
        <field name="abstractText" 
          type="text" indexed="true" stored="true" multiValued="false" />
        <field name="description" 
          type="text" indexed="true" stored="true" multiValued="false" />
      </fields>
      <!-- type definition left out, have a look in github -->
    </schema>
    

    现在我需要利用这个组合模式,所以我在 solr-unification 核心的 solrconfig.xml 中创建了一个查询处理程序

    <requestHandler name="standard" class="solr.StandardRequestHandler" default="true">
      <lst name="defaults">
        <str name="defType">edismax</str>
        <str name="q.alt">*:*</str>
        <str name="qf">id title description abstractText</str>
        <str name="fl">*,score</str>
        <str name="mm">100%</str>
      </lst>
    </requestHandler>
    <queryParser name="edismax" class="org.apache.solr.search.ExtendedDismaxQParserPlugin" />
    

    就是这样。现在 shard-1 和 shard-2 中需要一些索引数据。要查询统一结果,只需使用适当的分片参数查询分片统一。
    http://localhost/solr/shard-unification/select?q=*:*&rows=100&start=0&wt=json&shards=localhost/solr/shard-1,localhost/solr/shard-2
    

    这将返回一个结果,如

    {
      "responseHeader":{
        "status":0,
        "QTime":10},
      "response":{"numFound":2,"start":0,"maxScore":1.0,"docs":[
          {
            "id":1,
            "title":"title 1",
            "description":"description 1",
            "score":1.0},
          {
            "id":2,
            "title":"title 2",
            "abstractText":"abstract 2",
            "score":1.0}]
      }}
    

    获取文档的原始分片

    如果要将原始分片提取到每个文档中,只需指定 [shard]fl .作为查询的参数或在请求处理程序的默认值中,请参见下文。括号是强制性的,它们也将出现在结果响应中。

    <requestHandler name="standard" class="solr.StandardRequestHandler" default="true">
      <lst name="defaults">
        <str name="defType">edismax</str>
        <str name="q.alt">*:*</str>
        <str name="qf">id title description abstractText</str>
        <str name="fl">*,score,[shard]</str>
        <str name="mm">100%</str>
      </lst>
    </requestHandler>
    <queryParser name="edismax" class="org.apache.solr.search.ExtendedDismaxQParserPlugin" />
    

    工作样本

    如果您想查看运行示例,请查看 my solrsample project在 github 和 execute the ShardUnificationTest 上.我现在还包括了分片提取。

    关于solr - 在solr中查询具有不同字段的多个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19313910/

    相关文章:

    solr - Solr函数查询对多值字段的计数进行操作

    主从设置中的 Apache Solr 故障转移支持

    hadoop - HDFS 上的 Solr 核心创建失败

    elasticsearch - 通过 Elasticsearch 中的查询进行文档计数聚合(如 solr 中的facet.query)

    java - Solr 错误实例化类 : Custom Class

    mysql - 索引失败。回滚所有更改。 (Solr 数据导入)

    ruby-on-rails-3 - 太阳黑子/Solr/Rails : Model Associations are not updating in the Index

    solr - 在 SOLR 中索引和查询的最佳标记分析器

    用于非规范化混合语言文档的 Solr 语言检测更新处理器

    solr - 在 solr 空间搜索中使用英里而不是公里