开发者社区 问答 正文

使用通配符的ElasticSearch LIKE查询

我正在尝试为JSON以下实现类似的功能。

SELECT * FROM TABLE_NAME WHERE ORGID =“ BACKENDORG” AND TYPE =“ AUDIT” AND 
(sessionId LIKE'%16ECA064B298356B%'或loginUserId LIKE'%16ECA064B298356B%'或txnId LIKE'%16ECA064B298356B%');

{
  "sessionId": "16ECA064B298356B",
  "message": "2019-12-03T05:29:13.217Z [http-nio-8080-exec-4] INFO  http-nio-8080-exec-4 QueryController backendorg 16CFAFCCFB14D9A3 16ECA064B298356B 16ECA3A4EFA026BF 
  "type": "audit",
  "orgId": "backendorg",
  "loginUserId": "16CFAFCCFB14D9A3",
  "txnId": "16ECA3A4EFA026BF"
 }

试图使用BoolQueryBuilder编写一个LIKE查询,这是我的查询

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "orgId": {
              "value": "backendorg",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "type": {
              "value": "audit",
              "boost": 1
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "wildcard": {
                  "sessionId": {
                    "wildcard": "16ECA064B298356B",
                    "boost": 1
                  }
                }
              },
              {
                "wildcard": {
                  "loginUserId": {
                    "wildcard": "16ECA064B298356B",
                    "boost": 1
                  }
                }
              },
              {
                "wildcard": {
                  "txnId": {
                    "wildcard": "16ECA064B298356B",
                    "boost": 1
                  }
                }
              }
            ],
            "adjust_pure_negative": true,
            "minimum_should_match": "1",
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

上面的查询应返回上面的JSON结果,但显示为零。如果有人指出这个问题,将会很有帮助。

展开
收起
垚tutu 2019-12-04 16:33:34 916 分享 版权
阿里云 AI 助理回答

你的查询逻辑看起来是正确的,但是问题可能出在你使用wildcard查询的方式上。在Elasticsearch中,当你使用wildcard时,通常是在一个字段值的前后添加通配符(如*),而不是直接提供一个完整的值去匹配。然而,从你的SQL转换到Elasticsearch查询意图来看,你实际上想要实现的是对字段值包含特定字符串的搜索,这更接近于使用match_phrase_prefix或者构建一个带有prefix查询。

但考虑到你希望实现类似LIKE '%substring%'的功能,即在字符串任意位置查找子串,最直接的方法是使用query_string查询或者simple_query_string查询,它们允许你在字段值中进行模糊匹配,包括前缀、后缀或中间部分的匹配。

修改后的查询示例如下:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "orgId": {
              "value": "backendorg",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "type": {
              "value": "audit",
              "boost": 1
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "query_string": {
                  "query": "*16ECA064B298356B*",
                  "fields": ["sessionId", "loginUserId", "txnId"],
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "minimum_should_match": 1,
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

这个查询会检查sessionIdloginUserIdtxnId字段中是否包含"16ECA064B298356B"这个子串,无论它出现在值的任何位置。请确保你的Elasticsearch索引映射设置支持这些字段的全文搜索,如果有必要,可以将这些字段的类型设置为text以启用更丰富的文本搜索功能。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答