es 杂记

es version:5.6.8

bulk api

post index/type/_bulk

  • 提供批量操作doc的能力,要求通过dsl语法,一个操作跟着一个数据
  • Index:创建或更新一个doc,如果doc存在,则全量更新,不存在则创建;
  • Create:创建一个doc,如果doc存在,则报错
  • Update:更新一个doc,如果doc不存在,则报错;更新api使用doc部分更新需要字段
  • Delete:删除一个doc,如果doc不存在,不会报错; 删除一个doc

POST shop/order/_bulk
{"index":{"_id":1}} {"orderId":"1","venderId":1,"consName":"zhang san","createDate":"2017-09-23"}
{"index":{"_id":2}} {"orderId":"2","venderId":1,"consName":"li si","createDate":"2017-07-13"}
{"index":{"_id":3}} {"orderId":"3","venderId":2,"consName":"zhang san","createDate":"2017-09-17"}
{"index":{"_id":4}} {"orderId":"4","venderId":3,"consName":"wang wu","createDate":"2017-09-23"}


es会自动创建dynamic mapping,mapping如下:

{
  "shop": {
    "mappings": {
      "order": {
        "properties": {
          "consName": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "createDate": {
            "type": "date"
          },
          "orderId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "venderId": {
            "type": "long"
          }
        }
      }
    }
  }
}

term api

根据exact value进行搜索,支持keyword,数字,boolean,date类型

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "orderId": "1"
        }
      }
    }
  }
}

result:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "shop",
        "_type": "order",
        "_id": "1",
        "_score": 1,
        "_source": {
          "orderId": "1",
          "venderId": 1,
          "consName": "zhang san",
          "createDate": "2017-09-23"
        }
      }
    ]
  }
}

使用term查询text测试

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "consName": "zhang san"
        }
      }
    }
  }
}

result:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

关联查询

查询2017-07-13的订单,或者2017-09-23的商家id不为3的订单

GET shop/order/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "createDate": {
              "value": "2017-07-13"
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "createDate": {
                    "value": "2017-09-23"
                  }
                }
              }
            ],
            "must_not": [
              {
                "term": {
                  "venderId": {
                    "value": 3
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

result:

{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "shop",
        "_type": "order",
        "_id": "2",
        "_score": 1,
        "_source": {
          "orderId": "2",
          "venderId": 1,
          "consName": "li si",
          "createDate": "2017-07-13"
        }
      },
      {
        "_index": "shop",
        "_type": "order",
        "_id": "1",
        "_score": 1,
        "_source": {
          "orderId": "1",
          "venderId": 1,
          "consName": "zhang san",
          "createDate": "2017-09-23"
        }
      }
    ]
  }
}

查询2017-07-13的订单或2017-09-23;并且商家id不为3的订单

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter":{
        "bool": {
          "should":[
            {
              "term":{
                "createDate":"2017-07-13"
              }
            },
            {
              "term":{
                "createDate":"2017-09-23"
              }
            }
          ],
          "must_not":{
            "term":{
              "venderId":3
            }
          }
        }
      }
    }
  }
}
{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "shop",
        "_type": "order",
        "_id": "2",
        "_score": 1,
        "_source": {
          "orderId": "2",
          "venderId": 1,
          "consName": "li si",
          "createDate": "2017-07-13"
        }
      },
      {
        "_index": "shop",
        "_type": "order",
        "_id": "1",
        "_score": 1,
        "_source": {
          "orderId": "1",
          "venderId": 1,
          "consName": "zhang san",
          "createDate": "2017-09-23"
        }
      }
    ]
  }
}

terms 查询

相当于in查询
对order增加tag

POST shop/order/_bulk
{"update":{"_id":1}}
{"doc":{"tag":["sop"]}}
{"update":{"_id":2}}
{"doc":{"tag":["sop","fbp"]}}
{"update":{"_id":3}}
{"doc":{"tag":["loc","fbp"]}}
{"update":{"_id":4}}
{"doc":{"tag":["sop","fbp","loc"]}}

查询商家id为1或2的order

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "venderId": [
            1,2
          ]
        }
      }
    }
  }
}
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "shop",
        "_type": "order",
        "_id": "2",
        "_score": 1,
        "_source": {
          "orderId": "2",
          "venderId": 1,
          "consName": "li si",
          "createDate": "2017-07-13",
          "tag": [
            "sop",
            "fbp"
          ]
        }
      },
      {
        "_index": "shop",
        "_type": "order",
        "_id": "1",
        "_score": 1,
        "_source": {
          "orderId": "1",
          "venderId": 1,
          "consName": "zhang san",
          "createDate": "2017-09-23",
          "tag": [
            "sop"
          ]
        }
      },
      {
        "_index": "shop",
        "_type": "order",
        "_id": "3",
        "_score": 1,
        "_source": {
          "orderId": "3",
          "venderId": 2,
          "consName": "zhang san",
          "createDate": "2017-09-17",
          "tag": [
            "loc",
            "fbp"
          ]
        }
      }
    ]
  }
}

查询商家id为1,tag为fbp的order

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must":[
            {
              "term":{
              "venderId":1   
              }
            },
            {
              "terms":{
                "tag":["fbp"]
              }
            }
            ]
          }
        }
      }
    }
  }
}
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "shop",
        "_type": "order",
        "_id": "2",
        "_score": 1,
        "_source": {
          "orderId": "2",
          "venderId": 1,
          "consName": "li si",
          "createDate": "2017-07-13",
          "tag": [
            "sop",
            "fbp"
          ]
        }
      }
    ]
  }
}

range 范围查询

增加price字段

POST shop/order/_bulk
{"update":{"_id":1}}
{"doc":{"price":100}}
{"update":{"_id":2}}
{"doc":{"price":120}}
{"update":{"_id":3}}
{"doc":{"price":130}}
{"update":{"_id":4}}
{"doc":{"price":180}}

查询price在120到150之间的order

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "price": {
            "gte": 120,
            "lte": 250
          }
        }
      }
    }
  }
}
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "shop",
        "_type": "order",
        "_id": "2",
        "_score": 1,
        "_source": {
          "orderId": "2",
          "venderId": 1,
          "consName": "li si",
          "createDate": "2017-07-13",
          "tag": [
            "sop",
            "fbp"
          ],
          "price": 120
        }
      },
      {
        "_index": "shop",
        "_type": "order",
        "_id": "4",
        "_score": 1,
        "_source": {
          "orderId": "4",
          "venderId": 3,
          "consName": "wang wu",
          "createDate": "2017-09-23",
          "tag": [
            "sop",
            "fbp",
            "loc"
          ],
          "price": 180
        }
      },
      {
        "_index": "shop",
        "_type": "order",
        "_id": "3",
        "_score": 1,
        "_source": {
          "orderId": "3",
          "venderId": 2,
          "consName": "zhang san",
          "createDate": "2017-09-17",
          "tag": [
            "loc",
            "fbp"
          ],
          "price": 130
        }
      }
    ]
  }
}
时间计算表达式

查询9月份之前的order

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "createDate": {
            "lte": "2017-09-17||-17d"
          }
        }
      }
    }
  }
}
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "shop",
        "_type": "order",
        "_id": "2",
        "_score": 1,
        "_source": {
          "orderId": "2",
          "venderId": 1,
          "consName": "li si",
          "createDate": "2017-07-13",
          "tag": [
            "sop",
            "fbp"
          ],
          "price": 120
        }
      }
    ]
  }
}

取当前时间 now

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "createDate": {
            "lte": "now-8M"
          }
        }
      }
    }
  }
}
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "shop",
        "_type": "order",
        "_id": "2",
        "_score": 1,
        "_source": {
          "orderId": "2",
          "venderId": 1,
          "consName": "li si",
          "createDate": "2017-07-13",
          "tag": [
            "sop",
            "fbp"
          ],
          "price": 120
        }
      }
    ]
  }
}

多值搜索

增加备注字段

POST shop/order/_bulk
{"update":{"_id":1}}
{"doc":{"remark":"this is java and elasticsearch book"}}
{"update":{"_id":2}}
{"doc":{"remark":"this a java book"}}
{"update":{"_id":3}}
{"doc":{"remark":"this is a elasticsearch book"}}
{"update":{"_id":4}}
{"doc":{"remark":"this is a java and elasticsearch and spark book"}}

全文检索java书籍(数据比较多,size设置为0了)

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "match":{
          "remark":"java"
        }
      }
    }
  },
  "size": 0
}
{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  }
}
从total看出有3条数据

搜索多个词组

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "match":{
          "remark":"java elasticsearch spark"
        }
      }
    }
  },
  "size": 0
}
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0,
    "hits": []
  }
}

使用and控制必须同时包含"java","spark"

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "match":{
          "remark":{
            "query":"java spark",
            "operator":"and"
          }
        }
      }
    }
  },
  "size": 0
}
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0,
    "hits": []
  }
}

使用math查询keyword类型字段,是否分词

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "match":{
          "consName.keyword":"zhang qi"
        }
      }
    }
  },
  "size": 0
}
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  }
}

使用analyze测试

GET shop/_analyze
{
  "text": "zhang qi",
  "field": "consName.keyword"
}
{
  "tokens": [
    {
      "token": "zhang qi",
      "start_offset": 0,
      "end_offset": 8,
      "type": "word",
      "position": 0
    }
  ]
}

minimum_should_match 控制精准度,可以指定匹配多少个,也可以指定匹配多少

GET shop/order/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "match":{
          "remark":{
            "query":"java elasticsearch spark hadoop",
            "minimum_should_match":3
          }
        }
      }
    }
  },
  "size": 0
}
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0,
    "hits": []
  }
}

你可能感兴趣的:(es 杂记)