DLX Dev Everything Engineer

elasticsearch入门备忘

2020-06-08

想不到官方文档还有好几个版本,先看了《Elasticsearch - The Definitive Guide:》,后面再去官网点进去看居然发现内容不一样了,原来是没找对回家的路,应该看这个:

官方文档

Query-String 方式查询

curl -X GET "localhost:9200/_search?pretty"

{
   "hits" : {
      "total" :       14,
      "hits" : [
        {
          "_index":   "us",
          "_type":    "tweet",
          "_id":      "7",
          "_score":   1,
          "_source": {
             "date":    "2014-09-17",
             "name":    "John Smith",
             "tweet":   "The Query DSL is really powerful and flexible",
             "user_id": 2
          }
       },
        ... 9 RESULTS REMOVED ...
      ],
      "max_score" :   1
   },
   "took" :           4,
   "_shards" : {
      "failed" :      0,
      "successful" :  10,
      "total" :       10
   },
   "timed_out" :      false
}
curl -X GET "localhost:9200/_search?size=5&pretty"
curl -X GET "localhost:9200/_search?size=5&from=5&pretty"
curl -X GET "localhost:9200/_search?size=5&from=10&pretty"

#finds all documents of type `tweet` that contain the word `elasticsearch` in the `tweet` field:
curl -X GET "localhost:9200/_all/tweet/_search?q=tweet:elasticsearch&pretty"

#query looks for john in the name field and mary in the tweet field.
#+name:john +tweet:mary
curl -X GET "localhost:9200/_search?q=%2Bname%3Ajohn+%2Btweet%3Amary&pretty"

#This simple search returns all documents that contain the word mary:
curl -X GET "localhost:9200/_search?q=mary&pretty"

#The name field contains mary or john
#The date is greater than 2014-09-10
#The _all field contains either of the words aggregations or geo
#+name:(mary john) +date:>2014-09-10 +(aggregations geo)
?q=%2Bname%3A(mary+john)+%2Bdate%3A%3E2014-09-10+%2B(aggregations+geo)	

Full-Body 查询

empty:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{}
'

match all:

#matches all documents:
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "match_all": {}
    }
}
'

match:

#For instance, you can use a match query clause to find tweets that mention elasticsearch in the tweet field:
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "match": {
            "tweet": "elasticsearch"
        }
    }
}
'

bool:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "bool": {
        "must":     { "match": { "tweet": "elasticsearch" }},
        "must_not": { "match": { "name":  "mary" }},
        "should":   { "match": { "tweet": "full text" }},
        "filter":   { "range": { "age" : { "gt" : 30 }} }
    }
}
'

multi match:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "multi_match": {
        "query":    "full text search",
        "fields":   [ "title", "body" ]
    }
}
'

range query:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "range": {
        "age": {
            "gte":  20,
            "lt":   30
        }
    }
}
'

term query:

{ "term": { "age":    26           }}
{ "term": { "date":   "2014-09-01" }}
{ "term": { "public": true         }}
{ "term": { "tag":    "full_text"  }}

exists query:

{
    "exists":   {
        "field":    "title"
    }
}

Mapping

Testing Analyzers

curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "analyzer": "standard",
  "text": "Text to analyze"
}
'

{
   "tokens": [
      {
         "token":        "text",
         "start_offset": 0,
         "end_offset":   4,
         "type":         "<ALPHANUM>",
         "position":     1
      },
      {
         "token":        "to",
         "start_offset": 5,
         "end_offset":   7,
         "type":         "<ALPHANUM>",
         "position":     2
      },
      {
         "token":        "analyze",
         "start_offset": 8,
         "end_offset":   15,
         "type":         "<ALPHANUM>",
         "position":     3
      }
   ]
}

Mapping

curl -X PUT "localhost:9200/gb?pretty" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "tweet" : {
      "properties" : {
        "tweet" : {
          "type" :    "string",
          "analyzer": "english"
        },
        "date" : {
          "type" :   "date"
        },
        "name" : {
          "type" :   "string"
        },
        "user_id" : {
          "type" :   "long"
        }
      }
    }
  }
}
'

#增加一个
curl -X PUT "localhost:9200/gb/_mapping/tweet?pretty" -H 'Content-Type: application/json' -d'
{
  "properties" : {
    "tag" : {
      "type" :    "string",
      "index":    "not_analyzed"
    }
  }
}
'

Index相关

关闭index自动创建

action.auto_create_index: false

Index setting

PUT /my_temp_index
{
    "settings": {
        "number_of_shards" :   1,
        "number_of_replicas" : 0
    }
}

_source字段

PUT /my_index
{
    "mappings": {
        "my_type": {
            "_source": {
                "enabled":  false
            }
        }
    }
}

GET /_search
{
    "query":   { "match_all": {}},
    "_source": [ "title", "created" ]
}

_all字段

GET /_search
{
    "match": {
        "_all": "john smith marketing"
    }
}

PUT /my_index/_mapping/my_type
{
    "my_type": {
        "_all": { "enabled": false }
    }
}

PUT /my_index/my_type/_mapping
{
    "my_type": {
        "include_in_all": false,
        "properties": {
            "title": {
                "type":           "string",
                "include_in_all": true
            },
            ...
        }
    }
}
    
PUT /my_index/my_type/_mapping
{
    "my_type": {
        "_all": { "analyzer": "whitespace" }
    }
}


Similar Posts

Comments