弹性搜索URI搜索过滤

时间:2019-09-19 21:39:37

标签: elasticsearch

我有一个movies ES索引,具有以下映射:

{
  "doc": {
    "properties": {
       "director": {
          "type": "text"
       },
       "genres": {
          "type": "text",
          "index": "false"
       },
       "title": {
          "type": "text"
       },
       "year": {
          "type": "long"
       }
    }
  }
}

对于类型,我将提交一个字符串数组。现在,我希望允许我的客户针对我的端点提交查询,在该端点上我向q代理请求参数到ES客户端。

但是我正在努力用URI搜索过滤数据,方法与通过将查询作为有效载荷提交给我的索引可以实现的方式相同。例如:

GET movies/doc/_search
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "must": [
            { 
              "term": {
                "genres": "War"
              }
            }
          ],

          "must": [
            { 
              "term": {
                "genres": "Foo"
              }
            }
          ],

          "must": [
            { 
              "term": {
                "genres": "Bar"
              }
            }
          ]
        }
      }
    }
  }
}

是否可以通过URI搜索来实现?如果可以,怎么办?

2 个答案:

答案 0 :(得分:1)

来自ES文档

  

您可以使用查询参数直接定义搜索条件   在请求URI中,而不是在请求正文中。请求URI   搜索不支持完整的Elasticsearch查询DSL,但支持   方便测试。

根据我们的讨论,我建议您在收到客户端的请求后在后端构建DSL查询。您需要公开一个API,该API可以将UI输入以结构化格式(JSON)发送到后端,这可以帮助您轻松构建DSL查询。

答案 1 :(得分:0)

在没有q参数的情况下,这就是我最终要做的事情。版本6.0.1:

POST http://127.0.0.1:9200/movies/_mapping/doc
{
  "doc": {
    "properties": {
       "director": {
          "type": "text"
       },
       "title": {
          "type": "text"
       },
       "year": {
          "type": "long"
       },
        "genres": {
          "type": "keyword"
       }
    }
  }
}

创建文档:

POST http://127.0.0.1:9200/movies/doc/
{
    "title": "title 1",
    "director": "Igor M",
    "year": 2011,
    "genres": ["Bar", "Foo"]
}

查询文档

GET http://127.0.0.1:9200/movies/_search

{
  "query": {
    "bool": {
        "must":[
       { "match": { "genres": "War" }},
       { "match": { "genres": "Bar" }}
      ]
    }
  }
}