【问题标题】:Elasticsearch: Search in an array of JSONsElasticsearch:在 JSON 数组中搜索
【发布时间】:2017-03-20 00:39:57
【问题描述】:

我将 Elasticsearch 与 python 库一起使用,当对象变得有点复杂时,我在使用搜索查询时遇到了问题。我的索引中有这样的对象:

{
   "id" : 120,
   "name": bob,
   "shared_status": {
       "post_id": 123456789,
       "text": "This is a sample",
       "urls" : [
           {
              "url": "http://test.1.com",
              "displayed_url": "test.1.com" 
           },
           {
              "url": "http://blabla.com",
              "displayed_url": "blabla.com" 
           }
       ]
   }
}

现在我想做一个查询,仅当其中一个显示的 URL 中有一个子字符串“test”并且主文档中有一个字段“text”时才会返回这个文档。所以我做了这个查询:

{
   "query": {
       "bool": {
           "must": [
                    {"exists": {"field": "text"}}
                   ]
           }
        }
   }
}

但我不知道要为该部分添加什么查询:one of the displayed URL's a substring "test"

这可能吗?列表上的迭代是如何工作的?

【问题讨论】:

  • 有可能。请提供 _mapping 的输出:elastic.co/guide/en/elasticsearch/reference/current/…
  • 问题是关于查询弹性搜索而不是在 JSON 上迭代
  • @rahulroc 我用一个假的例子简化了情况,但我的应用程序是关于来自 API twitter 的推文。而且我的映射中没有为这些字段定义映射。
  • @mel 由于您没有提供显式映射,ES 假设一些基于输入数据的映射类型,答案中的更多详细信息

标签: elasticsearch full-text-search


【解决方案1】:

如果您没有为架构定义显式映射,elasticsearch 会根据数据输入创建默认映射。

由于您不需要 urldisplayed_url 之间的任何关联,因此当前架构可以正常工作。

您可以使用match query 进行全文匹配

GET _search
{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "text"
          }
        },
        {
          "match": {
            "urls.displayed_url": "test"
          }
        }
      ]
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多