angularjs和elasticsearch js api,如何强制GET而不是POST

时间:2014-11-27 15:32:15

标签: angularjs elasticsearch

我在angularjs中使用elasticsearch javascript api。这工作得非常好,但我需要通过中间件查询elasticsearch服务来过滤危险请求(删除,更新)。中间件使用'GET'但API默认使用'POST'查询。

根据文档我可以指定一个参数'method'来使用API​​的'GET'查询: API Conventions

它与'POST'一起使用但是'GET'失败,“GET”并且我有一个错误:

 Error: elasticQuery.search(...) is undefined

我该怎么办? “获取”可以使用长而复杂的查询,如下例所示?

这是我尝试的方式:

 return elasticQuery.search({
         method : 'GET',
         index: 'indexbotanic',
         from: 0
         size: 10,
         body: {
            "fields" : ["C_COLLECTIONCODE", "T_SCIENTIFICNAME", "T_SCIENTIFICNAMEAUTHORSHIP", "T_SPECIFICEPITHET", "T_KINGDOM", "T_PHYLUM", "T_CLASS_", "T_ORDER_", "T_FAMILY", "T_GENUS", "T_SUBGENUS", "T_VERNACULARNAME", "O_CATALOGNUMBER", "O_RECORDNUMBER", "O_CREATED", "O_SEX", "I_INSTITUTIONCODE", "D_DETERMINATIONID", "D_IDENTIFIEDBY", "M_IDENTIFIER", "E_RECORDNUMBER", "E_RECORDEDBY", "L_CONTINENT", "L_COUNTRY", "L_COUNTRYCODE", "L_COUNTY", "L_LOCALITY"],
            "query" : {
                "filtered" : {
                    "filter" : {
                        "and" : [{
                                "or" : [{
                                        "term" : {
                                            "L_CONTINENT" : "europe"
                                        }
                                    }
                                ]
                            }, {
                                "or" : [{
                                        "term" : {
                                            "T_FAMILY" : "lamiaceae"
                                        }
                                    }
                                ]
                            }, {
                                "or" : [{
                                        "term" : {
                                            "E_RECORDEDBY" : "balay, r."
                                        }
                                    }, {
                                        "term" : {
                                            "E_RECORDEDBY" : "boissier, p.e."
                                        }
                                    }
                                ]
                            }, {
                                "or" : [{
                                        "term" : {
                                            "T_SCIENTIFICNAME" : "lamium amplexicaule"
                                        }
                                    }
                                ]
                            }, {
                                "or" : [{
                                        "term" : {
                                            "T_GENUS" : "lamium"
                                        }
                                    }, {
                                        "term" : {
                                            "T_GENUS" : "betonica"
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                }
            },
            "highlight" : {
                "pre_tags" : ["<strong>"],
                "post_tags" : ["</strong>"],
                "fields" : {
                    "C_COLLECTIONCODE" : {},
                    "O_CATALOGNUMBER" : {},
                    "O_RECORDNUMBER" : {},
                    "O_CREATED" : {},
                    "O_SEX" : {},
                    "D_DETERMINATIONID" : {},
                    "D_IDENTIFIEDBY" : {},
                    "E_EVENTID" : {},
                    "E_RECORDNUMBER" : {},
                    "E_RECORDEDBY" : {},
                    "L_CONTINENT" : {},
                    "L_COUNTRY" : {},
                    "L_COUNTRYCODE" : {},
                    "L_COUNTY" : {},
                    "L_LOCALITY" : {},
                    "T_SCIENTIFICNAME" : {},
                    "T_SCIENTIFICNAMEAUTHORSHIP" : {},
                    "T_SPECIFICEPITHET" : {},
                    "T_KINGDOM" : {},
                    "T_PHYLUM" : {},
                    "T_CLASS_" : {},
                    "T_ORDER_" : {},
                    "T_FAMILY" : {},
                    "T_GENUS" : {},
                    "T_SUBGENUS" : {},
                    "T_VERNACULARNAME" : {}

                }
            },
            "aggs" : {
                "L_CONTINENT_MISSING" : {
                    "missing" : {
                        "field" : "L_CONTINENT"
                    }
                },
                "O_SEX_MISSING" : {
                    "missing" : {
                        "field" : "O_SEX"
                    }
                },
                "I_INSTITUTIONCODE_MISSING" : {
                    "missing" : {
                        "field" : "I_INSTITUTIONCODE"
                    }
                },
                "T_TAXONRANK_MISSING" : {
                    "missing" : {
                        "field" : "T_TAXONRANK"
                    }
                },
                "D_TYPESTATUS_MISSING" : {
                    "missing" : {
                        "field" : "D_TYPESTATUS"
                    }
                },
                "O_HASMEDIA_MISSING" : {
                    "missing" : {
                        "field" : "O_HASMEDIA"
                    }
                },
                "T_SCIENTIFICNAME_MISSING" : {
                    "missing" : {
                        "field" : "T_SCIENTIFICNAME"
                    }
                },
                "T_FAMILY_MISSING" : {
                    "missing" : {
                        "field" : "T_FAMILY"
                    }
                },
                "T_GENUS_MISSING" : {
                    "missing" : {
                        "field" : "T_GENUS"
                    }
                },
                "E_RECORDEDBY_MISSING" : {
                    "missing" : {
                        "field" : "E_RECORDEDBY"
                    }
                },
                "L_CONTINENT" : {
                    "terms" : {
                        "field" : "L_CONTINENT",
                        "size" : 20
                    }
                },
                "O_SEX" : {
                    "terms" : {
                        "field" : "O_SEX",
                        "size" : 20
                    }
                },
                "I_INSTITUTIONCODE" : {
                    "terms" : {
                        "field" : "I_INSTITUTIONCODE",
                        "size" : 20
                    }
                },
                "T_TAXONRANK" : {
                    "terms" : {
                        "field" : "T_TAXONRANK",
                        "size" : 20
                    }
                },
                "D_TYPESTATUS" : {
                    "terms" : {
                        "field" : "D_TYPESTATUS",
                        "size" : 20
                    }
                },
                "O_HASMEDIA" : {
                    "terms" : {
                        "field" : "O_HASMEDIA",
                        "size" : 20
                    }
                },
                "T_SCIENTIFICNAME" : {
                    "terms" : {
                        "field" : "T_SCIENTIFICNAME",
                        "size" : 20
                    }
                },
                "T_FAMILY" : {
                    "terms" : {
                        "field" : "T_FAMILY",
                        "size" : 20
                    }
                },
                "T_GENUS" : {
                    "terms" : {
                        "field" : "T_GENUS",
                        "size" : 20
                    }
                },
                "E_RECORDEDBY" : {
                    "terms" : {
                        "field" : "E_RECORDEDBY",
                        "size" : 20
                    }
                }
            },
            "sort" : "_score"
        }
     }).then(function (response) {
     ...
     }

例如这项工作与GET:

curl -XGET 'http://localhost:9200/index/tablename/_search?pretty' -d '{
  "query": {
    "query_string": { 
         "query":"*keysearch*", 
         "fields": ["field1","field2","field3"]
     }
   }
}'

那么如何强制api用“-d'{....}'”发送get? 非常感谢

1 个答案:

答案 0 :(得分:0)

尝试使用此客户端:https://github.com/YousefED/elasticsearch.angular.simple并将POST更改为GET。我不确定哪些浏览器支持GET请求,但是你可能会遇到限制。