【问题标题】:Conditions in ElasticSearch queriesElasticSearch 查询中的条件
【发布时间】:2016-05-28 11:19:12
【问题描述】:

我是 ElasticSearch 的新手。看起来非常棒,但我对基于 JSON 的查询语言感到非常困惑。

我打算使用 ES 作为文档存储。我有兴趣进行诸如“获取年龄 = 25 的所有文档”或“获取 name = 'john' 和 city = 'london' 的所有文档”之类的查询。但是我还没有理解如何做到这一点。

我可以这样做:

{ “询问”: { “匹配”: { “年龄”:“25” } } }

但这就是我要找的吗?我认为这也会返回age 为“25 个苹果”的文档。

请解释如何针对 ES 发出这样的简单查询。

【问题讨论】:

  • 您可以改用这些东西。首先在您的映射中将您的年龄字段设置为 not_analysed。其次,在您的映射中,为年龄字段定义一个类型为 long 的严格动态映射。第三确保您在该特定类型中拥有的数据是正确的,因为它为您提供了来自该特定字段的错误数据,这意味着您的数据是错误的并使用术语查询而不是匹配

标签: elasticsearch indexing database nosql


【解决方案1】:

对于age 匹配,您可以使用term QueryTerm Query 寻找完全匹配。

{
"query": {
  "term": {
     "age": "25"
   }
  }
}

【讨论】:

    【解决方案2】:

    如果您害怕查询 DSL,您可能不太害怕使用 query string mini-language(接近 Lucene 查询语言)passed directly in the URIq= 查询字符串参数,它可能会更简单一些学习和活动虽然有一些限制,但它可以让你走很长的路。

    例如,要查询年龄为 25 的所有文档,您可以这样做:

    curl -XGET 'localhost:9200/_search?q=age:25'
    

    对于年龄在 25 到 30 岁之间的所有文档:

    curl -XGET 'localhost:9200/_search?q=age:[25 TO 30]'
    

    对于年龄在 25 到 30 岁之间以及国家/地区US 的所有文件:

    curl -XGET 'localhost:9200/_search?q=age:[25 TO 30] AND country:us'
    

    【讨论】:

      【解决方案3】:
       To get all documents where (name = john and city = london).You can use following command:
      
      
          GET /bank/account/_search?q=firstname:Rodriquez%20AND%20age:31
      
           Or,You can also try Filters:
      
           GET /demo/post/_search?pretty=true
           {
             "query": {
               "filtered": {
                   "filter": {
                       "and": [
                      {
                      "term": {
                        "name": "john"
                      }
                    },
                    {
                   "term": {
                   "city": "london"
                          }
                        }
                   ]
                  }
                 }
                }
              } 
      

      【讨论】:

      • 您可以改用这些东西。首先在您的映射中将您的年龄字段设置为 not_analysed。其次,在您的映射中,为年龄字段定义一个类型为 long 的严格动态映射。第三,确保您在该特定类型中拥有的数据是正确的,因为它为您提供了该特定字段中的错误数据,这意味着您的数据是错误的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      相关资源
      最近更新 更多