【问题标题】:Elastic Search - check if array of strings has any string that starts with word "cake"弹性搜索 - 检查字符串数组是否有任何以单词“cake”开头的字符串
【发布时间】:2021-05-31 17:48:18
【问题描述】:

这些是我的字段:

      {"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:不能为此使用正则表达式

【问题讨论】:

    标签: elasticsearch elastic-stack elasticsearch-5


    【解决方案1】:

    您可以使用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"
              ]
            }
          }
        ]
    

    【讨论】:

    • 是的@Meg,您可以将must_not 子句与前缀查询一起使用。请浏览答案的更新部分
    • 感谢您的帮助!我的意思是问如何获取包含除蛋糕以外的所有文件的文件。它也可以包含蛋糕。例如:{“brand”:“A”,“productType”:“food”,“infoList”:[“pizza-34”,“cake-qaw-34”]},{“brand”:“B”,“ productType" : "food", "infoList" : [ "pasta-3", "cake-45" ]} 这两个都应该返回,因为它们都有除了 cake 之外的单词。如果没有蛋糕,也应该退回
    • @Meg 我可以知道你为什么不接受答案吗?
    • 你能帮我解决上述情况吗
    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 2019-10-11
    • 2022-06-16
    • 2023-01-01
    • 2016-12-05
    • 2011-05-04
    • 2019-11-03
    • 1970-01-01
    相关资源
    最近更新 更多