带过滤器的弹性搜索查询 - 弹性搜索1.7

时间:2016-07-25 12:55:52

标签: elasticsearch

将此发布到弹性搜索会引发异常 org.elasticsearch.index.query.QueryParsingException:[myapp]没有为[match]注册过滤器

 http://localhost:9200/  
      GET myapp/_search/
    {
      "query": {
        "filtered": {
          "filter": {
            "bool": {
              "must": [
                {
                  "match": {
                    "userName": "Micky"
                  }
                },
                {
                  "match": {
                    "Age": 21
                  }
                }
              ],
              "should": [],
              "must_not": []
            }
          }
        }
      },
      "from": 0,
      "size": 20
    }

为什么这个查询错了(技术细节)?

1 个答案:

答案 0 :(得分:0)

没有名为match的过滤器,您可以使用term代替

POST myapp/_search/
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "userName": "Micky"
              }
            },
            {
              "term": {
                "Age": 21
              }
            }
          ],
          "should": [],
          "must_not": []
        }
      }
    }
  },
  "from": 0,
  "size": 20
}

或使用constant_score查询代替filtered,因为您没有过滤器。

POST myapp/_search/
{
  "query": {
    "constant_score": {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "userName": "Micky"
              }
            },
            {
              "match": {
                "Age": 21
              }
            }
          ],
          "should": [],
          "must_not": []
        }
      }
    }
  },
  "from": 0,
  "size": 20
}
相关问题