1. 程式人生 > >elasticsearch基本操作三

elasticsearch基本操作三

0.資料準備

# text指定中文分詞器(ik_max_word)
PUT data
{
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 3
  },
  "mappings": {
    "user": {
      "properties": {
        "name":{"type":"text","analyzer":"ik_max_word"},
        "age":{"type":"integer"},
        "address":{"type":"text","analyzer":"ik_max_word"},
        "interests":{"type":"text","analyzer":"ik_max_word"},
        "birthday":{"type":"date"}
      } 
    }
  }
}

PUT data/user/1
{
  "name":"趙六",
  "address":"北京市西二旗",
  "age":23,
  "birthday":"1995-10-11",
  "interests":"吃雞 英雄聯盟 王者榮耀"
}

PUT data/user/2
{
  "name":"王五",
  "address":"上海市徐家彙",
  "age":13,
  "birthday":"2005-08-15",
  "interests":"吃雞 唱歌 王者榮耀"
}

PUT data/user/3
{
  "name":"趙四",
  "address":"南京市雨花臺",
  "age":18,
  "birthday":"2000-01-01",
  "interests":"吃雞 唱歌 王者榮耀"
}

PUT data/user/4
{
  "name":"張三",
  "address":"無錫市太湖",
  "age":18,
  "birthday":"2001-11-09"
}

注:常用中文分詞器ik_max_word ik_smart

1.filter查詢(不計算相關性;可以cache;比query快)

GET data/user/_search
{
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "age": [23, 18]
        }
      }
    }
  }
}

GET data/user/_search
{
  "query": {
    "bool": {
      "should":[
        {"term":{"age":23}},
        {"term":{"name":"趙"}}
      ],
      "must_not": [
        {"term":{"age":18}}
      ]
    }
  }
}
注:bool過濾查詢 must相當於and should相當於or must_not相當於!=

GET data/user/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gt": 13,
            "lte": 18
          }
        }
      }
    }
  }
}
注:gt(>) gte(>=) lt(<) lte(<=)

GET data/user/_search
{
  "query": {
    "bool": {
      "filter": {
        "exists": {
          "field": "interests"
        }
      }
    }
  }
}
注:exists不為null

2.聚合查詢

# 求和
GET data/user/_search
{
  "size": 0, 
  "aggs": {
    "age_of_sum": {
      "sum": {
        "field": "age"
      }
    }
  }
}

# 取最小值
GET data/user/_search
{
  "size": 0,
  "aggs": {
    "min_of_age": {
      "min": {
        "field": "age"
      }
    }
  }
}

# 取最大值
GET data/user/_search
{
  "size": 0, 
  "aggs": {
    "max_of_age": {
      "max": {
        "field": "age"
      }
    }
  }
}

# 取平均值
GET data/user/_search
{
  "size": 0,
  "aggs": {
    "age_of_avg": {
      "avg": {
        "field": "age"
      }
    }
  }
}

# 求基數
GET data/user/_search
{
  "size": 0,
  "aggs": {
    "age_of_cardinality": {
      "cardinality": {
        "field": "age"
      }
    }
  }
}

# 分組(相當於group)
GET data/user/_search
{
  "size": 0,
  "aggs": {
    "price_of_group": {
      "terms": {
        "field": "age"
      }
    }
  }
}

# 有唱歌興趣的使用者 按年齡分組
GET data/user/_search
{
  "query": {
    "match": {
      "interests": "唱歌"
    }
  },
  "aggs": {
    "age_of_group": {
      "terms": {
        "field": "age"
      }
    }
  }
}

3.複合查詢

GET data/user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "趙"
          }
        },
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "age": [13]
                }
              }
            ]
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "age": 18
          }
        }
      ]
    }
  }
}

4.constant_score, 不計算scope值

GET data/user/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "age": [
            18,
            23
          ]
        }
      }
    }
  }
}