Elastic Search - 检查字符串数组是否有任何以单词 "cake" 开头的字符串
Elastic Search - check if array of strings has any string that starts with word "cake"
这些是我的字段:
{"brand" : "A",
"productType" : "food",
"infoList" : [
"pizza-34","cake-qaw-34"
]},
{
"brand" : "B",
"productType" : "food",
"infoList" : [
"pasta-3"
]}
我只希望返回那些在其 infoList 中任何位置以单词“cake”开头的文档。在这种情况下,我应该得到这个
{"brand" : "A",
"productType" : "food",
"infoList" : [
"pizza-34","cake-qaw-34"
]}
谁能告诉我如何实现这个?
PS: 不能为此使用正则表达式
您可以使用 prefix query 来获取其字符串数组中包含任何以单词“cake”开头的字符串的文档
{
"query": {
"prefix": {
"infoList": "cake"
}
}
}
搜索结果将是
"hits": [
{
"_index": "67778291",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"brand": "A",
"productType": "food",
"infoList": [
"pizza-34",
"cake-qaw-34"
]
}
}
]
更新 1:
{
"query": {
"bool": {
"must_not": {
"prefix": {
"infoList": "cake"
}
}
}
}
}
搜索结果将是
"hits": [
{
"_index": "67778291",
"_type": "_doc",
"_id": "2",
"_score": 0.0,
"_source": {
"brand": "B",
"productType": "food",
"infoList": [
"pasta-3"
]
}
}
]
这些是我的字段:
{"brand" : "A",
"productType" : "food",
"infoList" : [
"pizza-34","cake-qaw-34"
]},
{
"brand" : "B",
"productType" : "food",
"infoList" : [
"pasta-3"
]}
我只希望返回那些在其 infoList 中任何位置以单词“cake”开头的文档。在这种情况下,我应该得到这个
{"brand" : "A",
"productType" : "food",
"infoList" : [
"pizza-34","cake-qaw-34"
]}
谁能告诉我如何实现这个?
PS: 不能为此使用正则表达式
您可以使用 prefix query 来获取其字符串数组中包含任何以单词“cake”开头的字符串的文档
{
"query": {
"prefix": {
"infoList": "cake"
}
}
}
搜索结果将是
"hits": [
{
"_index": "67778291",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"brand": "A",
"productType": "food",
"infoList": [
"pizza-34",
"cake-qaw-34"
]
}
}
]
更新 1:
{
"query": {
"bool": {
"must_not": {
"prefix": {
"infoList": "cake"
}
}
}
}
}
搜索结果将是
"hits": [
{
"_index": "67778291",
"_type": "_doc",
"_id": "2",
"_score": 0.0,
"_source": {
"brand": "B",
"productType": "food",
"infoList": [
"pasta-3"
]
}
}
]