dongmei1828 2015-09-07 14:22
浏览 17

弹性搜索有可能吗?

Below is sample format of my JSON documents which is stored inside elasticsearch.

{
    "_index": "in22",
    "_type": "event",
    "_id": "ET00009709",
    "_version": 1,
    "_score": 1,
    "_source": {
        "Group": "Event",
        "Title": "Jurassic World",
        "_boost": 3,
        "inner_hits": [
            {
            "Code": "ET00009709",
            "IsDefault": "",
            "Language": "English",
            "Format": "3D",
            "Region": "MUMBAI"
        },
        {
            "Code": "ET00009710",
            "IsDefault": "Y",
            "Language": "English",
            "Format": "2D",
            "Region": "CHEN"
        },
        {
            "Code": "ET00009713",
            "IsDefault": "",
            "Language": "Hindi",
            "Format": "2D",
            "Region": "MUMBAI"
        },
        {
            "Code": "ET00009714",
            "IsDefault": "",
            "Language": "Tamil",
            "Format": "3D",
            "Region": "MUMBAI"
        },
        {
            "Code": "ET00009715",
            "IsDefault": "",
            "Language": "Hindi",
            "Format": "3D",
            "Region": "MUMBAI"
        },
        {
            "Code": "ET00009716",
            "IsDefault": "",
            "Language": "Bengali",
            "Format": "2D",
            "Region": "MUMBAI"
        }
        ]
    }
}

Now what I want to achieve is whenever I search for Title=Jurassic World and region=MUMBAI, I should get the above document but inner_hits should not contain

{
                "Code": "ET00009710",
                "IsDefault": "Y",
                "Language": "English",
                "Format": "2D",
                "Region": "CHEN"
 }

So is this achievable?

What I tries so far is

{
  "query": {
    "nested": {
      "path": "inner_hits",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "inner_hits.Region": "MUMBAI"
              }
            }
          ]
        }
      }
    }
  }
}

But I couldn't achieve what i wanted, it didn't at all removed that block which contains Region: Chen

If anyone knows how to work around with this, please share. Thanks

  • 写回答

1条回答 默认 最新

  • dsc56927 2015-09-07 15:12
    关注

    one work around is to use the inner_hit feature added in elasticsearch 1.5. This returns the nested documents which matched the nested query.

    Example:

       {
       "_source": {
          "exclude": ["inner_hits.*"]
       },
       "query": {
          "nested": {
             "path": "inner_hits",
             "query": {
                "bool": {
                   "must": [
                      {
                         "match": {
                            "inner_hits.Region": "MUMBAI"
                         }
                      }
                   ]
                }
             },
             "inner_hits": {
                  "size" : 10
              }
          }
       }
    }
    

    Response

    "hits": [
         {
            "_index": "test",
            "_type": "test",
            "_id": "1",
            "_score": 3.8630462,
            "_source": {
               "Group": "Event",
               "_boost": 3,
               "inner_hits": [],
               "Title": "Jurassic World"
            },
            "inner_hits": {
               "inner_hits": {
                  "hits": {
                     "total": 2,
                     "max_score": 3.8630462,
                     "hits": [
                        {
                           "_index": "test",
                           "_type": "test",
                           "_id": "1",
                           "_nested": {
                              "field": "inner_hits",
                              "offset": 2
                           },
                           "_score": 3.8630462,
                           "_source": {
                              "Code": "ET00009713",
                              "IsDefault": "",
                              "Language": "Hindi",
                              "Format": "2D",
                              "Region": "MUMBAI"
                           }
                        },
                        {
                           "_index": "test",
                           "_type": "test",
                           "_id": "1",
                           "_nested": {
                              "field": "inner_hits",
                              "offset": 0
                           },
                           "_score": 3.8630462,
                           "_source": {
                              "Code": "ET00009709",
                              "IsDefault": "",
                              "Language": "English",
                              "Format": "3D",
                              "Region": "MUMBAI"
                           }
                        }
                     ]
                  }
               }
            }
         }
      ]
    

    There is an additional inner_hits in the response besides the _source which contains only the nested documents that matched. By default inner_hits returns the top 3 nested docs. You can tweak this with the size option.

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)