# How can I filter an EntityClass by _not_ having a property?

With Mathematica 12, I can create implicit Entity Classes like:

I can also filter for having exactly a specific property, like so

It seems quite trivial, but I can't seem to filter by a property not having some value. For example, I'd like to be able to say

EntityClass["Country", "Continent" -> Not["Europe"]]


or something like that.

The documentation for EntityClass lists the following operators to be working with this syntax:

Property values in implicitly defined entity classes may make use of Quantity (and intervals of Quantity) for dimensional values, Between for numeric values, DateObject for dates, TakeLargest and TakeSmallest for ordinal selections, and ContainsAll, ContainsExactly, ContainsAny, ContainsOnly, ContainsNone for entities. Lists of entities are interpreted as ContainsAll, while a single entity is interpreted as ContainsAny[{entity}].

Non of the listed properties looks like it does what I want, i.e. querying for not.

You can use the operator UnequalTo for this purpose:

EntityList @ EntityClass["Country", "Continent"->UnequalTo["Europe"]]


For excluding multiple value, you could use the following syntax:

EntityList @ EntityClass["Country",
{
"Continent"->UnequalTo["Europe"],
"Continent"->UnequalTo["Asia"]
}
] //Length


139

• Fantastic. Thanks. Any idea how to expand that to multiple values, like UnequalTo[{"Europe","Asia"}] or so? It would seem that ContainsNone or a related operator would work, but it doesn't. – Stephan Oct 18 '19 at 15:22
• Just briefly: Which other Operators work with implicit EntityClasses that do not appear in the Documentation? Is there a list, or is it based on some property of the allowed operators? – Stephan Oct 18 '19 at 20:12

## Alternative solution with FilteredEntityClass and EntityFunction

### The code

For the sake of documenting this one more time on this site, a more general answer (w.r.t. the answer of Carl) would be to use EntityFunction and FilteredEntityClass. In this particular case, one additional subtlety is that in this method, one has to compare the "Continent" property with EntityClass["Country", "Europe"], rather than a string "Europe".

Here is the code

nonEuropeanCountries = EntityList @ FilteredEntityClass[
"Country",
EntityFunction[
c,
c["Continent"] =!=  EntityClass["Country", "Europe"]
]
]


### Differences with the operator-based method

Note that this result will be different from that obtained with Carl's code, since this one keeps the countries for which the continent is not defined, while Carl's code throws them away:

Complement[
nonEuropeanCountries,
EntityList @ EntityClass["Country", "Continent" -> UnequalTo["Europe"]]
]

(*
{
Entity["Country", "BouvetIsland"],
Entity["Country", "BritishIndianOceanTerritory"],
Entity["Country", "FrenchSouthernAndAntarcticLands"],
Entity["Country", "SouthGeorgiaAndTheSouthSandwichIslands"],
Entity["Country", "UnitedStatesMinorOutlyingIslands"]
}
*)


Which behavior is more desired, depends on the situation, but it is important to keep in mind this difference. Also, while one can filter such countries out from the above solution, one can't reconstruct those back from the one based on operators.

## More on EntityFunction

In general, EntityFunction is a somewhat more verbose, but also significantly more powerful device, than the short-hand syntax based on rules, since one can create predicates which couple together more than one entity property - which is in general impossible in the short syntax (except cases when the full predicate can be expressed via individual property predicates combined using simple And condition).

Here is an example that would be pretty hard to express in the short syntax, within a single query: find all countries which either are in Europe and their names start with "A", or have population higher than 100 million people. Here is the code for this using EntityFunction:

EntityList @ FilteredEntityClass[
"Country",
EntityFunction[c,
Or[
And[
c["Continent"] ===  EntityClass["Country", "Europe"],
StringStartsQ[c["Name"], "A"]
],
c["Population"] > Quantity[10^8, "People"]
]
]
]

(*

{
Entity["Country", "AlandIslands"], Entity["Country", "Albania"],
Entity["Country", "Andorra"], Entity["Country", "Austria"],
Entity["Country", "China"], Entity["Country", "Ethiopia"],
Entity["Country", "India"], Entity["Country", "Indonesia"],
Entity["Country", "Japan"], Entity["Country", "Mexico"],
Entity["Country", "Nigeria"], Entity["Country", "Pakistan"],
Entity["Country", "Philippines"], Entity["Country", "Russia"],
Entity["Country", "UnitedStates"]
}
*)

• Thanks for taking the time to post another answer. Yes, I knew about FilteredEntityClass and should have mentioned it as an alternative solution that I already knew about. The reason I wanted to know a solution using EntityClass was that I was hoping that that is faster... simply due to the fact that it restricts functionality to a few selected operators which perhaps are better optimized with the DB-backend than the far more general EntityFunction. Am I wrong? – Stephan Oct 18 '19 at 20:09
• For the DB backend this is all the same, the short form is simply compiled to the long one. EntityFunction gets compiled in all cases, for the db backend (we do not support callbacks from db to the kernel, so if we can't compile EntityFunction completely, we throw an error). Trust me, I co-wrote the db compiler :). – Leonid Shifrin Oct 18 '19 at 20:11
• OK, thanks. I do trust you, I've followed some of the livestreams by Stephen Wolfram and listened to you and others on the team about this whole DB-development with much interest... yes, I had too much time. – Stephan Oct 18 '19 at 20:15
• All right. I was just kidding :) – Leonid Shifrin Oct 18 '19 at 20:16
• Congratulations on reaching 100k, Leonid! :) – Andreas Rejbrand Oct 19 '19 at 22:25