Saturday, April 15, 2023

Database filtering blog posts
with logical NOT

Elon Musk with a "no" slash over his face
Adapted from Steve Jurvetson (CC BY 2.0)

Attentive reader Peggy K wishes for a way to make such complex blog searches as "all my posts about Twitter that are NOT about Elon Musk."

(My hypothetical example, not hers.)

Seek no further.

Blogger, the database

A useful insight is that Blogger is a kind of database that uses the labels you define to sort posts into pages by category.

Compound searches like "All my posts that are about both Twitter AND Facebook" are also possible if you have labeled posts accordingly.

"All my posts that are about either Twitter OR Facebook (or both)" is a little fussier, but also doable.

Why NOT

To exclude posts from a search by label, use the AND and NOT operators—a plus sign and a minus sign (or hyphen) -.
Image: Kismalac (public domain)
B excluding A

You are actually modifying an AND search. NOT will not work on its own.

Furthermore, the AND search must take the complex form

myblog.blogspot.com/search/?q=label:label1+​label:label2

not the more straightforward label-search AND I described in "Blogger database superpowers."

On this blog, this url

too-clever-by-half.blogspot.com/search/?q=​label:feeds+label:"things that changed"

finds all posts about feeds that are also about things that changed. It is the basic logical AND search. (The quotes are necessary if the label has any spaces in it, otherwise not.)

Where NOT

To exclude "things that changed," change the operator "+" (which means AND) to "+-" ("AND NOT") as follows:

The result is a page of posts about feeds, except if the post is also labeled "things that changed"—those posts are excluded.

You must use "AND NOT." "NOT" alone will not work.

More syntax

Alternatively, put NOT before the first term and AND before the second:
too-clever-by-half.blogspot.com/search/?q=​-label:feeds+label:"things that changed"

to find all posts about things that changed that are not also about feeds.

Search for text

To search for text within posts, leave off the label: identifier. Note that text searches do not also search labels.

For a search to find, say, all the posts that are not about Elon Musk, you must search for "NOT Elon" AND something that is found in every post. That might be a space or (in the English language anyway) the word "a."

Why: there must be a search term you do find, that defines the universe of posts from which Elon Musk is excluded.

Link to it

AND, OR, and NOT can operate on multiple search terms, and can be combined.

A page they produce updates itself automatically based on labels that you assign and posts that you publish. That is the database superpower.

The link you build to that search page can shared with your readers.

These compound searches do not have feeds. If you need a complex feed, you will have to combine, and exclude, feeds programmatically, or by using a feed reader that supports the operations you want.

Links

5 comments:

  1. Thank you Adam! That's more complicated than I was hoping for, but if it works, it works.

    ReplyDelete
    Replies
    1. My feeling is that if this is genuinely useful, the difficulty in building the link correctly is trivial versus being able to direct readers to the search page in perpetuity.

      I'm not sure what the use case is, though.

      Delete
  2. Abba, you are very kind.

    Best wishes for your blog.

    ReplyDelete
  3. I don't know if you've posted about this already but in case you haven't, there is a way to search for something in posts of a particular label only.

    For example, you have a blog about cars and have set labels for each car company, say, Mercedes, Toyota, Tata, Suzuki, etc. And you want to search for a red car in Toyota only.

    Now, in the search box on the Blogger blog if you search for red cars, then it will return results for entire site, that is which will include results for all labels. That is you will enter following in search box to look for red cars
    - - - -
    red cars
    - - - -

    What the above built in search of Blogger does is to look for the text red cars in all the blog posts.

    Now, coming to the search only for particular label trick,

    But you want to search for red car in Toyota only. This can be achieved by using the following in the search box
    - - - -
    label:"Toyota" red cars
    - - - -

    The above query will return search results for search term red cars under label Toyota only.

    After "Label Name" give one space and then enter your search term, something similar to a site search you do in Google. Search term can be a word or a phrase, as usual.

    Note: Good practice to use "quotes" for all labels, as you have noted above for labels with a space in between two words. Also note that in case you use capitalisation in label names then the same capitalisation has to used while using label:"Label Name" as the label names are case sensitive in Blogger. That is, of your label name is Toyota, then you have to use "Toyota" and if it is toyota then you have to use "toyota".

    The search term that follows it does not need capitalisation and will work anyway like any regular search term will.

    This works for me. Hopefully you and your readers may find this useful too.

    Try it out for searching different things in different labels and then compare results of a usual search done without the label: operator in search box.
    ...

    Regards,
    Pradeep

    ReplyDelete
    Replies
    1. Thank you, @Pradeep! I was not aware one could use the "label" parameter like that in the search box. Using so produces a search url of the "?q=" variety.

      As Pradeep notes, label names are case sensitive in search.

      Delete