Search code examples
restodatasensenet

Sensenet length filter Not working


I want to query the Field which are empty and which are not empty using Sensenet Odata Rest API. Their documentation mentions a filter function called 'length'. I have tried to query the field with the length operation but it fails with the error.

This is the filter I have used

$filter=length(Name) eq 2

Sense/Net 6.5.4.9496

Exception

"code": "NotSpecified",
"exceptiontype": "SnNotSupportedException",
"message": {
"lang": "en-us",
"value": "Unknown method: length"
},

Wiki Link http://wiki.sensenet.com/OData_REST_API


Solution

  • The length operation was included in the list of supported methods incorrectly, we apologise for that. SenseNet compiles these filters to Lucene queries and it is not possible to compose such a query in Lucene that performs an operation on a field.

    (the remaining methods, like substringof or startswith can be compiled to a wildcard expression easily, so that should work)

    Unfortunately 'empty' expressions are also not supported by Lucene, because of their document/term structure. So the following expression does not work either:

    Description eq ''
    

    Edit: as a workaround, developers may create a custom field index handler.

    For every field you want to check for emptiness (e.g. Description), you may create a technical hidden bool field (IsDescriptionEmpty) in the content type definition. The only thing you have to create and define is a custom field index handler class. In your case it would inherit from the built-in bool field index handler and you could return a boolean index value based on whether the target field (in this case Description) is empty or not.

    After this you would be able to define search exressions like the following:

    +Type:File +IsDescriptionEmpty:true
    

    Please check the wiki article below and the source code for index handler examples.

    How to create a field indexhandler