我正在尝试在Spring Data Solr生成的Solr查询中添加下面的参数(qf,bq)。
Solr参数是:
qf => Spring Data Solr Method?
bq => Spring Data Solr Method?
我能够找到以下方法
fq => addFilterQuery
fl => addProjectionOnField
defType => setDefType
qt => setRequestHandler
我看到一个未解决的问题qf https://jira.spring.io/browse/DATASOLR-153
如何将qf和bq参数添加到使用Spring Data Solr构建的Solr查询中。
由于
答案 0 :(得分:1)
您可以使用模板级别的SolrCallback
访问SolrClient
并从那里执行查询,或者为自定义查询类型注册您自己的QueryParser
。
可能是这样的:
@Bean
public SolrTemplate solrTemplate(SolrClient client) {
SolrTemplate template = new SolrTemplate(client);
template.registerQueryParser(EdismaxQuery.class, new EdisMaxQueryParser());
return template;
}
class EdismaxQuery extends SimpleQuery {
// ... add stuff you need. Maybe `autoRelax`
}
class EdisMaxQueryParser extends QueryParserBase<EdismaxQuery> {
DefaultQueryParser defaultQueryParser = new DefaultQueryParser();
@Override
public SolrQuery doConstructSolrQuery(EdismaxQuery source) {
// just use the default parser to construct the query string in first place.
SolrQuery target = defaultQueryParser.constructSolrQuery(source);
// add missing parameters
target.add("defType", "edismax");
target.add("qf", source....);
return target;
}
}
答案 1 :(得分:0)
为了避免:
org.springframework.data.solr.core.QueryParserBase $ NamedObjectsQuery 无法转换为EdismaxQuery
EdisMaxQueryParser应如下所示:
class EdisMaxQueryParser extends QueryParserBase {
@Override
public SolrQuery doConstructSolrQuery(SolrDataQuery source) {
// your stuff
}
}
答案 2 :(得分:0)
如果要向每个选择查询添加静态 qf 表达式,可以在solrconfig.xml中完成:
<requestHandler name="/select" class="solr.SearchHandler">
<lst name="defaults">
...
</lst>
<lst name="appends">
<str name="defType">edismax</str>
<str name="qf">offerId^100 vendorCode^100</str>
</lst>
...
</requestHandler>
答案 3 :(得分:0)
Spring Data Solr API 4.0
中有一些更改,因此您可能需要稍微改变自己注册QueryParser
的方式。
@Bean
public SolrTemplate solrTemplate(SolrClient client) {
SolrTemplate template = new SolrTemplate(client);
solrTemplate.registerQueryParser(EdismaxQuery.class, new EdisMaxQueryParser(new SimpleSolrMappingContext()));
return template;
}
public static class EdismaxQuery extends SimpleQuery {
private String defaultField;
private String minimumShouldMatch;
private String boostQuery;
private String queryField;
public EdismaxQuery(String queryString) {
super(queryString);
}
//... typical getter/setter
}
public static class EdisMaxQueryParser extends QueryParserBase<AbstractQueryDecorator> {
private final DefaultQueryParser defaultQueryParser;
public EdisMaxQueryParser(MappingContext<? extends SolrPersistentEntity<?>, SolrPersistentProperty> mappingContext) {
super(mappingContext);
defaultQueryParser = new DefaultQueryParser(mappingContext);
}
@Override
public SolrQuery doConstructSolrQuery(AbstractQueryDecorator queryDecorator, Class<?> domainType) {
// for some reason the API wrapped our query object with NamedObjectsQuery, so we need to unwrapped/get our actual query object first
EdismaxQuery query = (EdismaxQuery) queryDecorator.getDecoratedQuery();
// use defaultQueryParser to populate basic query parameters
SolrQuery solrQuery = defaultQueryParser.doConstructSolrQuery(query, domainType);
// set our own 'extra' parameter
if (query.getDefaultField() != null) {
solrQuery.add("df", query.getDefaultField());
}
if (query.getMinimumShouldMatch() != null) {
solrQuery.add("mm", query.getMinimumShouldMatch());
}
if (query.getQueryField() != null) {
solrQuery.add("qf", query.getQueryField());
}
if (query.getBoostQuery() != null) {
solrQuery.add("bq", query.getBoostQuery());
}
//...
return target;
}
}
这里是如何使用新的EdismaxQuery对象进行查询
EdismaxQuery query = new EdismaxQuery("hello world");
query.setDefType("edismax");
query.setRows(3);
query.setQueryField("text^2");
query.setMinimumShouldMatch("30%");
query.setBoostQuery("date:[NOW/DAY-1YEAR TO NOW/DAY]");
Page<ResultBean> results = solrTemplate.query("collection", query, ResultBean.class);
答案 4 :(得分:0)
您可以将edismax库用于spring-solr https://github.com/KmSYS/edismax-solr-spring
但是要克服覆盖afterPropertiesSet()处注册的查询解析器的问题,您需要在config类中添加以下bean,
@Bean
public SolrTemplate solrTemplate(SolrClient client) {
SolrTemplate template = new SolrTemplate(client) {
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
registerQueryParser(SimpleEdismaxQuery.class, new EdisMaxQueryParser(new SimpleSolrMappingContext()));
}
};
template.afterPropertiesSet();
return template;
}
此外,示例代码位于https://github.com/KmSYS/edismax-solr-spring-sample
http://www.kmsys.tech/solr/edixmax-query-parser-template.html