You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hyperledger Composer Query Language类SQL LIKE通配符过滤实现求助

Hyperledger Composer Query Language: 实现类似SQL LIKE的通配符查询

Hey there, let's figure out how to get wildcard filtering working in Hyperledger Composer Query Language—since you mentioned CONTAINS wasn't behaving as expected, I'll break down the right approaches to replicate SQL's LIKE functionality.

Why CONTAINS might not have worked

First, quick clarification: CONTAINS is designed for exact substring matches, not wildcard patterns. If you tried using CONTAINS('%Smith%'), that's why it failed—CONTAINS takes a plain substring (like 'Smith') instead of a string with % wildcards. While fullName CONTAINS 'Smith' will find entries where "Smith" appears anywhere in the name, it's less flexible than LIKE for pattern matching (like starting/ending with a string).

1. Use the LIKE operator (closest to SQL's LIKE)

Composer Query Language supports the LIKE operator with two standard wildcards:

  • %: Matches any sequence of characters (including an empty sequence)
  • _: Matches exactly one single character

Let's use an example asset to show how this works. Suppose you have a Person asset defined like this:

asset Person identified by personId {
  o String personId
  o String fullName
  o Integer age
}

Here are practical query examples:

  • Find all people with "Smith" anywhere in their name:
    query findPeopleWithSmithInName {
      description: "Retrieve all people whose full name contains 'Smith'"
      statement:
        SELECT org.example.Person
          WHERE fullName LIKE '%Smith%'
    }
    
  • Find people whose name starts with "John":
    query findPeopleNamedJohnPrefix {
      description: "Retrieve all people whose full name starts with 'John'"
      statement:
        SELECT org.example.Person
          WHERE fullName LIKE 'John%'
    }
    
  • Find people whose name ends with "son":
    query findPeopleNamedSonSuffix {
      description: "Retrieve all people whose full name ends with 'son'"
      statement:
        SELECT org.example.Person
          WHERE fullName LIKE '%son'
    }
    
  • Find people where the second character of their name is "o":
    query findPeopleWithSecondCharO {
      description: "Retrieve all people whose full name has 'o' as the second character"
      statement:
        SELECT org.example.Person
          WHERE fullName LIKE '_o%'
    }
    

2. Use MATCHES for regex-based filtering

If you need more complex pattern matching (like case-insensitive searches or advanced regex patterns), use the MATCHES operator. It supports standard JavaScript regular expressions.

Example queries:

  • Case-insensitive search for names starting with "j":
    query findPeopleStartingWithJCaseInsensitive {
      description: "Retrieve all people whose full name starts with 'J' (case-insensitive)"
      statement:
        SELECT org.example.Person
          WHERE fullName MATCHES '(?i)^j.*'
    }
    
  • Find names with exactly 5 characters:
    query findPeopleWith5CharName {
      description: "Retrieve all people whose full name is exactly 5 characters long"
      statement:
        SELECT org.example.Person
          WHERE fullName MATCHES '^.{5}$'
    }
    

Quick notes to keep in mind

  • Make sure you're using a Composer version that supports these operators (v0.19.0 and later should work fine)
  • Always use single quotes around your pattern strings in the query
  • For MATCHES, stick to JavaScript regex syntax—you can test your regex patterns in a JS console if you're unsure

Hope these examples get your wildcard filtering up and running smoothly! If you hit snags with your specific asset definitions or queries, feel free to share those details for more targeted help.

内容的提问来源于stack exchange,提问作者Jeson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:38:15