如何使用Elasticsearch .js和Angular在Elasticsearch上搜索嵌套对象

How to search nested objects on Elasticsearch using elasticsearch.js and Angular

本文关键字:Elasticsearch 搜索 嵌套 对象 Angular 何使用 js      更新时间:2023-09-26

我想在elasticsearch数据库上搜索嵌套文档上的字段。我使用以下命令搜索嵌套对象上的字段(获得10个内部命中):

curl -X GET 'http://localhost:9200/jira-dev/_search?pretty=true' -d '
{
"_source" : false,
"query" : {
    "nested" : {
        "path" : "issues",
        "query" : {
          "bool": {
            "should": [                   
              {
                "wildcard":{
                  "issues.fields.description":"*migrate*"
                } 
              },
              {
                "wildcard":{
                  "issues.fields.comment.comments.body":"*autodeploy*"
                } 
              }
            ]
          }               
        },
        "inner_hits" : {"size": 5000}
        }
    }   
}'

但是当我尝试使用elasticsearch.js以这种方式调用它时,它没有得到任何结果:

client.search({
    index: 'jira-dev/',
    type: 'jira',
    body: {
        query: {
            nested : {
                path : "issues",
                query : {
                    bool: {
                        should: [                   
                        {
                            wildcard:{
                                "issues.fields.description":"*migrate*"
                            } 
                        },
                        {
                            wildcard:{
                                "issues.fields.comment.comments.body":"*autodeploy*"
                            } 
                        }
                        ]
                    }               
                },
                inner_hits : {size: 5000}
            }
          }
      }
}).then(function (resp) {
    var hits = resp.hits.totals;
    console.log('works');
}, function (err) {
    console.log('epic fail');
    console.trace(err.message);
});

我认为我使用的语法是不正确的,但我没有发现使用elasticsearch.js嵌套查询的任何示例。

提前感谢。

编辑:

根据要求,以下是文档的配置:

curl -X POST 'http://localhost:9200/jira-dev/' -d '{
    "mappings" : {
        "jira" : {
            "properties" : {
                "expand" : {"type" : "string"},
                "startAt" : {"type" : "integer"},
                "maxResults" : {"type" : "integer"},
                "total" : {"type" : "integer"},
                "issues" : {
                    "type" : "nested",
                    "include_in_parent" : false,
                    "properties" : {
                        "created" : {"type" : "date", "format" : "date_hour_minute_second_fraction"}
                    }
                }
            }
        }
    },
    "settings" : {
        "number_of_shards" : 5,
        "number_of_replicas" : 1
    }
}'

和一个例子:

{
    "_index": "jira-dev",
    "_type": "jira",
    "_id": "1",
    "_version": 1,
    "_score": 1,
    "_source": {
        "expand": "schema,names",
        "startAt": 0,
        "maxResults": 1000,
        "total": 604,
        "issues": [
            {
                "key": "STACK-1",
                "fields": {
                    "summary": "A nice summary",
                    "created": "2016-06-28T09:45:32.000+0000",
                    "description": null,                    
                    "comment": {
                        "startAt": 0,
                        "maxResults": 1,
                        "total": 1,
                        "comments": [ 
                            {
                                "self": url",
                                "id": "30293",
                                "author": {
                                    "name": "stack_overflow",
                                    "key": "stack_overflow"
                                },
                                "body": "Following epic has been created: url",
                                "updateAuthor": {
                                    "name": "stack_overflow",
                                    "key": "stack_overflow",
                                    "timeZone": "Europe/Madrid"
                                },
                                "created": "2016-03-04T10:09:11.000+0000",
                                "updated": "2016-03-04T10:09:11.000+0000"
                            }
                        ]
                    }
                }
            }
        ]
    }
}

最后,为了跳过这个问题,我在angular和elastic之间使用了一个中间件服务器。这个服务器是一个python服务器,很容易通过curl对elasticsearch进行GET/POST操作。

此外,我尝试直接在Angular中使用curl (html GET),但由于惯例,html服务器可以忽略GET请求中的数据。因此,通过Angular的curl,不可能在elastic上执行请求。