Skip to content

Commit

Permalink
SharePoint API: search namespace enhancements #496
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed May 27, 2022
1 parent fd58a6e commit eaa617a
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 27 deletions.
13 changes: 7 additions & 6 deletions examples/sharepoint/search/search_sort_list.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import json

from office365.runtime.types.string_collection import StringCollection
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.search.query.sort import Sort
from office365.sharepoint.search.search_request import SearchRequest
Expand All @@ -10,8 +7,12 @@
ctx = ClientContext(test_site_url).with_credentials(test_user_credentials)
search = SearchService(ctx)

select_props = StringCollection(["Path", "LastModifiedTime"])
request = SearchRequest("IsDocument:1", SelectProperties=select_props)
request.SortList.add(Sort("ModifiedBy", 1))
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'))
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from office365.runtime.queries.service_operation_query import ServiceOperationQuery
from office365.runtime.paths.service_operation import ServiceOperationPath
from office365.sharepoint.base_entity_collection import BaseEntityCollection
from office365.sharepoint.recyclebin.recycleBinItem import RecycleBinItem
from office365.sharepoint.recyclebin.item import RecycleBinItem


class RecycleBinItemCollection(BaseEntityCollection):
Expand Down
9 changes: 9 additions & 0 deletions office365/sharepoint/search/query/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from office365.runtime.client_value import ClientValue


class QueryConfiguration(ClientValue):
"""This object contains the query configuration for the local farm and is the response
to the REST call get query configuration (section 3.1.5.18.2.1.6)."""

def __init__(self):
pass
6 changes: 6 additions & 0 deletions office365/sharepoint/search/query/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from office365.runtime.client_value import ClientValue


class QueryContext(ClientValue):
"""This object contains the query context properties."""
pass
5 changes: 0 additions & 5 deletions office365/sharepoint/search/query/querySuggestionResults.py

This file was deleted.

16 changes: 16 additions & 0 deletions office365/sharepoint/search/query/suggestion_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from office365.runtime.client_value import ClientValue
from office365.runtime.types.string_collection import StringCollection


class QuerySuggestionResults(ClientValue):
"""
The QuerySuggestionResults complex type is a container for arrays of query suggestions, people name suggestions,
and personal result suggestions.
"""

def __init__(self, people_names=None):
"""
:param list[str] people_names: People names suggested for the user query. MUST be null if
ShowPeopleNameSuggestions in properties input element is set to false.
"""
self.PeopleNames = StringCollection(people_names)
68 changes: 60 additions & 8 deletions office365/sharepoint/search/search_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,73 @@ class SearchRequest(ClientValue):
Uniform Resource Locator (URL) length limitations that some clients experience with HTTP GET operations.
"""

def __init__(self, query_text, select_properties=None, culture=None, trim_duplicates=False, **kwargs):
def __init__(self, query_text, select_properties=None, culture=None, trim_duplicates=False,
row_limit=None, rows_per_page=None, start_row=None,
enable_sorting=None, sort_list=None, query_template=None, ranking_model_id=None,
summary_length=None, collapse_specification=None, client_type=None,
enable_query_rules=None, **kwargs):
"""
:param str query_text: Query expression
:param list[str] or None select_properties:
:param list[str] or None culture:
:param bool or None trim_duplicates:
:param str query_text: The query text of the search query. If this element is not present or a value is not
specified, a default value of an empty string MUST be used, and the server MUST return a
FaultException<ExceptionDetail> message.
:param list[str] or None select_properties: As specified in [MS-QSSWS] section 2.2.4.11.
:param list[str] or None culture: Specifies the identifier of the language culture of the search query.
If present, the value MUST be a valid language code identifier (LCID) of a culture name,
as specified in [RFC3066].
:param bool or None trim_duplicates: Specifies whether duplicates are removed by the protocol server
before sorting, selecting, and sending the search results. A value of "true" indicates that the protocol
server SHOULD perform duplicate result removal. A value of "false" indicates that the protocol server
MUST NOT attempt to perform duplicate result removal. If this element is not present or a value is
not specified, a default value of "true" MUST be used. The algorithm used for duplicate detection is
specific to the implementation of the protocol server.
:param str or None ranking_model_id: The GUID of the ranking model that SHOULD be used for this search query.
If this element is not present or a value is not specified, the protocol server MUST use the default
ranking model, according to protocol server configuration.
:param int or None row_limit: The number of search results the protocol client wants to receive, starting at
the index specified in the StartRow element. The RowLimit value MUST be greater than or equal to zero.
:param bool or None enable_sorting: Specifies whether sorting of results is enabled or not.
MUST ignore the SortList specified if this value is set to false.
:param list[Sort] or None sort_list: Specifies the list of properties with which to sort the search results.
MUST be a SortCollection data type, as specified in section 3.1.4.7.3.4. If this element is not present
or a value is not specified, the default managed property Rank and default direction
of "Descending" MUST be used.
:param str or None query_template: This is the text that will replace the query text.
It can contain query variables which a query transform will replace during execution of the query.
:param int or None summary_length: The maximum number of characters in the result summary.
The protocol server MUST return a HitHighlightedSummary property that contains less than or equal
to SummaryLength number of characters. The SummaryLength value MUST be greater than or equal to zero
and less than or equal to 10000.
:param int or None start_row: zero-based index of the first search result in the list of all search results
the protocol server returns. The StartRow value MUST be greater than or equal to zero.
:param int or None rows_per_page: The number of result items the protocol client displays per page.
If this element is set to an integer value less than 1, the value of the RowLimit element MUST be used
as the default value.
:param str or None collapse_specification: A set of collapse specifications containing managed properties
that are used to determine how to collapse individual search results. Results are collapsed into one or
a specified number of results if they match any of the individual collapse specifications.
Within a single collapse specification, results will be collapsed if their properties match
all of the individual properties in the collapse specification.
:param str or None client_type: represents the place where the search query is sent from
:param bool or None enable_query_rules: Specifies whether query rules are included when a search query is
executed. If the value is true, query rules are applied to the search query. If the value is false,
query rules MUST NOT be applied in the search query.
"""
super(SearchRequest, self).__init__()
self.Querytext = query_text
self.SelectProperties = StringCollection(select_properties)
self.ClientType = None
self.CollapseSpecification = None
self.ClientType = client_type
self.CollapseSpecification = collapse_specification
self.Culture = culture
self.SortList = ClientValueCollection(Sort)
self.EnableSorting = enable_sorting
self.SortList = ClientValueCollection(Sort, sort_list)
self.TrimDuplicates = trim_duplicates
self.RankingModelId = ranking_model_id
self.RowLimit = row_limit
self.RowsPerPage = rows_per_page
self.QueryTemplate = query_template
self.SummaryLength = summary_length
self.StartRow = start_row
self.EnableQueryRules = enable_query_rules
self.__dict__.update(**kwargs)

@property
Expand Down
2 changes: 1 addition & 1 deletion office365/sharepoint/search/search_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from office365.runtime.paths.resource_path import ResourcePath
from office365.sharepoint.base_entity import BaseEntity
from office365.sharepoint.search.query.popular_tenant_query import PopularTenantQuery
from office365.sharepoint.search.query.querySuggestionResults import QuerySuggestionResults
from office365.sharepoint.search.query.suggestion_results import QuerySuggestionResults
from office365.sharepoint.search.search_result import SearchResult


Expand Down
19 changes: 18 additions & 1 deletion office365/sharepoint/search/search_setting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
from office365.runtime.client_result import ClientResult
from office365.runtime.queries.service_operation_query import ServiceOperationQuery
from office365.sharepoint.base_entity import BaseEntity
from office365.sharepoint.search.query.configuration import QueryConfiguration


class SearchSetting(BaseEntity):
pass
"""This object provides the REST operations defined under search settings."""

def get_query_configuration(self, call_local_search_farms_only=True):
"""
This REST operation gets the query configuration. See section 3.1.5.18.2.1.6.
:param bool call_local_search_farms_only: This is a flag that indicates to only call the local search farm.
"""
result = ClientResult(self.context, QueryConfiguration())
payload = {
"callLocalSearchFarmsOnly": call_local_search_farms_only
}
qry = ServiceOperationQuery(self, "getqueryconfiguration", None, payload, None, result)
self.context.add_query(qry)
return result
2 changes: 1 addition & 1 deletion office365/sharepoint/sites/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from office365.sharepoint.portal.site_status import SiteStatus
from office365.sharepoint.principal.user import User
from office365.sharepoint.publishing.communication_site import CommunicationSiteCreationRequest
from office365.sharepoint.recyclebin.recycleBinItemCollection import RecycleBinItemCollection
from office365.sharepoint.recyclebin.item_collection import RecycleBinItemCollection
from office365.sharepoint.sites.site_health_summary import SiteHealthSummary
from office365.sharepoint.sites.sph_site import SPHSite
from office365.sharepoint.sites.usage_info import UsageInfo
Expand Down
2 changes: 1 addition & 1 deletion office365/sharepoint/webs/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from office365.sharepoint.principal.user import User
from office365.sharepoint.principal.user_collection import UserCollection
from office365.sharepoint.pushnotifications.push_notification_subscriber import PushNotificationSubscriber
from office365.sharepoint.recyclebin.recycleBinItemCollection import RecycleBinItemCollection
from office365.sharepoint.recyclebin.item_collection import RecycleBinItemCollection
from office365.sharepoint.sharing.externalSharingSiteOption import ExternalSharingSiteOption
from office365.sharepoint.sharing.object_sharing_settings import ObjectSharingSettings
from office365.sharepoint.sharing.sharing_link_data import SharingLinkData
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9'
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10'
],
packages=setuptools.find_packages(exclude=['tests', 'tests.*',
'generator', 'generator.*',
Expand Down
2 changes: 1 addition & 1 deletion tests/sharepoint/test_recycle_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from tests.sharepoint.sharepoint_case import SPTestCase

from office365.sharepoint.files.file import File
from office365.sharepoint.recyclebin.recycleBinItemCollection import RecycleBinItemCollection
from office365.sharepoint.recyclebin.item_collection import RecycleBinItemCollection


class TestSharePointRecycleBin(SPTestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/sharepoint/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest import TestCase

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.search.query.querySuggestionResults import QuerySuggestionResults
from office365.sharepoint.search.query.suggestion_results import QuerySuggestionResults
from office365.sharepoint.search.query_result import QueryResult
from office365.sharepoint.search.search_request import SearchRequest
from office365.sharepoint.search.search_result import SearchResult
Expand Down

0 comments on commit eaa617a

Please sign in to comment.