Boolean Logic 101

Hello! It’s me, Jay, your friendly neighborhood librarian. (Logseq Librarian? Has a nice ring to it!)

During the Query Learning Sprints, I thought it would be useful to share some basics and tips for Boolean logic. Why learn this from me? Well, in the United States at least, people in library science graduate programs start learning Boolean logic to search catalogs and databases on day one. I’ve been teaching university students, faculty, and staff these same techniques since 2015.

If you’ve learned anything about Boolean logic before, you’ve probably learned it via Boolean algebra and the TRUE or FALSE operators in computer programming.

However, Boolean is so much more than that! For our purposes, Boolean logic and Boolean operators should be thought of in a search context, not necessarily a mathematical or programming context.

The Basics

There are three basic Boolean operators: AND, OR, and NOT. But despite them being basic, their logic can be somewhat counter-intuitive.

(Side note: I always thought a good Halloween costume would be a spooky sheet ghost with AND OR NOT written all over you…a Boooooooooolean Operator.)

AND :heavy_minus_sign:

First up, we have AND.

You might think, “Oh hey, AND. That sounds like it increases my results when searching!”

But AND actually decreases our search/query results. Let me explain.

Imagine you’re searching a library catalog, a search engine, a database, or even your notes. You want to see everything you have about ‘ghosts’, so you search the keyword ghosts. Let’s say you get 100 results from that. That’s a lot! You realize you actually only wanted to see results about ghosts in Shakespeare plays. You want to decrease your results. You search for ghosts AND Shakespeare. Now you only get 20 results.

AND means that all the results in our example must contain the keywords ghosts and Shakespeare. If we had a Venn diagram of items that contain ghosts in one circle and items that contain Shakespeare in the other, our search for ghosts AND Shakespeare would give us results that are where those two circles overlap.

image

OR :heavy_plus_sign:

On the other hand, what if we wanted results that contain ghosts or Shakespeare. That’s what OR is for! OR increases the number of search results. In a Venn diagram, OR brings us the entirety of both circles and where they overlap. This OR is not exclusionary, meaning results can contain both or either. You’ll get the 100 results from just searching ghosts (including the overlaps with Shakespeare), plus any results that are about Shakespeare and not ghosts. In our AND search, you wouldn’t have been able to see results about Romeo and Juliet, for example.

image

NOT :x:

Now you want to see results that are about ghosts but not about Shakespeare. You search ghosts NOT Shakespeare. If our original search for ghosts was 100 results, and if our search for ghosts AND Shakespeare was only 20 notes, then the number of notes about ghosts but not about Shakespeare is 80. In a Venn diagram, this query would be only the part of the ghost circle that didn’t overlap with the Shakespeare circle.

image

I used to work in Salt Lake City, Utah, where a lot of the university students I interacted with were members of the Church of Latter Day Saints, or they were interested in researching it. My favorite example for the NOT operator was wanting to search for library resources about the Book of Mormon, but realizing that search also brought up a lot of results about the Broadway musical The Book of Mormon. So, our search would be "Book of Mormon" NOT musical.

Combining Operators

When we start to get into advanced queries, we’re going to need more than just AND, OR, and NOT. We’re going to need to combine operators.

Returning to our ghosts example, we realize we actually do want to see results about Shakespeare, but we don’t want to see anything about Hamlet or Julius Caesar.

To build our search query, we’ll need to use parenthesis, like in math! This will tell the query how the operators should be combined.

Let’s build our combined query.

  1. We want ghosts: ghosts
  2. We only want the results that are also about Shakespeare: ghosts AND Shakespeare
  3. But we don’t want to see anything about Hamlet: ghosts AND Shakespeare NOT Hamlet
  4. And we don’t want to see anything about Julius Caesar, either. It would make sense for our query to be ghosts AND Shakespeare NOT Hamlet OR Julius Caesar right?

Nope!

Think about the math problem 1 + 2 x 3. Is the answer 9? 1 + 2, and then 3 x 3? No, it’s 7. 2 x 3, and then 6 +1. An easier way to write this equation is 1 + (2 x 3). The parentheses let us know which part of the equation to do first.

In Boolean, parentheses do the same thing. Let’s revisit what we want to know. We want results about ghosts and Shakespeare, but we don’t want to see anything about Hamlet or Julius Caesar, which are both plays by Shakespeare.

Let’s start with those two plays: Hamlet OR "Julius Caesar".

The quotation marks around Julius Caesar makes sure the search is for those two words as a phrase. (In most search indices, the search Julius Caesar is actually searching Julius AND Caesar, where both those words have to appear in the search result, but not necessarily next to each other or in that order! You might get a result about a mall that has an Orange Julius and a Little Caesar’s.)

Julius Caesar, however, is a real historical figure, and there might be cool ghost stuff about him! So, we need to make sure the Julius Caesar stuff we get is only about the Shakespeare play.

For this, our search would be Shakespeare NOT (Hamlet OR "Julius Caesar").

And there’s our first set of parentheses! This is making sure our search is getting us results about Shakespeare, but not Hamlet or Julius Caesar.

Finally, we need to bring ghosts back in, since that’s what we really care about. ghosts AND (Shakespeare NOT (Hamlet OR "Julius Caesar"))

You’ll notice we put the previous query we did in its own set of parentheses. That tells the query that Shakespeare and NOT (Hamlet OR "Julius Caesar") are related. This means we can still get stuff about Julius Caesar the person. So, whatever results we get here, we can then make sure it’s also only stuff about ghosts.

For this final query, our Venn diagram would be four circles. We have our Shakespeare circle. Our Hamlet circle partially overlaps our Shakespeare circle, and so does our Julius Caesar circle. All three circles partially overlap with our ghosts circle. If we imagine our results as the filled in part of our diagram, then only the part of our circle where ghosts and Shakespeare overlap is filled in. But within the Shakespeare overlapping part, only the part that doesn’t overlap with Hamlet or Julius Caesar will be filled in.

Conclusion

In Logseq queries, think of them like you’re searching Google (or DuckDuckGo preferably) or a database like JSTOR. You want to find your notes which have certain “keywords”. Knowing how to structure those queries with Boolean logic can allow you to do more complicated queries, and also to get more specific.

An example from my own graph. I want to see things in my digital garden that I have published. However, in my digital garden, I also have a Zettelkasten. So, I want to see my pages and blocks with the property published (or not draft), and also the property Digital Garden or Zettelkasten:

{{query (and (or (property areas Zettelkasten) (property areas [[Digital Garden]])) (property status published))}}

Additional resources

15 Likes

This was VERY well written and explained! I’ve never been able to get the full gist of Boolean operators before. Excellent job! Bookmarking this for future reference.

1 Like

Great article, Jay! :clap: I’ve created simple Venn diagrams in Excalidraw, I hope they’ll make things even more clear. I’ll probably also turn your examples into actual queries and post the screenshots here later.

3 Likes