gis.ContentManager.search will not do exact match.

2142
14
Jump to solution
08-23-2021 05:04 PM
Brian_Wilson
Occasional Contributor III

Running this code in Jupyter notebook gives this output.

 

 

from arcgis.gis import GIS
from config import Config
portal = GIS(Config.PORTAL_URL, Config.PORTAL_USER, Config.PORTAL_PASSWORD)
print("Logged in as " + str(portal.properties.user.username))
portal.content.search("title:\"Unlabeled Vector Tile Layer\"")

 

 

Logged in as bwilson@CLATSOP
[<Item title:"Package for Unlabeled Vector Tile Layer" type:Vector Tile Package owner:bwilson@CLATSOP>, <Item title:"Unlabeled Vector Tile Layer" type:Vector Tile Layer owner:bwilson@CLATSOP>]

How can I force an exact match? 

I looked in Search Reference and gis.ContentManager but I can't see a way to do this.

 

0 Kudos
14 Replies
Brian_Wilson
Occasional Contributor III

It's pretty easy, a REST query works like this to give me an exact match. It was so easy that I must be missing something critical 🙂 Here is a sample query.

https://myserver/sharing/rest/search?f=json&q=&filter=title:"Vector Tile Layer"

It returns exactly one match.

Note that I had to send an empty query for the call to work "q="

I get the fuzzy matching when I move the query from filter to q, then I get 4 matches.

https://myserver/portal/sharing/rest/search?f=json&q=title:"Vector Tile Layer"

title="Vector Tile Layer",  title="Package for Unlabeled Vector Tile Layer", and so on. 

 

0 Kudos
Brian_Wilson
Occasional Contributor III

I'm used to using SQL-like queries all over in Esri APIs, so it's a mystery why queries with this tool are so useless. I assume that it was an intern's summer project and no one competent ever checked it, it fell between the cracks.

Next summer maybe they will get an intern who has finished learning about SQL or even just wildcard matches and they can add a more useful method to the ContentManager.

 

Brian_Wilson
Occasional Contributor III

Today when revisiting this issue I found this

https://community.esri.com/t5/arcgis-online-ideas/exact-match-option-for-gis-content-search/idi-p/93...

and there's a mention that Esri closed the "enhancement request" with basically "we're not going to do this because it would be hard for us." 🙂

 

0 Kudos
Brian_Wilson
Occasional Contributor III

This took a little digging around in the code but I got exact match to work, now I can wrap it in a "get id" function. Here is an example using fuzzy and exact searches.

from config import Config
from arcgis.gis import GIS
portal = GIS(Config.PORTAL_URL, Config.PORTAL_USER, Config.PORTAL_PASSWORD)
print("Logged in as " + str(portal.properties.user.username))
q='title:"Vector Tile Layer"'
fuzzy = portal.content.search(query=q)
connection = portal._con # I understand, this is private and could change
params = {'q': '', 'filter': q}
exact = connection.post(Config.PORTAL_URL + '/sharing/rest/search', params)
print("Fuzzy matches:", len(fuzzy))
print("Exact matches: %d ID: %s" % (res['total'], res['results'][0]['id']))

 

Output looks like this: 

Logged in as bwilson@CLATSOP
Fuzzy matches: 4
Exact matches: 1 ID: b69b15dbf53c459a9e709bde764aaefa

All the same options that work for the search and advanced_search methods can be passed in the "params" dictionary. If Esri would simply put an additional "filter" argument into those methods, they would work since all this code does is go directly to the post() call that's buried in those methods.

PeterMilenkovic
Occasional Contributor

Well done, that's really neat.

0 Kudos