ElasticSearch学习之路-day16

转载自:https://blog.csdn.net/chengyuqiang/column/info/18392,ES版本号6.3.0

创建索引

PUT website
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 5
  },
  "mappings": {
    "blog":{
      "properties": {
        "title":{
          "type": "text",
          "analyzer": "ik_max_word"
        },
        "author":{
          "type": "text"
        },
        "postdate":{
          "type": "date",
          "format": "yyyy-MM-dd"
        },
        "abstract":{
          "type": "text",
          "analyzer": "ik_max_word"
        },
        "url":{
          "type": "text"
        }
      }
    }
  }
}

准备数据

POST /_bulk
{ "index":{ "_index": "website", "_type": "blog", "_id": "1" }}
{ "title": "Ambari源码编译","author":"程裕强","postdate":"2016-12-21","abstract":"CentOS7.x下的Ambari2.4源码编译","url":"http://url.cn/53788351"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "2" }}
{ "title": "watchman源码编译","author":"程裕强","postdate":"2016-12-23","abstract":"CentOS7.x的watchman源码编译","url":"http://url.cn/53844169"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "3" }}
{ "title": "CentOS升级gcc","author":"程裕强","postdate":"2016-12-25","abstract":"CentOS升级gcc","url":"http://url.cn/53868915"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "4" }}
{ "title": "vmware复制虚拟机","author":"程裕强","postdate":"2016-12-29","abstract":"vmware复制虚拟机","url":"http://url.cn/53946664"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "5" }}
{ "title": "libstdc++.so.6","author":"程裕强","postdate":"2016-12-30","abstract":"libstdc++.so.6问题解决","url":"http://url.cn/53946911"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "6" }}
{ "title": "CentOS更换国内yum源","author":"程裕强","postdate":"2016-12-30","abstract":"CentOS更换国内yum源","url":"http://url.cn/53946911"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "7" }}
{ "title": "搭建Ember开发环境","author":"程裕强","postdate":"2016-12-30","abstract":"CentOS下搭建Ember开发环境","url":"http://url.cn/53947507"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "8" }}
{ "title": "es高亮","author":"程裕强","postdate":"2017-01-03","abstract":"Elasticsearch查询关键字高亮","url":"http://url/53991802"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "9" }}
{ "title": "to be or not to be","author":"somebody","postdate":"2018-01-03","abstract":"to be or not to be,that is the question","url":"http://url/63991802"}

term查询
 

GET website/_search
{
  "query": {
    "term": {
      "title":"vmware"
    }
  }
}

返回结果

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "4",
        "_score": 0.9227539,
        "_source": {
          "title": "vmware复制虚拟机",
          "author": "程裕强",
          "postdate": "2016-12-29",
          "abstract": "vmware复制虚拟机",
          "url": "http://url.cn/53946664"
        }
      }
    ]
  }
}

分页查询

GET website/_search
{
  "from": 0,
  "size": 3,
  "query": {
    "match_all": {}
  }
}

返回

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": 1,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "5",
        "_score": 1,
        "_source": {
          "title": "libstdc++.so.6",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "libstdc++.so.6问题解决",
          "url": "http://url.cn/53946911"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "8",
        "_score": 1,
        "_source": {
          "title": "es高亮",
          "author": "程裕强",
          "postdate": "2017-01-03",
          "abstract": "Elasticsearch查询关键字高亮",
          "url": "http://url/53991802"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "9",
        "_score": 1,
        "_source": {
          "title": "to be or not to be",
          "author": "somebody",
          "postdate": "2018-01-03",
          "abstract": "to be or not to be,that is the question",
          "url": "http://url/63991802"
        }
      }
    ]
  }
}

过滤字段
 

GET website/_search
{
  "_source": ["title","author"],
  "query": {
    "term": {
      "title": "centos"
    }
  }
}

返回

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "author": "程裕强",
          "title": "CentOS更换国内yum源"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "author": "程裕强",
          "title": "CentOS升级gcc"
        }
      }
    ]
  }
}

显示version
 

GET website/_search
{
  "_source": ["title"],
  "version": true, 
  "query": {
    "term": {
      "title": "centos"
    }
  }
}

返回

扫描二维码关注公众号,回复: 6532191 查看本文章
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_version": 1,
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更换国内yum源"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_version": 1,
        "_score": 0.2876821,
        "_source": {
          "title": "CentOS升级gcc"
        }
      }
    ]
  }
}

评分过滤

GET website/_search
{
  "min_score":"0.5",
  "query": {
    "term": {
      "title":"centos"
    }
  }
}

返回

{
  "took": 43,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更换国内yum源",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "CentOS更换国内yum源",
          "url": "http://url.cn/53946911"
        }
      }
    ]
  }
}

高亮关键字

GET website/_search
{
  "query": {
    "term": {
      "title":"centos"
    }
  },
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}

返回

{
  "took": 124,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更换国内yum源",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "CentOS更换国内yum源",
          "url": "http://url.cn/53946911"
        },
        "highlight": {
          "title": [
            "<em>CentOS</em>更换国内yum源"
          ]
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "title": "CentOS升级gcc",
          "author": "程裕强",
          "postdate": "2016-12-25",
          "abstract": "CentOS升级gcc",
          "url": "http://url.cn/53868915"
        },
        "highlight": {
          "title": [
            "<em>CentOS</em>升级gcc"
          ]
        }
      }
    ]
  }
}

猜你喜欢

转载自blog.csdn.net/qq_23536449/article/details/91365617
今日推荐