显式Mapping设置与常见参数介绍

2019-07-11 11:05:12 浏览数 (2)

代码语言:javascript复制

#设置 index 为 false
DELETE users
PUT users
{
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text"
        },
        "lastName" : {
          "type" : "text"
        },
        "mobile" : {
          "type" : "text",
          "index": false
        }
      }
    }
}

PUT users/_doc/1
{
  "firstName":"Ruan",
  "lastName": "Yiming",
  "mobile": "12345678"
}

POST /users/_search
{
  "query": {
    "match": {
      "mobile":"12345678"
    }
  }                                                            
}

搜索的时候会出现如下结果

代码语言:javascript复制
{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: {n  "match" : {n    "mobile" : {n      "query" : "12345678",n      "operator" : "OR",n      "prefix_length" : 0,n      "max_expansions" : 50,n      "fuzzy_transpositions" : true,n      "lenient" : false,n      "zero_terms_query" : "NONE",n      "auto_generate_synonyms_phrase_query" : true,n      "boost" : 1.0n    }n  }n}",
        "index_uuid": "mRXzZX3oSziQNIlWAXUz1Q",
        "index": "users"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "users",
        "node": "r_dJGhryRUC069QXDl9iWw",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: {n  "match" : {n    "mobile" : {n      "query" : "12345678",n      "operator" : "OR",n      "prefix_length" : 0,n      "max_expansions" : 50,n      "fuzzy_transpositions" : true,n      "lenient" : false,n      "zero_terms_query" : "NONE",n      "auto_generate_synonyms_phrase_query" : true,n      "boost" : 1.0n    }n  }n}",
          "index_uuid": "mRXzZX3oSziQNIlWAXUz1Q",
          "index": "users",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Cannot search on field [mobile] since it is not indexed."
          }
        }
      }
    ]
  },
  "status": 400
}

就是说:Cannot search on field [mobile] since it is not indexed mobile 没有被索引 所以不能进行搜索

代码语言:javascript复制
#设定Null_value

DELETE users
PUT users
{
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text"
        },
        "lastName" : {
          "type" : "text"
        },
        "mobile" : {
          "type" : "keyword",
          "null_value": "NULL"
        }

      }
    }
}

PUT users/_doc/1
{
  "firstName":"Ruan",
  "lastName": "Yiming",
  "mobile": null
}


PUT users/_doc/2
{
  "firstName":"Ruan2",
  "lastName": "Yiming2"

}

GET users/_search
{
  "query": {
    "match": {
      "mobile":"NULL"
    }
  }

}

这里你会发现你只能搜索到 mobile为null的 user,没有手机号的信息是搜索不到的

代码语言:javascript复制
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "firstName" : "Ruan",
          "lastName" : "Yiming",
          "mobile" : null
        }
      }
    ]
  }
}

设置copy to

代码语言:javascript复制
DELETE users
PUT users
{
  "mappings": {
    "properties": {
      "firstName":{
        "type": "text",
        "copy_to": "fullName"
      },
      "lastName":{
        "type": "text",
        "copy_to": "fullName"
      }
    }
  }
}
PUT users/_doc/1
{
  "firstName":"Ruan",
  "lastName": "Yiming"
}

GET users/_search?q=fullName:(Ruan Yiming)

POST users/_search
{
  "query": {
    "match": {
       "fullName":{
        "query": "Ruan Yiming",
        "operator": "and"
      }
    }
  }
}

搜索结果为

代码语言:javascript复制
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "firstName" : "Ruan",
          "lastName" : "Yiming"
        }
      }
    ]
  }
}

可见我们是可以设置copy_to 根据copy_to 的字段进行搜索

代码语言:javascript复制
#数组类型
PUT users/_doc/1
{
  "name":"onebird",
  "interests":"reading"
}

PUT users/_doc/1
{
  "name":"twobirds",
  "interests":["reading","music"]
}

POST users/_search
{
  "query": {
        "match_all": {}
    }
}

GET users/_mapping

0 人点赞