Elasticsearch仅查找数值

时间:2018-10-04 21:35:42

标签: elasticsearch

大家早上好!

使用elasticsearch,我可以执行此请求,该请求使我可以拥有某个特定phone_user的所有配置文件,它可以正常工作:

curl -H 'Content-Type: application/json' -XPOST 'localhost:9200/companyname/projectname/_search?pretty' -d '
{"query":{"bool":{"filter":{"terms":{"phone_user":["33612345678"]}}}}}'

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "companyname",
        "_type" : "projectname",
        "_id" : "113",
        "_score" : null,
        "_source" : {
          "phone_user" : "33612345678",
          "status_user" : "READY",

          ……

        }
      }
    ]
  }
}

但是,如果值不再是数字而是文本,那么我将不再有任何结果:

curl -H 'Content-Type: application/json' -XPOST 'localhost:9200/companyname/projectname/_search?pretty' -d '
{"query":{"bool":{"filter":{"terms":{"status_user":["READY"]}}}}}'

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

我的映射:

{
  "conpanyname" : {
    "mappings" : {
      "projectname" : {
        "properties" : {
          "phone_user" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "status_user" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

您以前遇到过这种问题吗?

非常感谢您!

1 个答案:

答案 0 :(得分:0)

由于input=sc.textFile("hdfs://host/user/data/file1.txt") ## it is RDD status_user文本字段,因此您可以:

在关键字子字段上使用terms query进行确切的请求,或者在文本字段上进行match query(可能进行文本分析)。但是您必须选择:D

因此完全匹配

analyzed

{
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "status_user.keyword": [
            "READY"
          ]
        }
      }
    }
  }
}