首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用elasticsearch.js和Angular在Elasticsearch上搜索嵌套对象

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

Stack Overflow用户
提问于 2016-07-04 18:24:15
回答 1查看 797关注 0票数 1

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

代码语言:javascript
复制
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调用它时,它不会得到任何结果:

代码语言:javascript
复制
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的嵌套查询示例。

提前谢谢。

编辑:

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

代码语言:javascript
复制
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
    }
}'

下面是一个例子:

代码语言:javascript
复制
{

    "_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"
                            }
                        ]
                    }
                }
            }
        ]
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-07-29 17:15:55

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

此外,我尝试直接从Angular (html GET)使用curl,但由于约定,html服务器可能会忽略GET请求中的数据。因此,通过curl from Angular是不可能在elastic上做请求的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38182383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档