2016-10-01 59 views
0

我在Elastic Search新和在我的应用程序中使用ES。当我在ES上的单个表上运行简单的查询,然后它工作文件..但是当我使用嵌套查询,然后它不给我正确的结果。弹性搜索:parse_exception:解析搜索源失败。预期的字段名称,但得到了[START_OBJECT]

基本上,我有两个表peopleinteractions都是单独的表。并且交互表具有参考人表id的person_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, 
     .......... 
    } 
] 

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

+0

可以显示您的索引和示例文档的映射 – blackmamba

+0

@blackmamba我用索引映射示例更新了我的问题。请建议我如何解决我的问题 –

回答

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格式不正确。您错过了Sort的']'。试试以下

您可以使用像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" 
      } 
     }] 
    } 
} 
+0

@ lucky-saini尝试更新的答案。 – jay