cancel
Showing results for 
Search instead for 
Did you mean: 

How do I replicate the Alfresco search via API

vezance
Champ in-the-making
Champ in-the-making

I'm using Alfresco Community Edition 5.2 (EA). I'm building a separate front-end for the users to search for documents, and am using the REST APIs to communicate with Alfresco.

I've noticed that the results returned via the REST API (Post > Search) match with the results shown for the same query in the node browser. However, these differ from results shown when searching within Alfresco.

e.g. If I search Alfresco for 'term', I will get documents which contain 'term' across all properties, including name, content and tags. However, if I enter the same query in node browser, or in the API, I only get documents which contain 'term' in their content. Documents which have 'term' in both the title and as tags, but not in the content, are not returned.

Further, the ordering of results returned by the API differs from the order shown in Alfresco, even when sorted by relevance or score.

The query I attempted via the REST API:

{
  "query": {
    "query": "TERM",
    "language": "afts"
  },
  "spellcheck":{"query":"TERM"},

  "sort": [{"type":"FIELD", "field":"relevance", "ascending":"false"}]
}

I could at least include documents with 'term' in their name by updating the query to:

{

  "query": {
    "query": "@cm:name:TERM OR TERM"...

However, this still did not consider document tags.

I simply want to ensure that the results which are shown when searching in Alfresco are the same results which are fetched and displayed via API.

1 ACCEPTED ANSWER

afaust
Legendary Innovator
Legendary Innovator

The Alfresco Share Faceted Search UI uses a special (internal) ReST backend to execute queries that transparently enhances the user search terms, i.e. it adds filters to exclude results that make no sense in the Share application, adds a query template to apply the keywords (temrs) on multiple fields at the same time (i.e. name, title, description, content, tags ...), and also prepares the results in a way that they can be consumed by the UI with as little overhead as possible.

The public ReST API for queries is a raw interface to the search APIs and as such you would have to replicate a lot of the intrinsic conditionals the backend for the UI applies. Unfortunately the v1 ReST API documentation is still missing for 5.2 so it is hard to tell what parameters you'd have to set how at this time. The documentation should be there sometime in February at the latest when Alfresco One 5.2 is scheduled to be released.

View answer in original post

15 REPLIES 15

romschn
Star Collaborator
Star Collaborator

Are you using the same REST API which is being invoked from Alfresco Search screen? Try using that, it should return exactly the same results as you see in Alfresco search result screen.

Hope this help.

vezance
Champ in-the-making
Champ in-the-making

Thanks Ramesh.

How do I determine which is the API invoked from the Alfresco Search screen?

romschn
Star Collaborator
Star Collaborator

You may want to use the developer tools of the browser and track the network calls which should show the HTTP requests being made.

Hope this helps.

afaust
Legendary Innovator
Legendary Innovator

The Alfresco Share Faceted Search UI uses a special (internal) ReST backend to execute queries that transparently enhances the user search terms, i.e. it adds filters to exclude results that make no sense in the Share application, adds a query template to apply the keywords (temrs) on multiple fields at the same time (i.e. name, title, description, content, tags ...), and also prepares the results in a way that they can be consumed by the UI with as little overhead as possible.

The public ReST API for queries is a raw interface to the search APIs and as such you would have to replicate a lot of the intrinsic conditionals the backend for the UI applies. Unfortunately the v1 ReST API documentation is still missing for 5.2 so it is hard to tell what parameters you'd have to set how at this time. The documentation should be there sometime in February at the latest when Alfresco One 5.2 is scheduled to be released.

vezance
Champ in-the-making
Champ in-the-making

Thanks Axel.

Is similar documentation available for 5.1 or 5.0? I'm hoping I could glean something from there.

I've found this link but it doesn't seem to be what you are referring to.

http://docs.alfresco.com/5.0/pra/1/topics/pra-welcome-aara.html

Additionally, would I be able to achieve what I'm looking for using the CMIS REST or Java APIs, instead of the Alfresco REST APIs??

afaust
Legendary Innovator
Legendary Innovator

The v1 ReST API is new in Alfresco 5.2 and thus there is no relevant documentation for 5.1/5.0 that you could use as reference instead.

The v1 ReST API is just a wrapper around the internal Java API, so of course you could achieve what you want to do by creating a custom web script of your own and using the Java API (or even the Script API for JavaScript-based web scripts). The CMIS API only allows CMIS SQL queries to be executed and does not support concepts like query templates, facets etc.

If you are creating a new integration with an external application I would really recommend you try and stick to the standard, new v1 ReST APIs and/or CMIS APIs as best as possible. At this time you just may have to figure one or two things out yourself until the documentation for v1 ReST API is complete. From what I understand, all the low-level search functionality should be exposed via the v1 ReST APIs, so I expect query templates and all the other features of the default search UI to be accessible via the v1 ReST API as well - I just can't tell you right now how you would have to adapt your current call since I have not worked with the new v1 ReST API myself (and there is no complete documentation yet).

ddraper
World-Class Innovator
World-Class Innovator

The Share team evaluated the new public APIs for search that will be available in 5.2 and found they could not easily be used within the current search page in Share. One of the main issues was the way in which faceting can be requested and subsequent filters applied. Because of the way in which search was previously implemented this meant that using the new API was a non-starter for Share. Therefore the internal API is likely to stay around for as long as Share does.

It's worth noting that we have continued to improve the internal search API used by Search in 5.2 so that it includes full node metadata and now supports search term highlighting. It makes use of a re-usable search.lib.js file that could be used by custom WebScript APIs and as Axel says is just a wrapper around the Java API. 

Depending upon your use case you may find the internal API fits your needs better, or you may find that the public API is a better fit.

vezance
Champ in-the-making
Champ in-the-making

Hi Dave,

We are also evaluating the usefulness of the web API used by Share. I believe it is of this structure?

http://<IP & Port>/alfresco/service/slingshot/search?term=<term>&tag=&startIndex=0&sort=&rootNode=<rootNode>&repo=false&query=&pageSize=25&maxResults=0&spellcheck=true

I was wondering if there is a way to use search term boosting here. I tried changing to URL to (change in bold):

http://<IP & Port>/alfresco/service/slingshot/search?term=<term>&cm:description:<term>^100&tag=&startIndex=0&sort=&rootNode=<rootNode>&repo=false&query=&pageSize=25&maxResults=0&spellcheck=true

However, nothing changed in the results.

Am I doing it wrong, or is term boosting unsupported?

ddraper
World-Class Innovator
World-Class Innovator

Unfortunately I don't know the answer to this. I will try and find out the answer to this, the value assigned to "term" will be passed through to the underlying Java API, but whether or not this is actually used I don't know.