ElasticSearch入门


http://localhost:9200/
{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":1
    },
    "mappings":{
        "novel":{
            "properties":{
                "word_count":{
                    "type":"integer"
                },
                "author":{
                    "type":"keyword"
                },
                "title":{
                    "type":"text"
                },
                "public_date":{
                    "type":"date",
                    "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                }
            }
        }
    }
}
http://localhost:9200/book/novel/_mappings
{
  "novel": {
    "properties": {
      "word_count": {
        "type": "integer"
      },
      "author": {
        "type": "keyword"
      },
      "title": {
        "type": "text"
      },
      "public_date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}
http://localhost:9200/people/man/1
{
  "name":"张枋",
  "country":"China",
  "age":26,
  "date":"1992-01-28"
}

http://localhost:9200/people/man/1
{
  "script":{
    "lang":"painless",
    "inline":"ctx._source.age=params.age",
    "params":{
      "age":100
    }
  }
}

query:(会计算匹配程度score,而filter只判断文档是否满足条件)
http://localhost:9200/book/novel/5
post http://localhost:9200/book/_search
{
  "query":{
    "match_all":{}
  },
  "from":1,
  "size":1
}

{
  "query": {
    "match": {
      "title": "雪"
    }
  },
  "sort": [
    {
      "public_date": {
        "order": "desc"
      }
    }
  ]
}

{
  "query": {
    "match": {
      "title": "雪"
    }
  },
  "sort": [
    {
      "public_date": {
        "order": "desc"
      }
    }
  ],
  "from": 1,
  "size": 1
}

聚合查询

{
  "aggs": {
    "group_by_word_count": {
      "terms": {
        "field": "word_count"
      }
    },
    "group_by_public_date": {
      "terms": {
        "field": "public_date"
      }
    },
    "grades_word_count": {
      "stats": {
        "field": "word_count"
      }
    },
    "grades_word_count_min": {
      "min": {
        "field": "word_count"
      }
    }
  }
}

非结构化查询
{
  "query":{
    "match_phrase":{
      "title":"雪"
    }
  }
}

{
  "query": {
    "multi_match": {
      "query": "雪人",
      "fields": [
        "author",
        "title"
      ]
    }
  }
}

{
  "query": {
    "query_string": {
      "query": "(雪 AND 人) OR 园"
    }
  }
}

{
  "query": {
    "query_string": {
      "query": "雪 OR 人",
      "fields": [
        "title",
        "author"
      ]
    }
  }
}

字段查询(结构化)
{
  "query": {
    "term": {
      "author": "鲁迅"
    }
  }
}
{
  "query": {
    "range": {
      "word_count": {
        "gte":100,//gt   e:equals
        "lte":1000//lt
      }
    }
  }
}

filter:(需要结合bool使用,查询结果通过true|false过滤,查询速度快于query)
{
  "query":{
    "bool":{
      "filter":{
        "term":{
          "word_count":500
        }
      }
    }
  }
}
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "author": "毛泽东"
          }
        },
        {
          "match": {
            "title": "雪"
          }
        }
      ],
      "filter":[
        {
          "term":{
            "word_count":500
          }
        }
      ]
    }
  }
}
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "author": "毛泽东"
          }
        },
        {
          "match": {
            "title": "雪"
          }
        }
      ]
    }
  }
}
全文搜索:
http://localhost:9200/_search
{
  "query":{
    "match":{
      "title":"雪"
    }
  }
}
固定分数查询
{
  "query": {
    "constant_score": {
      "filter": {
        "match": {
          "author": "毛泽东"
        }
      },
      "boost": 2
    }
  }
}

spring集成es:
https://www.cnblogs.com/ouyanxia/p/8288749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值