elasticsearch之mappings的其他設置:index、copy_to、對象屬性、settings


elasticsearch之mappings的其他設置:index、copy_to、對象屬性、settings

 

前言

上一小節中,根據dynamic的狀態不同,我們對字段有了更多可自定義的操作。現在再來補充一個參數,使自定義的屬性更加的靈活。

index

首先來創建一個mappings

PUT m4
{
  "mappings": {
    "doc": {
      "dynamic": false,
      "properties": {
        "name": {
          "type": "text",
          "index": true
        },
        "age": {
          "type": "long",
          "index": false
        }
      }
    }
  }
}

可以看到,我們在創建索引的時候,為每個屬性添加一個index參數。那會有什么效果呢?
先來添加一篇文檔:

PUT m4/doc/1
{
  "name": "小黑",
  "age": 18
}

再來查詢看效果:

GET m4/doc/_search
{
  "query": {
    "match": {
      "name": "小黑"
    }
  }
}

GET m4/doc/_search
{
  "query": {
    "match": {
      "age": 18
    }
  }
}

name查詢沒問題,但是,以age作為查詢條件就有問題了:

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: {\n  \"match\" : {\n    \"age\" : {\n      \"query\" : 18,\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.0\n    }\n  }\n}",
        "index_uuid": "GHBPeT5pRnSi3g6DkpIkow",
        "index": "m4"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "m4",
        "node": "dhkqLLTsRemm7qEgRdpvTg",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: {\n  \"match\" : {\n    \"age\" : {\n      \"query\" : 18,\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.0\n    }\n  }\n}",
          "index_uuid": "GHBPeT5pRnSi3g6DkpIkow",
          "index": "m4",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Cannot search on field [age] since it is not indexed."
          }
        }
      }
    ]
  },
  "status": 400
}

返回的是報錯結果,這其中就是index參數在起作用。

小結:index屬性默認為true,如果該屬性設置為false,那么,elasticsearch不會為該屬性創建索引,也就是說無法當做主查詢條件。

copy_to

現在,再來學習一個copy_to屬性,該屬性允許我們將多個字段的值復制到組字段中,然后將組字段作為單個字段進行查詢。

PUT m5
{
  "mappings": {
    "doc": {
      "dynamic":false,
      "properties": {
        "first_name":{
          "type": "text",
          "copy_to": "full_name"
        },
        "last_name": {
          "type": "text",
          "copy_to": "full_name"
        },
        "full_name": {
          "type": "text"
        }
      }
    }
  }
}

PUT m5/doc/1
{
  "first_name":"tom",
  "last_name":"ben"
}
PUT m5/doc/2
{
  "first_name":"john",
  "last_name":"smith"
}

GET m5/doc/_search
{
  "query": {
    "match": {
      "first_name": "tom"
    }
  }
}

GET m5/doc/_search
{
  "query": {
    "match": {
      "full_name": "tom"
    }
  }
}

上例中,我們將first_namelast_name都復制到full_name中。並且使用full_name查詢也返回了結果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "m5",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "first_name" : "tom",
          "last_name" : "ben"
        }
      }
    ]
  }
}

返回結果表示查詢成功。但既要查詢tom還要查詢smith該怎么辦?

GET m5/doc/_search
{
  "query": {
    "match": {
      "full_name": {
        "query": "tom smith",
        "operator": "or"
      }
    }
  }
}

將查詢條件以空格隔開並封裝在query內,operator參數為多個條件的查詢關系也可以是and,也有簡寫方式:

GET m5/doc/_search
{
  "query": {
    "match": {
      "full_name": "tom smith"
    }
  }
}

copy_to還支持將相同的屬性值復制給不同的字段。

PUT m6
{
  "mappings": {
    "doc": {
      "dynamic":false,
      "properties": {
        "first_name":{
          "type": "text",
          "copy_to": "full_name"
        },
        "last_name": {
          "type": "text",
          "copy_to": ["field1", "field2"]
        },
        "field1": {
          "type": "text"
        },
        "field2": {
          "type": "text"
        }
      }
    }
  }
}


PUT m6/doc/1
{
  "first_name":"tom",
  "last_name":"ben"
}
PUT m6/doc/2
{
  "first_name":"john",
  "last_name":"smith"
}

上例中,只需要將copy_to的字段以數組的形式封裝即可。無論是通過field1還是field2都可以查詢。
小結:

  • copy_to復制的是屬性值而不是屬性
  • copy_to如果要應用於聚合請將filddata設置為true
  • 如果要將屬性值復制給多個字段,請用數組,比如copy_to:["field1", "field2"]

對象屬性

現在,有一個個人信息文檔如下:

PUT m7/doc/1
{
  "name":"tom",
  "age":18,
  "info":{
    "addr":"北京",
    "tel":"10010"
  }
}

首先,這樣嵌套多層的mappings該如何設計呢?

PUT m7
{
  "mappings": {
    "doc": {
      "dynamic": false,
      "properties": {
        "name": {
          "type": "text"
        },
        "age": {
          "type": "text"
        },
        "info": {
          "properties": {
            "addr": {
              "type": "text"
            },
            "tel": {
              "type" : "text"
            }
          }
        }
      }
    }
  }
}

那么,如果要以name或者age屬性作為查詢條件查詢難不倒我們。
現在如果要以info中的tel為條件怎么寫查詢語句呢?

GET mapping_test9/doc/_search
{
  "query": {
    "match": {
      "info.tel": "10086"
    }
  }
}

上例中,info既是一個屬性,也是一個對象,我們稱為info這類字段為對象型字段。該對象內又包含addrtel兩個字段,如上例這種以嵌套內的字段為查詢條件的話,查詢語句可以以字段點子字段的方式來寫即可。

settings設置

設置主、復制分片

在創建一個索引的時候,我們可以在settings中指定分片信息:

PUT s1
{
  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text"
        }
      }
    }
  }, 
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 5
  }
}

number_of_shards是主分片數量(每個索引默認5個主分片),而number_of_replicas是復制分片,默認一個主分片搭配一個復制分片。


see also:elasticsearch mapping|copy_to
歡迎斧正,that's all

 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM