Alfresco Solr SearchService.query() error parsing

2019-09-03 08:31发布

问题:

I'm trying to query for some files in Alfresco using SearchService; my idea is:

1) get folder's noderef where I want to search in for files

2) then get noderef's path via NodeService

3) finally query Solar via SearchService to find files in that specific path

The problem raises when querying to Solr, I get the following exception:

    ERROR [solr.core.SolrCore] [http-bio-8443-exec-1] org.apache.solr.common.SolrException: org.apache.lucene.queryParser.ParseException: **Cannot parse** 'PATH:"/{http\://www.alfresco.org/model/application/1.0}company_home/{http\://www.alfresco.org/model/application/1.0}user_homes/{http\://www.alfresco.org/model/content/1.0}abeecher/{http\://www.alfresco.org/model/content/1.0}nominas//*"': **Failed to parse XPath**...
Unexpected '{http://www.alfresco.org/model/application/1.0}company_home/{http://www.alfresco.org/model/application/1.0}user_homes/{http://www.alfresco.org/model/content/1.0}abeecher/{http://www.alfresco.org/model/content/1.0}nominas//*'

If I replace the full prefixes by prefixes of type cm: etc... the query works well.

Is there any "Alfresco Way" to do this instead of transforming the string with a regex? Or am I doing something wrong?

Code I'm using is:

Path path3 = nodeService.getPath(folder); 

    SearchParameters sp = new SearchParameters();
    sp.addStore(Repository.getStoreRef());
    sp.setLanguage(SearchService.LANGUAGE_LUCENE);
    sp.setQuery("PATH:\"/{http://www.alfresco.org/model/application/1.0}company_home/{http://www.alfresco.org/model/application/1.0}user_homes/{http://www.alfresco.org/model/content/1.0}abeecher/{http://www.alfresco.org/model/content/1.0}nominas//*\"");    
    //sp.setQuery(path3);                           
    //sp.setQuery(path3.toString());                           
    ResultSet results = null;
    results = searchService.query(sp);

回答1:

afaik PATH-Queries using full namespace syntax is not supported. Take a look here: http://wiki.alfresco.com/wiki/Search#Path_Queries

You'll have to use the prefix version. But, please don't use a regex to get the prefix. There is an org.alfresco.service.namespace.NamespacePrefixResolver (bean NamespaceService) taht defines a method Collection<String> getPrefixes(String namespaceURI).

your dummy code to get the QNamePath of a node:

Path path = nodeService.getPath(folder);
final Map<String, String> cache = new HashMap<String, String>();
final StringBuilder buf = new StringBuilder(128);
for (final Path.Element e : path)
{
    if (e instanceof Path.ChildAssocElement)
    {
        final QName qname = ((Path.ChildAssocElement)e).getRef().getQName();
        if (qname != null)
        {
            String prefix = cache.get(qname.getNamespaceURI());
            if (prefix == null)
            {
                // first request for this namespace prefix, get and cache result
                Collection<String> prefixes = ns.getPrefixes(qname.getNamespaceURI());
                prefix = prefixes.size() != 0 ? prefixes.iterator().next() : "";
                cache.put(qname.getNamespaceURI(), prefix);
            }
            buf.append('/').append(prefix).append(':').append(ISO9075.encode(qname.getLocalName()));
        }
    }
    else
    {
        buf.append('/').append(e.toString());
    }
}
String searchPath = buf.toString();