elasticsearch - Elasticsearch 一个数组至少包含一个元素
问题描述
我正在尝试使用弹性搜索按标签搜索食谱
{
...
"tag": [
"cool",
"cooler"
]
},
{
...
"tag": [
"cool",
"hard"
]
},
{
...
"tag": [
"coolest",
"hardest"
]
},
我想搜索所有包含“酷”标签的实体
我尝试了什么:
GET /recipes/_search
{
"query": {
"terms": {
"tag": ["cool"]
}
}
}
什么都不退
GET /recipes/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"tag": "cool"
}
}
]
}
}
}
什么都不退
GET /recipes/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"tag": "cool"
}
}
]
}
}
}
返回所有实体
如何仅检索包含“酷”和可能其他值但规则是标签必须存在于标签数组中的项目
解决方案
本解决方案使用脚本查询脚本查询
GET /recipes/_search
{
"query": {
"bool": {
"must": {
"script": {
"script": {
"source": "doc['tag'] instanceof List && doc['tag'][0].contains('cool')"
}
}
}
}
}
}
推荐阅读
- spring-boot - Kotlin Spring Boot Dependency Injection
- typescript - What is the idiomatic way in TypeScript to extend an object (already implementing an interface) to a wider interface?
- java - Spring Webflux 如何在不阻塞的情况下顺序调用 2 个不同的服务?
- swift - Equivalent of a popover in SpriteKit
- php - Apache 2.4, PHP 7.4.7, Windows 10 can't load cURL Extension from Apache Service but loads fine from Command Line with httpd
- node.js - 404 error when connecting to IP address but not in domain name
- python - pywinauto: get URL from MS Edge Canary Address Bar
- python - tkinter filedialog save and docx tpl
- python - Need to reference a variable before it is called in Python Flask
- wso2 - WSO2 IS - Analytics : `Error while SSL handshake` error