使用elasticsearch查询动态属性或数组中的对象

时间:2015-02-25 16:53:25

标签: elasticsearch

出于某种原因,我根本无法在弹性搜索中做到这一点(我很难理解一些文档;感觉有15种方法可以这样做,我无法弄清楚它们中的任何一个)但是说我有以下对象:

{
    "name": "Me",
    "favorites": [
        { title: "Stackoverflow", url: "www.stackoverflow.com" },
        { title: "Google", url: "www.google.com" }
    ],
    "stuff": {
        "fdjalja": { "property": "yes" },
        "nvbdfgd": { "property": "no" }
    }
}
  1. 如何查询数组内部,例如title ==“Google”?我知道我可以轻松地打击嵌套对象,但只是添加一个点但是当它的数组时我无法理解它。

  2. 如何查询密钥是动态的东西对象?所以我可以搜索属性==“是”?

1 个答案:

答案 0 :(得分:0)

我得到nested query这样的工作:

请注意,我将"type":"nested"放在映射中:

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "doc": {
         "properties": {
            "favorites": {
               "type": "nested", 
               "properties": {
                  "title": {
                     "type": "string"
                  },
                  "url": {
                     "type": "string"
                  }
               }
            },
            "name": {
               "type": "string"
            },
            "stuff": {
               "properties": {
                  "fdjalja": {
                     "properties": {
                        "property": {
                           "type": "string"
                        }
                     }
                  },
                  "nvbdfgd": {
                     "properties": {
                        "property": {
                           "type": "string"
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

然后添加了文档:

PUT /test_index/doc/1
{
    "name": "Me",
    "favorites": [
        { "title": "Stackoverflow", "url": "www.stackoverflow.com" },
        { "title": "Google", "url": "www.google.com" }
    ],
    "stuff": {
        "fdjalja": { "property": "yes" },
        "nvbdfgd": { "property": "no" }
    }
}

然后"favorites.title"上的嵌套匹配会返回doc:

POST /test_index/_search
{
   "query": {
      "nested": {
         "path": "favorites",
         "query": {
            "match": {
               "favorites.title": "google"
            }
         }
      }
   }
}
...
{
   "took": 46,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1.4054651,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 1.4054651,
            "_source": {
               "name": "Me",
               "favorites": [
                  {
                     "title": "Stackoverflow",
                     "url": "www.stackoverflow.com"
                  },
                  {
                     "title": "Google",
                     "url": "www.google.com"
                  }
               ],
               "stuff": {
                  "fdjalja": {
                     "property": "yes"
                  },
                  "nvbdfgd": {
                     "property": "no"
                  }
               }
            }
         }
      ]
   }
}

您可以使用以下路径查询动态非数组属性:

POST /test_index/_search
{
   "query": {
      "match": {
         "stuff.fdjalja.property": "yes"
      }
   }
}