1. 索引

  • 创建索引
PUT /employee
PUT /movie
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "english"
      },
      "tagline": {
        "type": "text",
        "analyzer": "english"
      },
      "release_date": {
        "type": "date",
        "format": "yyyy-MM-dd"
      },
      "popularity": {
        "type": "double"
      },
      "overview": {
        "type": "text",
        "analyzer": "english"
      }
    }
  }
}
  • 查看索引是否存在
HEAD /employee
  • 查看索引
GET /employee
  • 查看索引设置
GET /employee/_settings
  • 删除索引
DELETE /employee
  • 关闭索引
POST /employee/_close
  • 打开索引
POST /employee/_open
  • 克隆索引
POST /employee/_clone/clone_employee
  • 添加映射
PUT /employee,employee2/_mapping
{
  "properties": {
    "remark": {
      "type": "text",
      "analyzer": "ik_max_word"
    }
  }
}
PUT /employee
{
  "mappings": {
    "properties": {
      "remark": {
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}
  • 查看映射
GET /employee/_mapping

2. 文档

  • 创建索引和文档,如果索引不存在,则创建索引,如果文档不存在则创建文档,否则全量更新文档
PUT /employee/_doc/1
{
  "name": "陈开开",
  "age": 25
}
  • 创建文档,如果文档已存在则失败
POST /employee/_create/1
{
  "name": "中国人民",
  "age": 20
}
  • 删除文档
DELETE /employee/_doc/1
  • 检查文档是否存在
HEAD /employee/_doc/1
  • 查看文档总数量
GET /movie/_count
  • 查看指定文档
GET /employee/_doc/1
  • 查看源
GET /employee/_source/1
  • 修改文档
POST /employee/_update/1
{
  "doc": {
    "name": "陈21"
  }
}
  • 批量查看
GET /_mget
{
  "docs": [
    {
      "_index": "employee",
      "_id": "1"
    },
    {
      "_index": "employee2",
      "_id": "1"
    }
  ]
}
GET /employee/_doc/_mget
{
  "docs": [
    {
      "_id": "1"
    },
    {
      "_id": "2"
    }
  ]
}
GET /employee/_mget
{
  "ids": [
    "1",
    "2"
  ]
}
  • 批量操作
POST _bulk
{
  "index": {
    "_index": "test",
    "_id": "1"
  }
}
{
  "field1": "value1"
}
{
  "delete": {
    "_index": "test",
    "_id": "2"
  }
}
{
  "create": {
    "_index": "test",
    "_id": "3"
  }
}
{
  "field1": "value3"
}
{
  "update": {
    "_id": "1",
    "_index": "test"
  }
}
{
  "doc": {
    "field2": "value2"
  }
}
POST /employee/_bulk
{
  "index": {
    "_id": "1"
  }
}
{
  "name": "男子偷上万元发红包求交女友 被抓获时仍然单身"
}
{
  "index": {
    "_id": "2"
  }
}
{
  "name": "16岁少女为结婚“变”22岁 7年后想离婚被法院拒绝"
}
  • 根据查询删除
POST /employee/_delete_by_query
{
  "query": {
    "match": {
      "name": "中国"
    }
  }
}
  • 根据查询修改
POST /employee/_update_by_query
{
  "script": {
    "source": "ctx._source.age++",
    "lang": "painless"
  },
  "query": {
    "match": {
      "_id": 1
    }
  }
}

3. 搜索

  • match 查看,按照字段上定义的分词分析后去索引内查看,默认 operator 为 or,默认 minimum_should_match 为 1
GET /movie/_search
{
  "query": {
    "match": {
      "title": {
        "query": "Wall Street: Money Never Sleeps",
        "operator": "or",
        "minimum_should_match": 2
      }
    }
  }
}
  • term 查看,不会进行分词,精确匹配
GET /movie/_search
{
  "query": {
    "term": {
      "title": "killer"
    }
  }
}
GET /movie/_search
{
  "query": {
    "terms": {
      "title": [
        "killer",
        "elite"
      ]
    }
  }
}
  • 查看全部
GET /employee/_search
GET /movie/_search
{
  "query": {
    "match_all": {}
  }
}
  • 短语查看
GET /movie/_search
{
  "query": {
    "match_phrase": {
      "title": "steve zissou"
    }
  }
}
  • 短语前缀查看
GET /movie/_search
{
  "query": {
    "match_phrase_prefix": {
      "title": "steve zis"
    }
  }
}
  • 多字段查看

best_fields:默认,查找匹配任何字段的文档,但使用来自最佳字段的 _score
most_fields:查找匹配任何字段的文档,并合并每个字段的 _score
cross_fields:使用同一个分析器处理字段,就像它们是一个大字段一样。查找任何字段中的每个单词
phrase:在每个字段上运行 match_phrase 查看,并使用来自最佳字段的 _score
phrase_prefix:在每个字段上运行 match_phrase_prefix 查看,并使用来自最佳字段的 _score
bool_prefix:在每个字段上创建一个 match_bool_prefix 查看,并合并每个字段的 _score

GET /movie/_search
{
  "query": {
    "multi_match": {
      "type": "most_fields", 
      "query": "basketball with cartoom aliens",
      "fields": [
        "title",
        "overview"
      ]
    }
  },
  "from": 0,
  "size": 10,
  "_source": [
    "title",
    "overview"
  ]
}
  • query_string
GET /movie/_search
{
  "query": {
    "query_string": {
      "fields": [
        "title"
      ],
      "query": "steve AND jobs"
    }
  }
}
  • 过滤
GET /movie/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "popularity": {
              "gt": 10
            }
          }
        }
      ]
    }
  }
}
  • 多条件过滤
GET /movie/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "title": "steve"
          }
        },
        {
          "range": {
            "release_date": {
              "lte": "2015-01-01"
            }
          }
        },
        {
          "range": {
            "popularity": {
              "gte": "25"
            }
          }
        }
      ]
    }
  }
}
  • 带分数的过滤
GET /movie/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "life"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "title": "steve"
          }
        },
        {
          "range": {
            "release_date": {
              "lte": "2015-01-01"
            }
          }
        },
        {
          "range": {
            "popularity": {
              "gte": "25"
            }
          }
        }
      ]
    }
  }
}
  • 聚合
GET /movie/_search
{
  "sort": [
    {
      "popularity": {
        "order": "desc"
      }
    }
  ],
  "aggs": {
    "group_by_release_date": {
      "terms": {
        "field": "release_date",
        "size": 5
      }
    }
  }
}
  • Bool 查看

must:必须都是 true
must not:必须都是 false
should:其中有一个为 true 即可,但 true 的越多得分越高

GET /movie/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "basketball with cartoom aliens"
          }
        },
        {
          "match": {
            "overview": "basketball with cartoom aliens"
          }
        }
      ]
    }
  }
}
  • 突出显示

"tags_schema" : "styled",
"pre_tags": [""], "post_tags": [""],

GET /movie/_search
{
  "query": {
    "match": {
      "title": "world"
    }
  },
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}

4. 其它

  • 校验语句是否合法
GET /movie/_validate/query?explain
{
  "query": {
    "multi_match": {
      "query": "steve job",
      "fields": [
        "title",
        "overview"
      ],
      "operator": "or",
      "type": "most_fields"
    }
  }
}
  • 分析调试
GET /movie/_search
{
  "explain": true,
  "query": {
    "match": {
      "title": "steve"
    }
  }
}

  • 修改打分权重
GET /movie/_search
{
  "query": {
    "multi_match": {
      "query": "basketball with cartoom aliens",
      "fields": [
        "title^10",
        "overview"
      ],
      "tie_breaker": 0.3
    }
  }
}
  • 自定义打分
GET /movie/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query": "steve job",
          "fields": [
            "title",
            "overview"
          ],
          "operator": "or",
          "type": "most_fields"
        }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "popularity",
            "modifier": "log2p",
            "factor": 10
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "sum"
    }
  }
}

Q.E.D.


盛年不重来,一日难再晨。