天天看點

elasticsearch查詢檢索總結查詢聚合映射

查詢

查詢全部

GET /bank/_search
{
  "query": {
    "match_all": {}
  }
  , "sort": [
    {
      "balance": {
        "order": "desc"
      }
    }
  ],
  "from": 10,
  "size": 5,
  "_source": ["account_number", "balance", "firstname"]
}
           

match

全文檢索,維護了反向索引,分詞,按評分由高到低排序

GET /bank/_search
{
  "query": {
    "match": {
      "address": "Mill Lane"
    }
  }
}
           

match_phrase

把詞組當成整體查詢,包含這個字段,不分詞

GET /bank/_search
{
  "query": {
    "match_phrase": {
      "address": "Mill Lane"
    }
  }
}
           

.keyword

隻能查詢精确比對,全等于

GET /bank/_search
{
  "query": {
    "match": {
      "address.keyword": "Mill Lane"
    }
  }
}
           

fields

多個字段包含某個詞,多字段比對 分詞

GET /bank/_search
{
  "query": {
    "multi_match": {
      "query": "Mill Road",
      "fields": ["address","firstname"]
    }
  }
}
           

bool

must必須滿足, must_not必須不滿足,should可以比對可以不比對,會貢獻相關性得分

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "gender": "M"
          }
        },
        {
         "match": {
            "address": "mill"
          } 
        }
      ],
      "must_not": [
        {
          "match": {
            "age": "28"
          }
        }
      ],
      "should": [
        {
          "match": {
            "lastname": "Holland"
          }
        }
      ]
    }
  }
}
           

must range

會計算每個結果的相關性得分

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "age": {
              "gte": 40,
              "lte": 50
            }
          }
        }
      ]
    }
  }
}
           

filter

不會計算每個結果的相關性得分

GET /bank/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 40,
            "lte": 50
          }
        }
      }
    }
  }
}
           

term

找精确字段(如年齡)使用term, text文本全文檢索的使用match

GET /bank/_search
{
  "query": {
    "term": {
      "age": "28"
    }
  }
}
           

聚合

可以執行多個聚合

GET /bank/_search
{
  "size": 0, 
  "query": {
    "bool": {
      "filter": {
        "range": {
          "balance": {
            "gte": 30000,
            "lte": 40000
          }
        }
      }
    }
  },
  "aggs": {
    "aggAge": {
      "terms": {
        "field": "age",
        "size": 10
      }
    },
    "ageAvg": {
      "avg": {
        "field": "age"
      }
    },
    "balanceAvg": {
      "avg": {
        "field": "balance"
      }
    }
  }
}
           

檢視年齡段的平均薪資

GET /bank/_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "ageRange": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 10,
            "to": 20
          },
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "to": 50
          }
        ]
      },
      "aggs": {
        "bananceAvg": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}
           

查出所有年齡分布 并且這些年齡中M的和F的平均薪資,最大最小薪資以及這個年齡段的平均薪資最大最小薪資

GET /bank/_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "ageRange": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "to": 50
          }
        ]
      },
      "aggs": {
        "genderAgg": {
          "terms": {
            "field": "gender.keyword"
          },
          "aggs": {
            "balanceAvg": {
              "avg": {
                "field": "balance"
              }
            },
            "balanceMax": {
              "max": {
                "field": "balance"
              }
            },
            "balanceMin": {
              "min": {
                "field": "balance"
              }
            }
          }
        },
        "balanceAvg": {
          "avg": {
            "field": "balance"
          }
        },
        "balanceMax": {
          "max": {
            "field": "balance"
          }
        },
        "balanceMin": {
          "min": {
            "field": "balance"
          }
        }
      }
    }
  }
}
           

映射

擷取字段映射

GET /myindex/_mapping
           

新增映射

6.0版本後建議索引後面不加類型

PUT /myindex
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer"
      },
      "email": {
        "type": "keyword"
      },
      "name": {
        "type": "text"
      }
    }
  }
}
           

添加字段映射

index:false是否被檢索

PUT /myindex/_mapping
{
  "properties": {
    "remark": {
      "type": "text",
      "index": false 
    }
  }
}
           

修改字段映射

1.建立一個新索引2.将資料遷移過去

建立一個新索引

PUT /newindex
{
  "mappings": {
    "properties": {
      "age": {
        "type": "long"
      },
      "email": {
        "type": "text"
      },
      "name": {
        "type": "text"
      }
    }
  }
}
           

資料遷移沒有類型的

# 資料遷移沒有類型的
POST _reindex
{
  "source": {
    "index": "myindex"
  },
  "dest": {
    "index": "newindex"
  }
}
# 6.0之前有類型的,需要加類型
POST _reindex
{
  "source": {
    "index": "myindex",
    "type": "type"
  },
  "dest": {
    "index": "newindex"
  }
}
           

繼續閱讀