简体   繁体   English

如何在java / solrj中解析Solr Analysis请求的响应?

[英]How to parse the response of a Solr Analysis request in java/solrj?

i have a java program to make requests in a solr server. 我有一个Java程序在solr服务器中发出请求。 I created a query that triggers the analysis service of solr : 我创建了一个查询来触发solr的分析服务:

HttpSolrClient server = new 
HttpSolrClient("http://localhost:8983/solr/docs");
            SolrQuery query = new SolrQuery();
            query.setRequestHandler("/analysis/field");
            query.set("analysis.fieldtype", "text_en");
            query.set("analysis.fieldvalue", "TESTS");
            query.set("wt", "json");

The response i get back is something like: 我得到的回复是这样的:

{responseHeader={status=0,QTime=2},analysis={field_types={text_en={index={org.apache.lucene.analysis.standard.StandardTokenizer=[{text=TESTS,raw_bytes=[54 45 53 54 53],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1]}],org.apache.lucene.analysis.core.StopFilter=[{text=TESTS,raw_bytes=[54 45 53 54 53],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1, 1]}],org.apache.lucene.analysis.core.LowerCaseFilter=[{text=tests,raw_bytes=[74 65 73 74 73],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1, 1, 1]}],org.apache.lucene.analysis.en.EnglishPossessiveFilter=[{text=tests,raw_bytes=[74 65 73 74 73],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1, 1, 1, 1]}],org.apache.lucene.analysis.miscellaneous.SetKeywordMarkerFilter=[{text=tests,raw_bytes=[74 65 73 74 73],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1, 1, 1, 1, 1],org.apache.lucene.analysis.tokenattributes.KeywordAttribute#keyword=false}],org.apache.lucene.analysis.en.PorterStemFilter=[{text=test,raw_bytes=[74 65 73 74],start=0,end=5,org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute#positionLength=1,type=<ALPHANUM>,position=1,positionHistory=[1, 1, 1, 1, 1, 1],org.apache.lucene.analysis.tokenattributes.KeywordAttribute#keyword=false}]}}},field_names={}}}

Which is not a valid json. 这不是有效的json。 I want to parse it and get the texts ie "tests", "test". 我想解析它并获取文本,即“测试”,“测试”。

I can only retrieve the analysis part by : 我只能通过以下方式检索分析部分:

response.getResponse().get("analysis");

which is a class org.apache.solr.common.util.SimpleOrderedMap Object. 这是org.apache.solr.common.util.SimpleOrderedMap对象的类。

Any ideas? 有任何想法吗? Thank you in advance. 先感谢您。

I finally used another solution. 我终于使用了另一种解决方案。 With an http request in: 在http请求中:

http://localhost:8983/solr/docs/analysis/field?wt=json&analysis.showmatch=true&analysis.fieldvalue={custom}&analysis.fieldtype={custom}

I get back the result in a valid json format. 我以有效的json格式返回结果。

Basically, it's a sin of Solr, as you mentioned it's not valid json, but it's rather string representation of what is called in Solr a NamedList (SimpleOrderedMap is a subclass of NamedList) 基本上,这是Solr的罪过,正如您提到的那样,它不是有效的json,但是它只是Solr中称为NamedList的字符串表示形式 (SimpleOrderedMap是NamedList的子类)

A simple container class for modeling an ordered list of name/value pairs. 一个简单的容器类,用于建模名称/值对的有序列表。 Unlike Maps: 与地图不同:

  • Names may be repeated 名称可以重复
  • Order of elements is maintained 元素顺序保持不变
  • Elements may be accessed by numeric index 元素可以通过数字索引访问
  • Names and Values can both be null 名称和值都可以为空

    NamedList provides fast access by element number, but not by name. NamedList提供按元素编号(但不按名称)的快速访问。

Unfortunately, there are no built in support for transforming NamedList, so, you have to write custom Java code, that will extract needed properties out of NamedList 不幸的是,没有内置支持转换NamedList的功能,因此,您必须编写自定义Java代码,该代码将从NamedList中提取所需的属性。

The other possibility is to use FieldAnalysisRequest , which will return it FieldAnalysisResponse , which have a methods like: 另一种可能性是使用FieldAnalysisRequest ,它将返回它FieldAnalysisResponse ,它具有如下方法:

getFieldNameAnalysis(String fieldName)

which will give you FieldAnalysisResponse.Analysis 这将为您提供FieldAnalysisResponse.Analysis

From two possible solutions, I will recommend the latter, since it will be much easier to grasp and maintain. 从两种可能的解决方案中,我将推荐后者,因为它很容易掌握和维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM