Stay Hungry.Stay Foolish.
ElasticSearch使用笔记

很多语句容易忘记,这里记录一下,使用的时候方便直接复制粘贴,就不用各种搜索了,注:文中的indexname指索引名字,typename指类型名字。

mapping

创建索引

curl -XPOST "http://127.0.0.1:9200/indexname"

查看某个索引下面所有的mapping

curl -XGET "http://127.0.0.1:9200/indexname/_mapping?pretty"

查看某个索引下面某个type的mapping

curl -XGET "http://127.0.0.1:9200/indexname/typename/_mapping?pretty"

禁用_all字段

curl -XPUT 'http://localhost:9200/indexname/_mapping/typename?pretty=true' -d '
{
    "typename": {
        "_all": { 
            "enabled": false
        }
    }
}'

通过 include_in_all 选项可以控制字段是否要被包含在 _all 字段中,默认值是 true。在一个对象上设置 include_in_all 可以修改这个对象所有字段的默认行为。 你可能想要保留 _all 字段来查询所有特定的全文字段,例如 title, overview, summary 和 tags。相对于完全禁用 _all 字段,你可以先默认禁用 include_in_all 选项,而选定字段上启用 include_in_all。

curl -XPUT 'http://localhost:9200/indexname/typename/_mapping?pretty=true' -d '
{
    "typename": {
        "include_in_all": false,
        "properties": {
            "title": {
                "type":           "string",
                "include_in_all": true
            },
            "tag": {
                "type":            "string"
            }
        }
    }
}'

谨记 _all 字段仅仅是一个经过分析的 string 字段。它使用默认的分析器来分析它的值,而不管这值本来所在的字段指定的分析器。而且像所有 string 类型字段一样,你可以配置 _all 字段使用的分析器:

curl -XPUT 'http://localhost:9200/indexname/typename/_mapping?pretty=true' -d '
{
    "typename": {
        "_all": { 
            "analyzer": "ik" 
        }
    }
}'

新建立索引,同时设置mapping

curl -XPUT 'http://localhost:9200/indexname/' -d '
{
  "mappings": {
     "typename": {
        "properties": {
           "title": {
              "type": "string",
              "analyzer": "standard",
              "term_vector": "yes"
           }
        }
     }
  }
}'

为已经存在type增加字段

curl -XPUT 'http://localhost:9200/indexname/_mapping/typename' -d '
{
  "properties" : {
    "tag" : {
      "type" :    "string",
      "index":    "not_analyzed"
    }
  }
}
'

插入数据

curl -XPUT 'http://localhost:9200/indexname/typename/1?pretty=true' -d '
{
    "title": "the Good Cats & the Good Dogs!"
}
'

查看title字段如何被索引

curl -XGET 'http://localhost:9200/indexname/typename/1/_termvector?fields=title&pretty=true'

测试字段如何被索引,不用制定type,直接指定索引下面的字段就行

curl -XGET "http://127.0.0.1:9200/indexname/_analyze?field=tag&text=Black-cats&pretty"
curl -XGET "http://127.0.0.1:9200/indexname/_analyze?field=title&text=Black-cats&pretty"

测试分析器

curl -XGET 'http://localhost:9200/indexname/_analyze?analyzer=standard&text=Text-to-analyze&pretty=true'

index参数控制字符串以何种方式被索引。它包含以下三个值当中的一个

  1. analyzed 首先分析这个字符串,然后索引。换言之,以全文形式索引此字段。
  2. not_analyzed 索引这个字段,使之可以被搜索,但是索引内容和指定值一样。不分析此字段。
  3. no 不索引这个字段。这个字段不能为搜索到。

string类型的默认index为analyzed, 对于analyzed类型的字符串字段,使用analyzer参数来指定哪一种分析器将在搜索和索引的时候使用

search

在所以索引中查询模糊匹配

curl -XGET "http://localhost:9200/_search?pretty=true" -d '
{
  "query": {
    "match": {
      "_all": "firebroo"
    }
  }
}'

精确匹配

time curl -XGET "http://localhost:9200/_search?pretty=true" -d '
{
  "query": {
    "match_phrase": {
      "_all": "firebroo"
    }
  }
}'

重建索引批量查询数据

curl -XGET 'http://localhost:9200/indexname/_search?scroll=1m&pretty=true' -d ' 
{
    "query": { "match_all": {}},
    "sort" : ["_doc"], 
    "size":  1000
}'
自由转载-非商用-非衍生-保持署名(创意共享3.0许可证
评论
2017-08-09 07:55:14

firebroo大傻逼

2017-09-18 03:03:08

@firebroo 哈哈

2017-09-18 03:03:53

@Firebroo 绕过

bob
2017-09-18 03:04:12

@fireBroo 绕过了

2017-09-18 03:04:35

@firebroo 绕过了