on ‎2018 Jul 19 12:33 PM
I want to be able to add a negative filter query. In my SearchQueryPopulator I can do
target.getSearchQuery().addFilterQuery("myfield", value);
to add
&fq=myfield_string_mv:value
to the Solr search, but what I want to add is
&fq=-myfield_string_mv:value
but I can't see how to do it.
The QueryField.QueryOperator class doesn't contain anything like a NOT_EQUAL_TO constant so I can't see how that would help me.
At the moment I'm having to resort to
addFilterRawQuery("-myfield_string_mv:" + value);
Thanks, Andy
Request clarification before answering.
Had a simliar problem (fetching all documents from solr which have a certain field exported) and solved it pretty much the same way as Andy:
1) In our facades extension I extended in mycore-beans.xml
<bean class="de.hybris.platform.commerceservices.search.solrfacetsearch.data.SolrSearchQueryData">
<property name="requireHitImage" type="boolean"/>
</bean>
mycore-spring:xml
<alias name="defaultSearchQueryHitImagePopulator" alias="searchQueryHitImagePopulator"/>
<bean id="defaultSearchQueryHitImagePopulator"
class="...core.search.solrfacetsearch.SearchQueryHitImagePopulator"/>
<bean parent="modifyPopulatorList">
<property name="list" ref="commerceSearchQueryPageableConverter"/>
<property name="add" ref="searchQueryHitImagePopulator"/>
</bean>
implementation:
public class SearchQueryHitImagePopulator<FACET_SEARCH_CONFIG_TYPE, INDEXED_TYPE_SORT_TYPE> implements
Populator<SearchQueryPageableData<SolrSearchQueryData>, SolrSearchRequest<FACET_SEARCH_CONFIG_TYPE, IndexedType, IndexedProperty, SearchQuery, INDEXED_TYPE_SORT_TYPE>> {
@Resource(name = "fieldNameTranslator")
private FieldNameTranslator fieldNameTranslator;
/**
* If only solr documents with a hit image are required, add a raw query filter
* "<fieldname>:*" matches all solr documents which have the field exported
*/
@Override
public void populate(SearchQueryPageableData<SolrSearchQueryData> source,
SolrSearchRequest<FACET_SEARCH_CONFIG_TYPE, IndexedType, IndexedProperty, SearchQuery, INDEXED_TYPE_SORT_TYPE> target) {
final SolrSearchQueryData searchQueryData = target.getSearchQueryData();
if (searchQueryData.isRequireHitImage()) {
String fieldName = fieldNameTranslator.translate(target.getSearchQuery(), MyCoreConstants.Facet.HIT_IMAGE, FieldNameProvider.FieldType.INDEX);
target.getSearchQuery().addFilterRawQuery(fieldName + ":*");
}
}
}
Be sure to add it as raw query since otherwise your value would be escaped before sending the request to solr
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @andyf9,
Did you find the exact solution how to correct the behaviour of getting the negative result in FQ ?
I? am also getting an extra "\" appended in the FQ.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I had the same problem.
What I did was
addQuery("-myfield_string_mv",SearchQueryOperator.Operator.OR,values)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the suggestion but I'm specifically looking for a filter query. However I did try what you've suggested with the addFilterQuery method and it generates the wrong Solr query.
e.g. with addFilterQuery("-myfield_string_mv", "value")
I get &fq=\-myfield_string_mv:valuein my Solr query which gives me an incorrect result (presumably because there isn't a field named \-myfield_string_mv)
Using addRawFilterQuery("-myfield_string_mv:value") gives me &fq=-myfield_string_mv:value which is the correct filter.
Implement Populator<SearchQueryConverterData, SolrQuery> to add your custom filter query.
you can use SolrQuery.addFilterQuery(string) to add a fq
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.