pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
749 stars 305 forks source link

Inconsistent Behavior using search vs GraphExplorer using same Query #2922

Closed frread closed 7 months ago

frread commented 7 months ago

Major Version

3.x

Minor Version Number

20

Target environment

SharePoint Framework

Additional environment details

spfx webpart development to use in sharepoing online

Expected or Desired Behavior

i would expect both queries in the graph explorer and in the sp.search would bring the same results

Observed Behavior

i am getting empty response

Steps to Reproduce

I run the following search query in graph explorer

https://graph.microsoft.com/v1.0/search/query

{
    "requests": [
        {
            "entityTypes": [
                "driveItem"
            ],
            "query": {
                "queryString": "isDocument:True AND siteId: '<site-id>' AND (PATH:*Han*)"
            }
        }
    ]
}

where is the current site-id where the fxpart is deployed and the results is what i expected.

But when i run this in my spfx

      const searchQuery = "isDocument:True AND siteId: '<site-id>' AND (PATH:*Han*)"
      console.log(searchQuery);
      const builder = SearchQueryBuilder(searchQuery)
        .selectProperties(spFieldsToSelect.join(','))
        .sortList(sortList)
        .rowLimit(this.maxResults);

      console.log(builder.toSearchQuery());

      const results = await this._sp.search(builder);

the results come as 0 and nothing is brougth.

If have tested my code in many ways and only when i put the PATH search parameter do the results differ Why could this be happening?

juliemturner commented 7 months ago

Usually, the best approach is to check the network tab after running the same call in PnPjs and see what the call looks like. If there's a difference you can point to then that would help track down how to fix it either with something you're doing or something wrong in the library.

frread commented 7 months ago

i checked the network and the querytext is the same,

here is the request url and the payload

url: https://.sharepoint.com/sites//_api/search/postquery

{"request":{"Querytext":"isDocument:True AND siteId: '<site-id>' AND (PATH:*Han*)","SelectProperties":["Title,FileExtension,LinkingUrl,FileType,UniqueId,Author,Created,LastModifiedTime],"SortList":[{"Property":"LastModifiedTime","Direction":1}],"RowLimit":10,"HitHighlightedProperties":[],"Properties":[],"RefinementFilters":[],"ReorderingRules":[]}}

what i am triying to achieve is a text search of the path so if i look for example "Han" i am able to bring the drive-items for that site, where "Han" appears anywhere on the path, either the folder structure or the item name

juliemturner commented 7 months ago

The example you shared has a URL of https://.sharepoint.com/sites//_api/search/postquery which is for SharePoint search not Graph search, those are two entirely different endpoints and work differently. Do you mean to use Graph search instead?

frread commented 7 months ago

i wanted to use sharepoint, but i was testing in graph explorer did not know they where different. Any ideas how i can do this search using the sharepoint end point?

juliemturner commented 7 months ago

That's a big question. You need to understand what ManagedProperties are available to get the values you want to filter and then use KQL to create the filter. I can guess that you're probably going to want to query on ContentTypeId which is a managed property, you can do a contains with the root content type id for a document which is 0×0101. After that you'll have to do some research for which managed properties you need. I would start in the SharePoint Admin center, under Search configuration.

frread commented 7 months ago

i know the path property is there, and the url, the issue would be why it is not being filtered.. Thank you for your help let me keep reseraching to see what i can find

bcameron1231 commented 7 months ago

Agree with Julie, learning KQL for the SharePoint API would help a bit here, as they do behave differently. If you look at the link she sent, you'll find the docs about prefix matching.

Your wildcard is wrong. You can't prepend to the term. Prefix matching only works at the end of the word, not the beginning so your search is invalid. So `Han*` is not a valid wildcard specification.

frread commented 7 months ago

after understanding that there are 2 different endpoint for graph and sharepoint i was able to find the managed property to use for my search in sharepoint. For graph i could use the path property but in sharepoint this one does not work, i had to use the ItemPath

juliemturner commented 7 months ago

I'm going to close this issue as answered. If you have further issues, please feel free to create a new issue and reference this one.

github-actions[bot] commented 7 months ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.