Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

java - When I do query from solr, it occurred a common exception telling me that undefined field userId

I am developing a web application. I use spring mvc framework , cassandra and solr. I use standalone solr, not solr cloud. I use solr as a fulltext retrieval tool. I am facing a strange problem, first here is the schema.xml.

<schema name="KSP_core" version="1.1">
  <types>
    <fieldType name="uuid" class="solr.UUIDField" indexed="true" />  
    <fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
    <fieldtype name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" positionIncrementGap="0"/>
  <fieldType name="text_ik" class="solr.TextField"> 
    <analyzer type="index" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/> 
    <analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/> 
    </fieldType>
  </types>

 <fields>   
  <!-- general -->
  <field name="id"        type="uuid"    indexed="true"  stored="true"  multiValued="false" required="true"/>
  <field name="dbTable"      type="string"    indexed="true"  stored="true"  multiValued="false" /> 
  <field name="userId"      type="string"    indexed="true"  stored="true"  multiValued="false" /> 
  <field name="originatorId"      type="string"    indexed="true"  stored="true"  multiValued="false" />
  <field name="associatedObjectId"      type="string"    indexed="true"  stored="true"  multiValued="false" /> 
  <field name="createTime"      type="date"      indexed="true"  stored="true"/>
  <field name="status"     type="int"    indexed="true"  stored="true"  multiValued="false" />
  <field name="postType"      type="string"    indexed="true"  stored="true"  multiValued="false" /> 

  <!-- user activity -->
  <field name="userOperate" type="string" indexed="true" stored="true" multiValued="false"/>


  <!--user reputation-->
  <field name="reputation" type="int"      indexed="true"  stored="true"/>
  <field name="dayTime"      type="string"    indexed="true"  stored="true"/> 
  <field name="reason"      type="string"    indexed="true"  stored="true"  multiValued="false" /> 

  <!--user tags-->
  <field name="tagId"      type="string"    indexed="true"  stored="true"  multiValued="false" /> 
  <field name="tagName"      type="string"    indexed="true"  stored="true"  multiValued="false" /> 

  <field name="reputationOnTag" type="int"      indexed="true"  stored="true"/>
  <field name="_version_" type="long" indexed="true" stored="true"/>
 </fields>

  <uniqueKey>id</uniqueKey>
 <!-- field for the QueryParser to use when an explicit fieldname is absent -->
 <defaultSearchField>userId</defaultSearchField>

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

Then I show you the solrconfig.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!--
 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.
-->

<!--
 This is a stripped down config file used for a simple example...  
 It is *not* a good example to work from. 
-->
<config>
  <luceneMatchVersion>LUCENE_40</luceneMatchVersion>
  <!--  The DirectoryFactory to use for indexes.
        solr.StandardDirectoryFactory, the default, is filesystem based.
        solr.RAMDirectoryFactory is memory based, not persistent, and doesn't work with replication. -->
  <directoryFactory name="DirectoryFactory" class="${solr.directoryFactory:solr.StandardDirectoryFactory}"/>

  <dataDir>${solr.core0.data.dir:}</dataDir>

  <updateHandler class="solr.DirectUpdateHandler2">
    <updateLog>
      <str name="dir">${solr.core0.data.dir:}</str>
    </updateLog>
  </updateHandler>

  <!-- realtime get handler, guaranteed to return the latest stored fields 
    of any document, without the need to commit or open a new searcher. The current 
    implementation relies on the updateLog feature being enabled. -->
  <requestHandler name="/get" class="solr.RealTimeGetHandler">
    <lst name="defaults">
      <str name="omitHeader">true</str>
    </lst>
  </requestHandler>  

  <requestHandler name="/replication" class="solr.ReplicationHandler" startup="lazy" /> 

  <requestDispatcher handleSelect="true" >
    <requestParsers enableRemoteStreaming="false" multipartUploadLimitInKB="2048" />
  </requestDispatcher>

  <requestHandler name="standard" class="solr.StandardRequestHandler" default="true" />
  <requestHandler name="/analysis/field" startup="lazy" class="solr.FieldAnalysisRequestHandler" />
  <requestHandler name="/update" class="solr.UpdateRequestHandler"  />
  <requestHandler name="/admin/" class="org.apache.solr.handler.admin.AdminHandlers" />

  <requestHandler name="/admin/ping" class="solr.PingRequestHandler">
    <lst name="invariants">
      <str name="q">solrpingquery</str>
    </lst>
    <lst name="defaults">
      <str name="echoParams">all</str>
    </lst>
  </requestHandler>

  <!-- config for the admin interface --> 
  <admin>
    <defaultQuery>solr</defaultQuery>
  </admin>

</config>

When I query the data from solr, I use the solrj library to achieve the query in general. like:

   String queryExpression = "*:*";
   String sortField = "createTime";
   String baseUrl = SolrUtil.getSolrBaseURL(solrHttpServer.getBaseURL());
   solrHttpServer.setBaseURL(baseUrl + "/" + coreName);
   SolrQuery query = new SolrQuery();
   query.setQuery(queryExpression);
   query.setSortField(sortField, SolrQuery.ORDER.asc);
   QueryResponse rsp = solrHttpServer.query(query);

And the following code is the error information during the query:

org.apache.solr.common.SolrException: undefined field userId
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:401)
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:181)
    at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:90)
    at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
    at com.augmentum.ksp.solr.dao.impl.SolrBaseDaoImpl.query(SolrBaseDaoImpl.java:141)
    at com.augmentum.ksp.service.impl.TagStatsServiceImpl.getUserTagStats(TagStatsServiceImpl.java:50)
    at com.augmentum.ksp.controller.TagStatsController.getUserTagStats(TagStatsController.java:162)
    at sun.reflect.GeneratedMethodAccessor188.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jasig.cas.client.util.AssertionThreadLocalFilter.doFilter(AssertionThreadLocalFilter.java:54)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jasig.cas.client.util.HttpServletRequestWrapperFilter.doFilter(HttpServletRequestWrapperFilter.java:75)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jasig.cas.client.validation.AbstractTicketValidationFilter.doFilter(AbstractTicketValidationFilter.java:201)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.augmentum.iaphelper.filter.AuthenticationFilter.doFilter(Unknown Source)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.augmentum.ksp.filter.I18nFilter.doFilter(I18nFilter.java:22)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As I see, you change baseUrl in your code. As you said in comments, you query multiple cores at the same page, and all of them use the same HttpSolrServer. So it seems to be a lack of synchronization in your solution, and that there could be two requests running at the same time, and one request quering the ‘core’ of another.

Of course, you could synchronize your method. But I think, that in your case it is better not to twitch HttpSolrServer each time, but to set additional parameter to your query, that will be used as additional path to your baseUrl. You will need to set baseUrl for HttpSolrServer once at creation time:

HttpSolrServer solrHttpServer = new HttpSolrServer(baseUrl);

and instead of changing it for each query you will set parameter (diff for your code):

 String queryExpression = "*:*";
 String sortField = "createTime";
-String baseUrl = SolrUtil.getSolrBaseURL(solrHttpServer.getBaseURL());
-solrHttpServer.setBaseURL(baseUrl + "/" + coreName);
 SolrQuery query = new SolrQuery();
+query.set(CommonParams.QT, "/" + coreName + "/select");
 query.setQuery(queryExpression);
 query.setSortField(sortField, SolrQuery.ORDER.asc);
 QueryResponse rsp = solrHttpServer.query(query);

But the best solution is to have separate HttpSolrServer for each core:

Map<String, HttpSolrServer> servers = new HashMap<String, HttpSolrServer>();
servers.put(coreName1, new HttpSolrServer(baseUrl + "/" + coreName1));
...

Resulting code changes:

 String queryExpression = "*:*";
 String sortField = "createTime";
-String baseUrl = SolrUtil.getSolrBaseURL(solrHttpServer.getBaseURL());
-solrHttpServer.setBaseURL(baseUrl + "/" + coreName);
 SolrQuery query = new SolrQuery();
 query.setQuery(queryExpression);
 query.setSortField(sortField, SolrQuery.ORDER.asc);
-QueryResponse rsp = solrHttpServer.query(query);
+QueryResponse rsp = servers.get(coreName).query(query);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...