cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How can I add a negative filter query to a solrfacetsearch SearchQuery?

andyfletcher
Active Contributor
4,034

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

Accepted Solutions (0)

Answers (4)

Answers (4)

jseidl
Explorer
0 Likes

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

Former Member
0 Likes

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.

sduvvuri
Explorer
0 Likes

I had the same problem.

What I did was

 addQuery("-myfield_string_mv",SearchQueryOperator.Operator.OR,values)
andyfletcher
Active Contributor
0 Likes

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.

andyfletcher
Active Contributor
0 Likes

Additionally it still means that I need to know the full Solr field name or build it with getFieldNameTranslator().translate(target.getSearchQuery(), "myfield", FieldType.INDEX) which using the addQuery method with just the Hybris field name does not.

Former Member
0 Likes

Implement Populator<SearchQueryConverterData, SolrQuery> to add your custom filter query.

you can use SolrQuery.addFilterQuery(string) to add a fq

andyfletcher
Active Contributor
0 Likes

You appear to have missed the point of my question. SolrQuery.addFilterQuery doesn't allow me to add a negative filter query. It's pretty small but notice the - in front of the field in the fq that I want.