弹性搜索:parse_exception:无法解析搜索源。期望的字段名称,但得到[START_OBJECT]

时间:2016-10-01 12:42:36

标签: elasticsearch

我是Elastic Search中的新用户并在我的应用程序中使用ES。当我在ES上的单个表上运行简单查询然后它工作文件..但是当我使用嵌套查询时,它没有给我正确的结果。

基本上,我有两个表peopleinteractions都是单独的表。并且交互表具有person_id,其引用人员表id。我想要获取user_id: 2人的互动。当我创建条件并运行查询时,我收到错误。

以下是我正在运行的查询。

{
  "index": "my_index",
  "type": "people",
  "fields": "_source,_timestamp",
  "size": 10,
  "from": 0,
  "body": {
    "query": {
      "bool": {
        "should": {
          "wildcard": {
            "_all": "*a*"
          }
        }
      },
      "nested": {
        "path": "interactions",
        "query": {
          "bool": {
            "should": {
              "match": {
                "interactions.user_id": 2
              }
            }
          }
        }
      }
    },
    "sort": [
      {
        "last_name": {
          "order": "asc"
        }
      }
    ]
  }
}

以下是我的回复

{
  "status": false,
  "error_code": 657,
  "errors": [
    "parse_exception: failed to parse search source. expected field name but got [START_OBJECT]"
  ]
}

以下是我们在ES中索引的数据

[
    {
        "id": 1,
        "first_name": "Test1",
        "last_name": "Data1",
        "date_of_birth": "1988-11-02",
        "created_at": ".......",
        "updated_at": ".......",
        "status": 1,
        "prefix": "Ms.",
        "suffix": "MD"
    },
    {
        "id": 1,
        "first_name": "Test2",
        "last_name": "Data2",
        "date_of_birth": "1988-11-02",
        "created_at": ".......",
        "updated_at": ".......",
        "status": 1,
        "prefix": "Ms.",
        "suffix": "MD"
    }
]

相互作用

[
    {
        "id": 1,
        "user_id": 11,
        "person_id": 6,
        "interaction_type": 1,
        "initiated_by": 2,
        "created_at": ".......",
        "updated_at": "......."
    },
    {
        "id": 2,
        "user_id": 10,
        "person_id": 5,
        ..........
    }
]

有人可以建议我在这个查询中做错了什么吗? 在此先感谢。

1 个答案:

答案 0 :(得分:1)

更新了答案。 试试这个。

{
    "fields" : "_source,_timestamp",
    "size" : 10,
    "from" : 0,
    "query" : {
        "bool" : {
            "should" : [{
                    "wildcard" : {
                        "_all" : "*a*"
                    }
                }, {
                    "nested" : {
                        "path" : "interactions",
                        "query" : {
                            "bool" : {
                                "should" : {
                                    "match" : {
                                        "interactions.user_id" : 2
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }
    },
    "sort" : [{
            "last_name" : {
                "order" : "asc"
            }
        }
    ]
}

你的json格格不入。你错过了一个']'对于排序。试试下面的

您可以使用http://jsonlint.com/之类的网站来验证jsons。

{
    "index": "my_index",
    "type": "people",
    "fields": "_source,_timestamp",
    "size": 10,
    "from": 0,
    "body": {
        "query": {
            "bool": {
                "should": {
                    "wildcard": {
                        "_all": "*a*"
                    }
                }
            },
            "nested": {
                "path": "interactions",
                "query": {
                    "bool": {
                        "should": {
                            "match": {
                                "interactions.user_id": 2
                            }
                        }
                    }
                }
            }
        },
        "sort": [{
            "last_name": {
                "order": "asc"
            }
        }]
    }
}