gis.ContentManager.search will not do exact match.

2696
14
Jump to solution
08-23-2021 05:04 PM
Brian_Wilson
Honored Contributor

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
1 Solution

Accepted Solutions
Brian_Wilson
Honored Contributor

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.

View solution in original post

14 Replies
DanPatterson
MVP Esteemed Contributor

Brian, I have moved your question to the ArcGIS for Python questions community since the python question is less python and more api related


... sort of retired...
0 Kudos
Brian_Wilson
Honored Contributor

Thanks Dan. I'm thinking about adding ...thinking about retiring... to my posts explain my surliness.

DanPatterson
MVP Esteemed Contributor

@JoeBorgione has laid claimsies to "Can't Wait to Retire" so I think "thinking about retiring" is still open for now 😉


... sort of retired...
PeterMilenkovic
Frequent Contributor

Hi there

Is there any reason why you don't want to use the item id? This will be an exact match.

item =  portal.content.get('itemid')

 

 

Brian_Wilson
Honored Contributor

My goal is to create tools that other people can and will use.

If I tell my co-workers they will need to remember to type in  "814cdd2d372f47ac88b9d2996567368e" instead of "Vector Tile Layer" they will simply not use the tool.

In fact I am already using "get" method because it works but that only works for me.

 

 

0 Kudos
PeterMilenkovic
Frequent Contributor

Ok,

You can specify the type of item? You have the same text in the title of your vector tile package/layer.

Brian_Wilson
Honored Contributor

Your suggestions are very good, but I still want to be able to do exact matching. 🙂

One of my other cases for example requires matching a layer named "Clatsop County" and we have probably 30 other layers that have "Clatsop County" somewhere in their names.

Ultimately I think I'll teach people how to find the ID, it is a pain but it removes all ambiguity.

Esri needs more than "Accept as Solution", I'd like to mark your answer as "Accept as Workaround" 🙂

 

PeterMilenkovic
Frequent Contributor

Ah, that would be annoying.

Found this re searching, the overview explains why exact matching is not ideal.

https://developers.arcgis.com/rest/users-groups-and-items/search-reference.htm

0 Kudos
Brian_Wilson
Honored Contributor

That would explain the documentation, the text for "filter" bled through to the python docs even though it's not implemented. It says

"As a general rule, filter (i.e. defined in filter parameter) should be used instead of query

  • for binary yes/no searches.
  • for queries on exact values."

So when I am bored later this afternoon I will look at using the REST interface instead of the API and see if it works.

 

0 Kudos