关于solr实现多表查询的方法

本文介绍了如何在Solr中实现多表查询,重点在于schema.xml的配置和使用copyField来处理关键字段,以避免主键冲突并实现查询功能。同时提供了java代码示例,适合开发者参考学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于solr实现多表查询的方法

话不多说上代码.
注意点一:防止主键唯一性的冲突,我选择将主键注释掉(也可以使用uuid代替主键id)
注意点二:实现多表查询的关键是 copyField 将需要分词的关键字段copy.即可实现多表查询/

下面为我的schema.xml 的配置文件

<?xml version="1.0" ?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<schema name="example core zero" version="1.1">

    <fieldtype name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/>

    <fieldType name="text_general" class="solr.TextField">

        <analyzer type="index" class="org.wltea.analyzer.lucene.IKAnalyzer" />
        <analyzer type="query" class="org.wltea.analyzer.lucene.IKAnalyzer" />

    </fieldType>

  <!-- general -->
  <!--
  <field name="id"        type="string"   indexed="true"  stored="true"  multiValued="false" required="true"/>
  -->
  <field name="id"        type="string"   indexed="true"  stored="true"  multiValued="false" />
  <field name="type"      type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="name"      type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="core0"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="_version_" type="long"     indexed="true"  stored="true"/>

  <field name="username"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="context"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="carname"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="bookname"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="bookid"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
 
  <field name="english"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="tableEn"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="tableName"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
 
  <field name="parentId"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="dKey"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="dValue"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="code"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="createTime"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="parentName"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
      <!-- cangku -->
  <field name="address"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="adminisArea"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="collectTime"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="xCoordinate"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="yCoordinate"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="height"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="storageGoods"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="telephone"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="area"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="status"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="geometry"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
      <!-- agriprobsae -->
  <field name="agriType"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="cycle"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="yield"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="relatedWareHouse"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
  <!-- logispark -->	
  <field name="desc"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="typeName"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
       <!-- field -->		 
 <field name="comment"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
 <field name="notNull"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 


<field name="url"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
<field name="layerName"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
<field name="layerType"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
<field name="isVisible"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
<field name="bound"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 
<field name="totalName"     type="text_general"   indexed="true"  stored="true"  multiValued="false" /> 


<!-- copy -->

<field name="msg_all"     type="text_general"   indexed="true"  stored="true"  multiValued="true" /> 

<copyField source="bookname" dest="msg_all"/>
<copyField source="carname" dest="msg_all"/>
<copyField source="english" dest="msg_all"/>
<copyField source="tableEn" dest="msg_all"/>
<copyField source="tableName" dest="msg_all"/>
<copyField source="code" dest="msg_all"/>
<copyField source="createTime" dest="msg_all"/>
<copyField source="parentName" dest="msg_all"/>
<copyField source="address" dest="msg_all"/>
<copyField source="adminisArea" dest="msg_all"/>
<copyField source="collectTime" dest="msg_all"/>
<copyField source="storageGoods" dest="msg_all"/>
<copyField source="area" dest="msg_all"/>
<copyField source="geometry" dest="msg_all"/>
<copyField source="relatedWareHouse" dest="msg_all"/>
<copyField source="typeName" dest="msg_all"/>
<copyField source="layerName" dest="msg_all"/>
<copyField source="bound" dest="msg_all"/>
<copyField source="totalName" dest="msg_all"/>
<!-- field to use to determine and enforce document uniqueness. -->
 <!-- 
   <uniqueKey>id</uniqueKey> 
  -->
 <!-- field for the QueryParser to use when an explicit fieldname is absent -->
 <defaultSearchField>name</defaultSearchField>

 <!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->
 <solrQueryParser defaultOperator="OR"/>

</schema>

java代码查询方法


	@RequestMapping("testQuerysolr")
	  public  void testQuerysolr(String str) throws SolrServerException {
	        SolrQuery params = new SolrQuery();
	        // 查询关键词,*:*代表所有属性、所有值,即所有index
	      //  params.set("q", str+"~2");// ~2模糊匹配,, *全文匹配
	       params.set("q", str);
	        params.set("df", "msg_all");
	        // 分页,start=0就是从0开始,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了。
	        params.set("start", 0);
	        params.set("rows", "1000");

	        // 排序,如果按照id排序,那么 写为: id desc(or asc)
	        params.set("sort", "id asc");

	        QueryResponse response =  solrclient.query(params);
	        try {

	            response = solrclient.query(params);
	        } catch (SolrServerException e) {
	            e.printStackTrace();
	        }

	        if (response != null) {
	            System.out.println("Search Results: ");
	            SolrDocumentList list = response.getResults();
	          
	            for (int i = 0; i < list.size(); i++) {
	            	//System.out.println(list);//根据key取值 
	                 System.out.println("第"+i +"条数据        " +list.get(i));
	            }
	        }

	    }

如有错误之处敬请指出.大家一起学习/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值