针对特定字段的ElasticSearch查询不会产生任何结果

时间:2014-12-26 20:04:11

标签: elasticsearch

当我尝试对映射的某些字段运行查询时,我得到一个空的结果集,例如“命中”:[]。 这是我的映射:

@classmethod
def get_mapping(cls):
    not_analyzed_str = {'type': 'string',
                        'index': 'not_analyzed'}
    stake_fields = {
        'id': {'type': 'integer'},
        'user': not_analyzed_str,
        'user_id': {'type': 'integer'},
        'side': {'type': 'integer'},
        'amount': {'type': 'double'},
        'created': {'type': 'date'},
        'address': not_analyzed_str,
    }
    return {
        'properties': {
            'id': {'type': 'integer'},
            'description': {'type': 'string',
                            'analyzer': 'snowball'},
            'group': not_analyzed_str,
            'state': not_analyzed_str,
            'type': not_analyzed_str,
            'currency': not_analyzed_str,
            'username': not_analyzed_str,
            'user_id': {'type': 'integer'},
            'expires': {'type': 'date'},
            'created': {'type': 'date'},
            'stakes_acception_end': {'type': 'date'},
            'winner': {'type': 'integer'},
            'stakes': {'type': 'nested',
                       'properties': stake_fields},
        }
    }

这是我的问题:

curl -XGET '_http://my.ip/main/core_bet/_search' -d '{
    "query": {
        "nested" : {
            "path":"stakes",
            "query" : {
                "bool": {
                    "should": [{
                        "match": {"user_id": 2}
                    }]
                }
            }
        }
    }
}'

我一无所获。但当我将“匹配”值更改为{“side”:0}时,我得到了这个:

    {
  "username": "DenisDavydov",
  "expires": "2014-12-28T17:53:00+00:00",
  "user_id": 1,
  "description": "[bet-engine] 0 bugs (2014-12-28 19:53)",
  "created": "2014-12-24T17:53:37.722558+00:00",
  "stakes_acception_end": "2014-12-27T17:53:00+00:00",
  "winner": null,
  "currency": "xxx/xxx",
  "state": "fresh",
  "group": "",
  "type": "0_bugs",
  "id": 45,
  "stakes": [
    {
      "user_id": 2,
      "created": "2014-12-26T14:24:52.565039+00:00",
      "id": 1,
      "amount": 12,
      "user": "admin",
      "address": "xxxxxxxxxxx",
      "side": 0
    },
    {
      "user_id": 2,
      "created": "2014-12-26T14:52:02.709043+00:00",
      "id": 2,
      "amount": 2,
      "user": "admin",
      "address": "xxxxxxxxx",
      "side": 0
    }
  ]
}

很明显,索引确实包含匹配“user_id”的记录:2,那么它有什么问题?我该如何解决?

1 个答案:

答案 0 :(得分:0)

根据文档,您需要提供完整路径 链接 - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html#query-dsl-nested-query

所以

curl -XGET '_http://my.ip/main/core_bet/_search' -d '{
    "query": {
        "nested" : {
            "path":"stakes",
            "query" : {
                "bool": {
                    "should": [{
                        "match": {"stakes.user_id": 2}
                    }]
                }
            }
        }
    }
}'

绝对可以。