首页 > 其他 > 详细

Elasticsearch 设置搜索 type 的问题

时间:2016-03-24 16:26:51      阅读:323      评论:0      收藏:0      [点我收藏+]

根源:

由于 kibana3 中,并不支持直接在请求的 url 中设置搜索的 type 。

为了支持 type 的搜索,所以我设置了个下每个 panel 的查询语句,让它增加一个:

"query_string": 
{"query": " _type:\"my_type\" "}

结果今天在查一个 bug 的时候,发现这样有一个坑,,,

 

问题描述:

由于URL请求的路径并没有给出 type ,所以每一次的搜索,依然会查询整个 index,只是在获取结果时候,再过滤一次 type 字段。

如果在同一个 index 下,存在不同 type 中,某个字段类型不一致的情况,那将可能导致搜索不到想要的结果。(因为不同的 type 有不同的 _mapping)

比如:

我在一个字段第一次存的时候,存为了 string 类型,而又新建了另一个 type,且类型变为了 date,

后来在对这个字段进行时间过滤的时候,发现总是不生效,hits 总是空,

URL:http://localhost:9200/index/_search
{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "should": [{
                        "query_string": {
                            "query": "_type:\"my_type\""
                        }
                    }]
                }
            },
            "filter": {
                "bool": {
                    "must": [{
                        "range": {
                            "过期时间": {
                                "from": 1860000665,
                                "to": 2550091665
                            }
                        }
                    }]
                }
            }
        }
    },
    "from": 0
}

结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": ,
    "max_score": 1,
    "hits": []
......

 

但是,完全相同的查询语句,如果在 URL 中指定 type,那么过滤就 OK 了,,,

URL: http://200.200.194.155:9200/index/my_type/_search
{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "should": [{
                        "query_string": {
                            "query": "_type:\"my_type1\""
                        }
                    }]
                }
            },
            "filter": {
                "bool": {
                    "must": [{
                        "range": {
                            "过期时间": {
                                "from": 1861665,
                                "to": 25500008799861665
                            }
                        }
                    }]
                }
            }
        }
    },
    "from": 0
}

结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 67,
    "max_score": 1,
    "hits": [
      {
......

 

解决方法:

确保相同字段的数据类型一致,,,so,删掉某一些,,,只保留一个呗。

或者,最好是能够设置请求的 URL 吧,把 type 加到请求的路径中去。

 

Elasticsearch 设置搜索 type 的问题

原文:http://www.cnblogs.com/licongyu/p/5315700.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!