首页 > 解决方案 > 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
              }
            }
          }
        }
      }
    }
  }
}

你以前有没有遇到过这样的问题?

非常感谢!

标签: elasticsearch

解决方案


作为文本字段status_useranalyzed您可以:

在关键字子字段上使用术语查询进行精确请求,或在文本字段上进行匹配查询(具有潜在的文本分析)。但你必须选择:D

所以为了精确匹配

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

或者

{
  "query": {
    "bool": {
      "filter": {
        "match": {
          "status_user": "READY"
        }
      }
    }
  }
}

推荐阅读