Request Filter Exception Using SolrJ

I am using a SolrJ-based client to request Solr, and I'm trying to build HTTP requests that exclude facet name / value pairs. The web interface I'm working with has additional functionality that allows you to exclude one or more facet values. I have 3 facet fields: domain, content type and author, and I would like to be able to handle the cut by excluding from each of them. For example, q = DickensAnd will fq=-author:Dickens, Janetbuild the following HTTP request:

/solr/solrbase/select?q=Dickens&fq=-author:Dickens%2c+Janet&wt=json&indent=true

While the XML dump will look like this:

             <facets>
              <facet name="author">
                <facetEntry count="20">Dickens, Charles</facetEntry>
                <facetEntry count="10">Dickens, Sarah</facetEntry>
              </facet>
            </facets>

So far, the Java implementation I'm working with does not handle the filter exception:

private HttpSolrServer solrServer;
solrServer = new HttpSolrServer("http://localhost:8983/solr/");

private static final String CONFIG_SOLR_FACET_FIELD = "facet_field";
private String[] _facetFields = new String[] {"author"};

private static final String CONFIG_SOLR_FACETS = "facets"
     Element el = myParams.getChild(CONFIG_SOLR_FACETS);

        _facetUse = el.getAttributeValue("useFacets", "true");
        _facetMinCount = el.getAttributeValue("minCount", String.valueOf(1));
        _facetLimit = el.getAttributeValue("limit", String.valueOf(20));


List vals = el.getChildren(CONFIG_SOLR_FACET_FIELD);
        if (vals.size() > 0) {
            _facetFields = new String[vals.size()];
            for (int i=0; i < vals.size(); i++) {
            _facetFields[i] = ((Element)vals.get(i)).getTextTrim();
            }   
        }

SolrQuery query = new SolrQuery();
query.setQuery(qs);


List facetList = doc.getRootElement().getChildren("facet");
                    Iterator<String> it = facetList.iterator();
                    while (it.hasNext()) {
                        Element el = (Element)it.next(); //
                        String name = el.getAttributeValue("name"); 
                        String value = el.getTextTrim();
                        if (name != null && value != null) {    
                            facets.add(name+":"+value);
                        }

                    }


query.setQuery(qs).
           setFacet(Boolean.parseBoolean(_facetUse)).
           setFacetMinCount(Integer.parseInt(_facetMinCount)).
           setFacetLimit(Integer.parseInt(_facetLimit)).

        for (int i=0; i<_facetFields.length; i++) {
            query.addFacetField(_facetFields[i]);       
        };

        for (int i=0; i<facets.size(); i++) {
            query.addFilterQuery(facets.get(i));
        };
  return query;

    }

I was recommended to use something in this direction:

  SolrQuery solrQuery = new SolrQuery();
  solrQuery.set(CommonParams.FQ, "-author:Dickens,Janet");

, . , , . ?

,

.

1

/ Solr Query, Solr:

private QueryResponse execQuery(SolrQuery query) throws SolrServerException {
    QueryResponse rsp = solrServer.query( query );
    return rsp;     

}

, , Solr -, -:

Element elfacets = new Element("facets"); 
            List<FacetField> facets = rsp.getFacetFields();
            if (facets != null) {
                int i = 0;
                for (FacetField facet : facets) {
                    Element sfacet = new Element("facet");
                    sfacet.setAttribute("name", facet.getName());

                    List<Count> facetEntries = facet.getValues();

                    for(FacetField.Count fcount : facetEntries) {
                        Element facetEntry = new Element("facetEntry");
                        facetEntry.setText(fcount.getName());
                        facetEntry.setAttribute("count", String.valueOf(fcount.getCount()));
                        sfacet.addContent(facetEntry);
                    }
                    elfacets.addContent(sfacet);

            }
            root.addContent(elfacets);
        } 


        doc.addContent(root);

        return doc;
    }

"facets" - , XSLT, , Solr , -.

2 "facets", , EDIT 1:

<xsl:template name="facets">
                <xsl:param name="q" />
                <xsl:analyze-string select="$q" regex='AND facet_(.*?):\(("?.*?"?)\)'>
                        <xsl:matching-substring>
                        <xsl:choose>
                        <xsl:when test="regex-group(1) = 'author'">
                                    <facet name="author"><xsl:value-of select="regex-group(2)" /></facet>
                        </xsl:when>    
                        </xsl:choose>
                        </xsl:matching-substring>
                       <xsl:non-matching-substring>
                       <!--<xsl:analyze-string select="$q" regex='AND NOT facet_(.*?):\(("?.*?"?)\)'>
                       <xsl:matching-substring>
                       <xsl:choose>
                       <xsl:when test="regex-group(1) = 'author'">
                                    <facet name="author"><xsl:value-of select="regex-group(2)" /></facet>
                        </xsl:when>   
                        </xsl:choose>
                     </xsl:matching-substring>
                     </xsl:analyze-string>-->
                     </xsl:non-matching-substring>
                </xsl:analyze-string>
    </xsl:template>
</xsl:stylesheet>

author, 3 . , - :

AND NOT facet_author:("Dickens, Janet")
+4
1

, . fq- - .

SolrQuery solrQuery = new SolrQuery();
solrQuery.set(CommonParams.FQ, "-author:Dickens,Janet");

fq, (, "-author: Dickens, Janet" ). . ,

/solr/solrbase/select?q=Dickens&fq=&wt=json&indent=true

. fq=, . fq. .

, .

+2

Source: https://habr.com/ru/post/1528954/


All Articles