jpskill.com
🛠️ 開発・MCP コミュニティ

querying-logseq-data

Expert in building Datalog queries for Logseq DB graphs. Auto-invokes when users need help writing Logseq queries, understanding Datalog syntax, optimizing query performance, or working with the Datascript query engine. Covers advanced query patterns, pull syntax, aggregations, and DB-specific query techniques.

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o querying-logseq-data.zip https://jpskill.com/download/17703.zip && unzip -o querying-logseq-data.zip && rm querying-logseq-data.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/17703.zip -OutFile "$d\querying-logseq-data.zip"; Expand-Archive "$d\querying-logseq-data.zip" -DestinationPath $d -Force; ri "$d\querying-logseq-data.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して querying-logseq-data.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → querying-logseq-data フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
2

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

Logseqデータのクエリ

このSkillを使用する場面

このSkillは、以下の場合に自動的に起動します。

  • ユーザーがLogseq用のDatalogクエリを構築したい場合
  • :find:where:in句に関する質問
  • pull構文に関する質問(pull ?e [*]
  • クエリの最適化またはパフォーマンスの問題
  • 集計クエリ(count、sum、avg、min、max)
  • ルール定義または再利用可能なクエリロジック
  • 単純なクエリ構文から完全なDatalogへの変換
  • ユーザーがLogseqのコンテキストで「Datalog」、「query」、「datascript」に言及した場合

参考資料: 一般的なクエリの例については、{baseDir}/references/query-patterns.mdを参照してください。

あなたは、Logseqのデータベースベースのグラフに対するDatalogクエリのエキスパートです。

Datalogクエリの基礎

基本的なクエリ構造

[:find ?variable          ; 何を返すか
 :in $ ?input-var         ; 入力($ = データベース)
 :where                   ; 条件
 [?entity :attribute ?value]]

Findの指定

;; すべての一致をタプルとして返す
[:find ?title ?author ...]

;; コレクションとして返す(単一の変数)
[:find [?title ...] ...]

;; 単一の値を返す
[:find ?title . ...]

;; 単一のタプルを返す
[:find [?title ?author] ...]

;; エンティティデータをpullする
[:find (pull ?e [*]) ...]
[:find (pull ?e [:block/title :block/tags]) ...]

一般的なクエリパターン

すべてのページを検索する

[:find (pull ?p [*])
 :where
 [?p :block/tags ?t]
 [?t :db/ident :logseq.class/Page]]

特定のタグ/クラスを持つブロックを検索する

[:find (pull ?b [*])
 :where
 [?b :block/tags ?t]
 [?t :block/title "Book"]]

プロパティ値で検索する

;; 完全一致
[:find (pull ?b [*])
 :where
 [?b :user.property/author "Stephen King"]]

;; 変数バインディングを使用
[:find ?title ?author
 :where
 [?b :block/title ?title]
 [?b :user.property/author ?author]
 [?b :block/tags ?t]
 [?t :block/title "Book"]]

ステータスでタスクを検索する

[:find (pull ?t [*])
 :where
 [?t :block/tags ?tag]
 [?tag :db/ident :logseq.class/Task]
 [?t :logseq.property/status ?s]
 [?s :block/title "In Progress"]]

日付範囲で検索する

;; 今週期限のタスク
[:find (pull ?t [*])
 :in $ ?start ?end
 :where
 [?t :block/tags ?tag]
 [?tag :db/ident :logseq.class/Task]
 [?t :logseq.property/deadline ?d]
 [(>= ?d ?start)]
 [(<= ?d ?end)]]

高度なクエリテクニック

集計

;; 著者ごとの書籍数をカウントする
[:find ?author (count ?b)
 :where
 [?b :block/tags ?t]
 [?t :block/title "Book"]
 [?b :user.property/author ?author]]

;; Sum, min, max, avg
[:find (sum ?rating) (avg ?rating) (min ?rating) (max ?rating)
 :where
 [?b :block/tags ?t]
 [?t :block/title "Book"]
 [?b :user.property/rating ?rating]]

ルール(再利用可能なクエリロジック)

;; ルールを定義する
[[(has-tag ?b ?tag-name)
  [?b :block/tags ?t]
  [?t :block/title ?tag-name]]

 [(is-task ?b)
  [?b :block/tags ?t]
  [?t :db/ident :logseq.class/Task]]]

;; クエリでルールを使用する
[:find (pull ?b [*])
 :in $ %
 :where
 (has-tag ?b "Important")
 (is-task ?b)]

否定

;; 評価のない書籍を検索する
[:find (pull ?b [*])
 :where
 [?b :block/tags ?t]
 [?t :block/title "Book"]
 (not [?b :user.property/rating _])]

Or句

;; 優先度の高いタスクまたは期限切れのタスクを検索する
[:find (pull ?t [*])
 :in $ ?today
 :where
 [?t :block/tags ?tag]
 [?tag :db/ident :logseq.class/Task]
 (or
   [?t :logseq.property/priority "High"]
   (and
     [?t :logseq.property/deadline ?d]
     [(< ?d ?today)]))]

再帰クエリ

;; ブロックのすべての子孫を検索する
[[(descendant ?parent ?child)
  [?child :block/parent ?parent]]
 [(descendant ?parent ?child)
  [?child :block/parent ?p]
  (descendant ?parent ?p)]]

[:find (pull ?c [*])
 :in $ % ?root-id
 :where
 [?root :block/uuid ?root-id]
 (descendant ?root ?c)]

Pull構文

選択的な属性

;; 特定の属性
(pull ?e [:block/title :block/tags])

;; 参照のネストされたpull
(pull ?e [:block/title {:block/tags [:block/title]}])

;; すべての属性
(pull ?e [*])

;; ネストされた結果を制限する
(pull ?e [:block/title {:block/children [:block/title] :limit 5}])

逆参照

;; このエンティティを参照するすべてのブロックを検索する
(pull ?e [:block/title {:block/_refs [:block/title]}])

DB固有のクエリパターン

クラスの操作

;; すべてのクラスを検索する(それ自体がTagとしてタグ付けされたタグ)
[:find (pull ?c [*])
 :where
 [?c :block/tags ?t]
 [?t :db/ident :logseq.class/Tag]]

;; クラス階層を検索する
[:find ?parent-name ?child-name
 :where
 [?child :logseq.property.class/extends ?parent]
 [?child :block/title ?child-name]
 [?parent :block/title ?parent-name]]

プロパティの操作

;; すべてのユーザー定義プロパティを検索する
[:find (pull ?p [*])
 :where
 [?p :block/tags ?t]
 [?t :db/ident :logseq.class/Property]
 [?p :db/ident ?ident]
 [(clojure.string/starts-with? (str ?ident) ":user.property")]]

;; 型付きのプロパティ値を検索する
[:find ?prop-name ?type
 :where
 [?p :block/tags ?t]
 [?t :db/ident :logseq.class/Property]
 [?p :block/title ?prop-name]
 [?p :logseq.property/type ?type]]

ジャーナルクエリ

;; すべてのジャーナルページを検索する
[:find (pull ?j [*])
 :where
 [?j :block/tags ?t]
 [?t :db/ident :logseq.class/Journal]]

;; 特定の日のジャーナルを検索する
[:find (pull ?j [*])
 :in $ ?date-str
 :where
 [?j :block/tags ?t]
 [?t :db/ident :logseq.class/Journal]
 [?j :block/title ?date-str]]

クエリ最適化のヒント

  1. 最も選択的な句を最初に配置する - 早めに結果を絞り込む
  2. インデックス付き属性を使用する - :db/ident:block/uuidはインデックス化されています
  3. pullでワイルドカードを避ける - 必要な属性を指定する
  4. 複雑なロジックにはルールを使用する - 可読性が向上し、キャッシュの可能性が高まります
  5. 可能な場合は結果を制限する - 大規模なデータセットには制限を追加する

;; 最適化されたクエリの例
[:find (pull ?b [:block/title :user.property/rating])
 :in $ ?min-rating
 :where
 ;; 最も選択的なものを最初に
 [?b :user.property/rating ?r]
 [(>= ?r ?min-rating)]
 ;; 次にタグでフィルタリング
 [?b :

(原文がここで切り詰められています)
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Querying Logseq Data

When to Use This Skill

This skill auto-invokes when:

  • User wants to build a Datalog query for Logseq
  • Questions about :find, :where, :in clauses
  • Pull syntax questions (pull ?e [*])
  • Query optimization or performance issues
  • Aggregation queries (count, sum, avg, min, max)
  • Rule definitions or reusable query logic
  • Converting simple query syntax to full Datalog
  • User mentions "Datalog", "query", "datascript" with Logseq context

Reference Material: See {baseDir}/references/query-patterns.md for common query examples.

You are an expert in Datalog queries for Logseq's database-based graphs.

Datalog Query Fundamentals

Basic Query Structure

[:find ?variable          ; What to return
 :in $ ?input-var         ; Inputs ($ = database)
 :where                   ; Conditions
 [?entity :attribute ?value]]

Find Specifications

;; Return all matches as tuples
[:find ?title ?author ...]

;; Return as collection (single variable)
[:find [?title ...] ...]

;; Return single value
[:find ?title . ...]

;; Return single tuple
[:find [?title ?author] ...]

;; Pull entity data
[:find (pull ?e [*]) ...]
[:find (pull ?e [:block/title :block/tags]) ...]

Common Query Patterns

Find All Pages

[:find (pull ?p [*])
 :where
 [?p :block/tags ?t]
 [?t :db/ident :logseq.class/Page]]

Find Blocks with Specific Tag/Class

[:find (pull ?b [*])
 :where
 [?b :block/tags ?t]
 [?t :block/title "Book"]]

Find by Property Value

;; Exact match
[:find (pull ?b [*])
 :where
 [?b :user.property/author "Stephen King"]]

;; With variable binding
[:find ?title ?author
 :where
 [?b :block/title ?title]
 [?b :user.property/author ?author]
 [?b :block/tags ?t]
 [?t :block/title "Book"]]

Find Tasks by Status

[:find (pull ?t [*])
 :where
 [?t :block/tags ?tag]
 [?tag :db/ident :logseq.class/Task]
 [?t :logseq.property/status ?s]
 [?s :block/title "In Progress"]]

Find with Date Ranges

;; Tasks due this week
[:find (pull ?t [*])
 :in $ ?start ?end
 :where
 [?t :block/tags ?tag]
 [?tag :db/ident :logseq.class/Task]
 [?t :logseq.property/deadline ?d]
 [(>= ?d ?start)]
 [(<= ?d ?end)]]

Advanced Query Techniques

Aggregations

;; Count books by author
[:find ?author (count ?b)
 :where
 [?b :block/tags ?t]
 [?t :block/title "Book"]
 [?b :user.property/author ?author]]

;; Sum, min, max, avg
[:find (sum ?rating) (avg ?rating) (min ?rating) (max ?rating)
 :where
 [?b :block/tags ?t]
 [?t :block/title "Book"]
 [?b :user.property/rating ?rating]]

Rules (Reusable Query Logic)

;; Define rules
[[(has-tag ?b ?tag-name)
  [?b :block/tags ?t]
  [?t :block/title ?tag-name]]

 [(is-task ?b)
  [?b :block/tags ?t]
  [?t :db/ident :logseq.class/Task]]]

;; Use rules in query
[:find (pull ?b [*])
 :in $ %
 :where
 (has-tag ?b "Important")
 (is-task ?b)]

Negation

;; Find books without rating
[:find (pull ?b [*])
 :where
 [?b :block/tags ?t]
 [?t :block/title "Book"]
 (not [?b :user.property/rating _])]

Or Clauses

;; Find high priority or overdue tasks
[:find (pull ?t [*])
 :in $ ?today
 :where
 [?t :block/tags ?tag]
 [?tag :db/ident :logseq.class/Task]
 (or
   [?t :logseq.property/priority "High"]
   (and
     [?t :logseq.property/deadline ?d]
     [(< ?d ?today)]))]

Recursive Queries

;; Find all descendants of a block
[[(descendant ?parent ?child)
  [?child :block/parent ?parent]]
 [(descendant ?parent ?child)
  [?child :block/parent ?p]
  (descendant ?parent ?p)]]

[:find (pull ?c [*])
 :in $ % ?root-id
 :where
 [?root :block/uuid ?root-id]
 (descendant ?root ?c)]

Pull Syntax

Selective Attributes

;; Specific attributes
(pull ?e [:block/title :block/tags])

;; Nested pulling for refs
(pull ?e [:block/title {:block/tags [:block/title]}])

;; All attributes
(pull ?e [*])

;; Limit nested results
(pull ?e [:block/title {:block/children [:block/title] :limit 5}])

Reverse References

;; Find all blocks referencing this entity
(pull ?e [:block/title {:block/_refs [:block/title]}])

DB-Specific Query Patterns

Working with Classes

;; Find all classes (tags that are themselves tagged as Tag)
[:find (pull ?c [*])
 :where
 [?c :block/tags ?t]
 [?t :db/ident :logseq.class/Tag]]

;; Find class hierarchy
[:find ?parent-name ?child-name
 :where
 [?child :logseq.property.class/extends ?parent]
 [?child :block/title ?child-name]
 [?parent :block/title ?parent-name]]

Working with Properties

;; Find all user-defined properties
[:find (pull ?p [*])
 :where
 [?p :block/tags ?t]
 [?t :db/ident :logseq.class/Property]
 [?p :db/ident ?ident]
 [(clojure.string/starts-with? (str ?ident) ":user.property")]]

;; Find property values with type
[:find ?prop-name ?type
 :where
 [?p :block/tags ?t]
 [?t :db/ident :logseq.class/Property]
 [?p :block/title ?prop-name]
 [?p :logseq.property/type ?type]]

Journal Queries

;; Find all journal pages
[:find (pull ?j [*])
 :where
 [?j :block/tags ?t]
 [?t :db/ident :logseq.class/Journal]]

;; Find journal for specific date
[:find (pull ?j [*])
 :in $ ?date-str
 :where
 [?j :block/tags ?t]
 [?t :db/ident :logseq.class/Journal]
 [?j :block/title ?date-str]]

Query Optimization Tips

  1. Put most selective clauses first - Narrow down results early
  2. Use indexed attributes - :db/ident, :block/uuid are indexed
  3. Avoid wildcards in pull - Specify needed attributes
  4. Use rules for complex logic - Better readability and potential caching
  5. Limit results when possible - Add limits for large datasets
;; Optimized query example
[:find (pull ?b [:block/title :user.property/rating])
 :in $ ?min-rating
 :where
 ;; Most selective first
 [?b :user.property/rating ?r]
 [(>= ?r ?min-rating)]
 ;; Then filter by tag
 [?b :block/tags ?t]
 [?t :block/title "Book"]]

Logseq Query UI vs Raw Datalog

Simple Query (UI)

{{query (and [[Book]] (property :rating 5))}}

Equivalent Datalog

[:find (pull ?b [*])
 :where
 [?b :block/tags ?t]
 [?t :block/title "Book"]
 [?b :user.property/rating 5]]

Advanced Query Block

#+BEGIN_QUERY
{:title "5-Star Books"
 :query [:find (pull ?b [*])
         :where
         [?b :block/tags ?t]
         [?t :block/title "Book"]
         [?b :user.property/rating 5]]
 :result-transform (fn [result] (sort-by :block/title result))
 :view (fn [rows] [:ul (for [r rows] [:li (:block/title r)])])}
#+END_QUERY

Common Gotchas

  1. MD vs DB attribute differences

    • MD: :block/content, :block/name
    • DB: :block/title, :block/tags
  2. Property namespacing

    • User properties: :user.property/name
    • System properties: :logseq.property/name
  3. Tag vs Class terminology

    • In UI: "Tags"
    • In schema: "Classes" (:logseq.class/*)
  4. Date handling

    • Dates link to journal pages
    • Compare using date functions, not strings

同梱ファイル

※ ZIPに含まれるファイル一覧。`SKILL.md` 本体に加え、参考資料・サンプル・スクリプトが入っている場合があります。