Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering Results in Sharepoint Search #496

Closed
domdinicola opened this issue Apr 8, 2022 · 7 comments
Closed

Ordering Results in Sharepoint Search #496

domdinicola opened this issue Apr 8, 2022 · 7 comments

Comments

@domdinicola
Copy link
Contributor

Hi @vgrem ,

Was wondering if the ordering is implemented in the Search Functionality..
I saw https://docs.microsoft.com/en-us/sharepoint/dev/general-development/sharepoint-search-rest-api-overview#sortlist , not sure if it's implemented

@domdinicola
Copy link
Contributor Author

@vgrem this is not been implemented yet, correct?

@vgrem vgrem added the question label Apr 14, 2022
@vgrem
Copy link
Owner

vgrem commented Apr 14, 2022

Greetings @domdinicola and good to hear from you :)

For the GET request

it could be represented like this:

ctx = ClientContext(site_url).with_credentials(credentials)
search = SearchService(ctx)
request = SearchRequest("sharepoint", sortlist="rank:descending,modifiedby:ascending")
result = search.query(request).execute_query()

For POST request

request = SearchRequest(query_text="sharepoint",
                        SortList={'results': [
                            {
                                'Direction': 1,
                                'Property': 'ModifiedBy',
                            },
                            {
                                'Property': 'Created',
                                'Direction': '0'
                            },
                        ]})
result = search.post_query(request).execute_query()

In terms of API design and usability apparently not the most intuitive and will be improved in future releases.

@vgrem vgrem closed this as completed Apr 14, 2022
@domdinicola
Copy link
Contributor Author

Greetings to you @vgrem ! Always good to hear from you :)

This doesn't seem to work for me... I also tried with different Property/Fields and changing the Direction...
Did you try it?

@vgrem
Copy link
Owner

vgrem commented May 29, 2022

Hi @domdinicola,

hmm, seems to be working as expected on my side.

Anyway in a new version (2.3.12) a few enhancements have been introduced for Search namespace, among them:

  • an alternative way of passing (KQL) query expression to query and post_query methods of SearchService class (no need of instantiating SearchRequest type if only query text needs to be passed):
search = SearchService(ctx)
result = search.query("IsDocument:1").execute_query()
  • a simplified way of passing the complex props (namely SelectProperties, SortList properties) when constructing a search request:
search = SearchService(ctx)
request = SearchRequest(query_text="IsDocument:1",
                        sort_list=[Sort("LastModifiedTime", 1)],
                        select_properties=["Path", "LastModifiedTime"],
                        row_limit=20)

Sorting search results example

request = SearchRequest(query_text="IsDocument:1",
                        sort_list=[Sort("LastModifiedTime", 1)],
                        select_properties=["Path", "LastModifiedTime"],
                        row_limit=20)
result = search.post_query(request).execute_query()
relevant_results = result.value.PrimaryQueryResult.RelevantResults
for r in relevant_results.get('Table').get('Rows').items():
    cells = r[1].get('Cells')
    print(cells[1].get('Value'))

Result

Screenshot from 2022-05-29 14-46-23

@vgrem vgrem closed this as completed May 29, 2022
@domdinicola
Copy link
Contributor Author

hi @vgrem !

Thanks for your help here! Much appreciated :)
Looks like the new SearchRequest doesn't have the possibility to set the source_id, can we (re)introduce it?

Dom

@vgrem
Copy link
Owner

vgrem commented Jun 1, 2022

Greetings @domdinicola,

ah, good catch, thanks! I confirm it is not explicitly exposed in 2.3.12 but it should be backward compatible and therefore SourceId could be passed like this:

request = SearchRequest(query_text="IsDocument:1",
                        sort_list=[Sort("LastModifiedTime", 1)],
                        select_properties=["Path", "LastModifiedTime"],
                        row_limit=20, SourceId="--source id goes here--")

@domdinicola
Copy link
Contributor Author

Thanks @vgrem ,
I confirm you it's backward compatible and that select_properties changed from dict to list.

Best,
Dom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants