1 Star 0 Fork 0

dingnate / elasticsearch.demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

类型 表示的数据类型 String string Whole number byte , short , integer , long Floating point float , double Boolean boolean Date date

ElasticSearch入门

ElasticSearch官网

下载

创建文档

指定id

curl -H 'Content-Type: application/json' -XPUT  localhost:9200/website/blog/123?pretty -d '{"title":  "My  first  blog  entry","text": "I am starting to get the hang of this...","date": "2014/01/01"}'
{
  "_index": "website",
  "_type": "blog",
  "_id": "123",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

使用自动生成的ID

curl -H 'Content-Type: application/json' -XPOST  localhost:9200/website/blog?pretty -d '{"title":  "My  second blog  entry","text": "I am using auto generatedId","date": "2014/01/03"}'
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "QSP_XmMBCiEWwbhxyNxi",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

不存在则创建文档

只有在相同的 _index 、 _type 和 _id 不存在时才接受我们的索引请求

op_type参数

curl -H 'Content-Type: application/json' -XPUT  'localhost:9200/website/blog/123?op_type=create&pretty' -d '{"title":  "My  first  blog  entry","text": "I am starting to get the hang of this...","date": "2014/01/02"}'
{
  "error": {
    "type": "version_conflict_engine_exception",
    "reason": "[blog][123]: version conflict, document already exists (current version [2])",
    "index_uuid": "5WSzbmd_Qs-irXSQpAoWbQ",
    "shard": "0",
    "index": "website"
  },
  "status": 409
}

_create后缀

curl -H 'Content-Type: application/json' -XPUT  'localhost:9200/website/blog/123/_create?pretty' -d '{"title":  "My  first  blog  entry","text": "I am starting to get the hang of this...","date": "2014/01/02"}'
结果跟上面的一样

检索文档

简易搜索

  • 查询所有类型为tweet并在tweet字段中包含elasticsearch字符的文档:
GET	/_all/tweet/_search?q=tweet:elasticsearch
  • 查找name字段中包含"john"和tweet字段包含"mary"的结果。实际的查询只需要:
GET	/_search?q=%2Bname%3Ajohn+%2Btweet%3Amary

注:"+"前缀表示语句匹配条件必须被满足,"-"前缀表示条件必须不被满足,上时url编码编码后字符串

  • 包含"mary"字符的所有文档的简单搜索:
GET	/_search?q=mary

检查文档是否存在

curl -I http://localhost:9200/website/blog/123
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 192

通过id查询

curl http://localhost:9200/website/blog/123?pretty
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "title" : "My  first  blog  entry",
    "text" : "I am starting to get the hang of this...",
    "date" : "2014/01/02"
  }
}

通过 _search 查询

curl -H 'Content-Type:application/json' -XGET 'http://localhost:9200/website/blog/_search?pretty' -d '{"query":{"match":{"_id":"123"}}}'
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "123",
        "_score" : 1.0,
        "_source" : {
          "title" : "My  first  blog  entry",
          "text" : "I am starting to get the hang of this...",
          "date" : "2014/01/02"
        }
      }
    ]
  }
}

_source路径

只查询_source部分

curl -H 'Content-Type:application/json' -XGET 'http://localhost:9200/website/blog/123/_source?pretty'
{
  "title" : "My  first  blog  entry",
  "text" : "I am starting to get the hang of this...",
  "date" : "2014/01/02"
}

_source参数

检索文档的一部分

curl -H 'Content-Type:application/json' -XGET 'http://localhost:9200/website/blog/123/_source?_source=title,text&pretty=true'
{
  "text" : "I am starting to get the hang of this...",
  "title" : "My  first  blog  entry"
}

删除文档

curl -XDELETE http://localhost:9200/website/blog/123?pretty
{
  "_index": "website",
  "_type": "blog",
  "_id": "123",
  "_version": 3,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 3
}

更新文档

curl -H 'Content-Type: application/json' -XPUT  localhost:9200/website/blog/123?pretty -d '{"title":  "My  first  blog  entry","text": "I am starting to get the hang of this...","date": "2014/01/02"}'
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

乐观并发锁控制

创建doc

curl -H 'Content-Type: application/json' -XPUT  'localhost:9200/website/blog/1/_create?pretty' -d '{"title": "My first blog entry", "text":  "Just trying this out..."}'
curl  'localhost:9200/website/blog/1?pretty'
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "title" : "My first blog entry",
    "text" : "Just trying this out..."
  }
}
  • 指定version参数
curl -H 'Content-Type: application/json' -XPUT  'localhost:9200/website/blog/1?version=1&pretty' -d '{"title": "My  first  blog  entry", "text": "Starting  to  get  the  hang  of  this..."}'
{
  "_index": "website",
  "_type": "blog",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 3
}

继续用version=1去更新时会返回错误,因为当前的version=2

{
  "error": {
    "type": "version_conflict_engine_exception",
    "reason": "[blog][1]: version conflict, current version [2] is different than the one provided [1]",
    "index_uuid": "5WSzbmd_Qs-irXSQpAoWbQ",
    "shard": "3",
    "index": "website"
  },
  "status": 409
}
  • version_type=external

    不再检查_version 是否与请求中指定的一致,而是检查是否小于指定的版本。如果请求成功,外部版本号就会被存储。版

    号必须是整数,大于零小于 9.2e+18 ——Java中的正的long

curl -H 'Content-Type: application/json' -XPUT  'localhost:9200/website/blog/2?version=5&version_type=external&pretty' -d '{"title": "My  first  blog  entry", "text": "Starting  to  get  the  hang  of  this..."}'
{
  "_index": "website",
  "_type": "blog",
  "_id": "2",
  "_version": 5,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 3
}

文档局部更新

curl -H 'Content-Type: application/json' -XPOST  'localhost:9200/website/blog/1/_update?pretty' -d '{ "doc":{"tags":["testing"],"views":0}}'
{
  "_index": "website",
  "_type": "blog",
  "_id": "1",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 3
}

使用脚本局部更新 (groovy)

脚本能够使用updateAPI改变_source字段的内容,它在脚本内部以ctx._source表示。例如,我们可以使用脚本增加博客的views数量:

curl -H 'Content-Type: application/json' -XPOST  'localhost:9200/website/blog/1/_update?pretty' -d '{"script":"ctx._source.views+=1"}'

查看更新后的结果:

curl localhost:9200/website/blog/1?pretty
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "1",
  "_version" : 4,
  "found" : true,
  "_source" : {
    "title" : "My  first  blog  entry",
    "text" : "Starting  to  get  the  hang  of  this...",
    "views" : 1,
    "tags" : [
      "testing"
    ]
  }
}

以使用脚本增加一个新标签到tags数组中,使用脚本参数:

curl -H 'Content-Type: application/json' -XPOST  'localhost:9200/website/blog/1/_update?pretty' -d '{"script":{"inline":"ctx._source.tags.add(params.new_tag)","params":{"new_tag":"search"}}}'

查看更新后的结果:

curl 'localhost:9200/website/blog/1?pretty'
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "1",
  "_version" : 5,
  "found" : true,
  "_source" : {
    "title" : "My  first  blog  entry",
    "text" : "Starting  to  get  the  hang  of  this...",
    "views" : 1,
    "tags" : [
      "testing",
      "search"
    ]
  }
}

ctx.op

通过设置ctx.op为delete我们可以根据内容删除文档

注:5.6.4、6.2.4版本测试不支持下面的操作

curl -H 'Content-Type: application/json' -XPOST  'localhost:9200/website/blog/1/_update?pretty' -d '{"script":{"inline":"ctx.op=ctx._source.views==params.count?'delete':'none'","params":{"count":1}}}'

更新可能不存在的文档

upsert

在这种情况下,我们可以使用upsert参数定义文档来使其不存在时被创建。

curl -H 'Content-Type: application/json' -XPOST  'localhost:9200/website/pageviews/1/_update?pretty' -d ' {"script":"ctx._source.views+=1","upsert":{"views":1}}'

查看更新后的结果:

curl 'localhost:9200/website/pageviews/1?pretty'
{
  "_index" : "website",
  "_type" : "pageviews",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "views" : 1
  }
}

更新和冲突

retry_on_conflict

默认值为0

对于多用户的局部更新,文档被修改了并不要紧。例如,两个进程都要增加页面浏览量,增加的顺序我们并不关心——如果冲突发生,我们唯一要做的仅仅是重新尝试更新既可。 这些可以通过retry_on_conflict参数设置重试次数来自动完成,这样update操作将会在发生错误前重试,下面的例子中retry_on_conflict=5

curl -H 'Content-Type: application/json' -XPOST  'localhost:9200/website/pageviews/1/_update?retry_on_conflict=5&pretty' -d ' {"script":"ctx._source.views+=1","upsert":{"views":0}}'

查看更新后的结果:

curl localhost:9200/website/pageviews/1?pretty
{
  "_index" : "website",
  "_type" : "pageviews",
  "_id" : "1",
  "_version" : 5,
  "found" : true,
  "_source" : {
    "views" : 2
  }
}
  • 接受一个version参数

    许你使用乐观并发控制(optimistic concurrency control)来指定你要更细文档的版本。

mget批量检索

参数是一个docs数组,数组的每个节点定义一个文档的_index_type_id元数据。如果你只想检索一个或几个确定的字段,也可以定义一个_source参数:

curl -H 'Content-Type: application/json' -XPOST  'localhost:9200/_mget?pretty' -d '{"docs":[{"_index":"website","_type":"blog","_id":2},{"_index":"website","_type":"pageviews","_id":1,"_source":"views"}]}'

响应体也包含一个docs数组,每个文档还包含一个响应,它们按照请求定义的顺序排列。每个这样的响应与单独使用get request响应体相同:

{
"docs" : [
  {
    "_index" : "website",
    "_type" : "blog",
    "_id" : "2",
    "_version" : 5,
    "found" : true,
    "_source" : {
      "title" : "My  first  blog  entry",
      "text" : "Starting  to  get  the  hang  of  this..."
    }
  },
  {
    "_index" : "website",
    "_type" : "pageviews",
    "_id" : "1",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "views" : 0
    }
  }
]
}

如果你想检索的文档在同一个_index中(甚至在同一个_type中),你就可以在URL中定义 一个默认的/_index或者/_index/_type

curl -H 'Content-Type: application/json' -XPOST  'localhost:9200/website/blog/_mget?pretty' -d  '{"docs":[{"_id":2},{"_type":"pageviews","_id":1}]}'
{
  "docs" : [
    {
      "_index" : "website",
      "_type" : "blog",
      "_id" : "2",
      "_version" : 5,
      "found" : true,
      "_source" : {
        "title" : "My  first  blog  entry",
        "text" : "Starting  to  get  the  hang  of  this..."
      }
    },
    {
      "_index" : "website",
      "_type" : "pageviews",
      "_id" : "1",
      "_version" : 1,
      "found" : true,
      "_source" : {
        "views" : 0
      }
    }
  ]
}

如果所有文档具有相同_index_type,你可以通过简单的ids数组来代替完整 的docs数组:

curl -H 'Content-Type: application/json' -XPOST  'localhost:9200/website/blog/_mget?pretty' -d  '{"ids":["2","1"]}'
{
  "docs" : [
    {
      "_index" : "website",
      "_type" : "blog",
      "_id" : "2",
      "_version" : 5,
      "found" : true,
      "_source" : {
        "title" : "My  first  blog  entry",
        "text" : "Starting  to  get  the  hang  of  this..."
      }
    },
    {
      "_index" : "website",
      "_type" : "blog",
      "_id" : "1",
      "_version" : 2,
      "found" : true,
      "_source" : {
        "title" : "My  first  blog  entry",
        "text" : "Starting  to  get  the  hang  of  this..."
      }
    }
  ]
}

bulk批量更新

bulk API允许我们使用单一请求来实现多个 文档的create、index、update或delete。这对索引类似于日志活动这样的数据流非常有 用,它们可以以成百上千的数据为一个批次按序进行索引

bulk请求体如下,它有一点不同寻常:

{ action: { metadata }}\n { request body }\n { action: { metadata }}\n { request body }\n ...

注意:

  • 每行必须以"\n"符号结尾,包括最后一行。这些都是作为每行有效的分离而做的标记。
  • 每一行的数据不能包含未被转义的换行符,它们会干扰分析——这意味着JSON不能被美化打印。

行为(action)必须是以下几种:

行为 解释
create 当文档不存在时创建之。详见《创建文档》
index 创建新文档或替换已有文档。见《索引文档》和《更新文档》
update 局部更新文档。见《局部更新》
delete 删除一个文档。见《删除文档》

注:delete操作不需要请求体(requestbody),create、index、update操作需要请求体。

delete

{"delete":{"_index":"website","_type":"blog","_id":"123"}}

create

{"create":{"_index":"website","_type":"blog","_id":"123"}}
{"title":"Myfirstblogpost"}

index

{"index":{"_index":"website","_type":"blog"}}
{"title":"Mysecondblogpost"}

update

{"update":{"_index":"website","_type":"blog","_id":"123","_retry_on_conflict":"5"}}
{"doc":{"title":"Myupdatedblogpost"}}

批量更新

注:在shell中输入时,每行尾使用enter键代替\n

curl -H 'Content-Type: application/json' -XPOST 'localhost:9200/_bulk?pretty' -d '
{"delete":{"_index":"website","_type":"blog","_id":"123"}}\n
{"create":{"_index":"website","_type":"blog","_id":"123"}}\n
{"title":"Myfirstblogpost"}\n
{"index":{"_index":"website","_type":"blog"}}\n
{"title":"Mysecondblogpost"}\n
{"update":{"_index":"website","_type":"blog","_id":"123","_retry_on_conflict":"5"}}\n
{"doc":{"title":"Myupdatedblogpost"}}\n'
{
  "took" : 291,
  "errors" : false,
  "items" : [
    {
      "delete" : {
        "found" : false,
        "_index" : "website",
        "_type" : "blog",
        "_id" : "123",
        "_version" : 1,
        "result" : "not_found",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "status" : 404
      }
    },
    {
      "create" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "123",
        "_version" : 2,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "AWNsti9ewhD4-ecniVQP",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    },
    {
      "update" : {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "123",
        "_version" : 3,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "status" : 200
      }
    }
  ]
}

聚合

aggs

有一个功能叫做聚合(aggregations),它允许你在数据上生成复杂的分析统计。它很像SQL 中的GROUP BY但是功能更强大。举个例子,让我们找到所有职员中最大的共同点(兴趣爱好)是什么:

GET	/megacorp/employee/_search
{
  "aggs": {
    "all_interests": {
      "terms": {"field": "interests"}    
    }
  }
}

如果我们想知道所有姓"Smith"的人最大的共同点(兴趣爱好),我们只需要增加合适的语句既可:

GET	/megacorp/employee/_search
{
  "query": {
    "match": {"last_name": "smith"} 
	},
  "aggs": {
    "all_interests": {
      "terms": {"field": "interests"} 
  	}
  }
}

all_interests聚合已经变成只包含和查询语句相匹配的文档了:

	...
  "all_interests": {
    "buckets": [
      {
        "key": "music",
        "doc_count": 2
      },
      {
        "key": "sports",
        "doc_count": 1
      }
    ]
  }
}

分页

from和size参数

size:结果数,默认 10 from:跳过开始的结果数,默认 0

如果你想每页显示5个结果,页码从1到3,那请求如下:

GET	/_search?size=5
GET	/_search?size=5&from=5
GET	/_search?size=5&from=10

分析器

指定分析器解析

GET	/_analyze?analyzer=standard&text=Text	to	analyze

映射

查看映射

GET	/index/_mapping/type
{
  "website" : {
    "mappings" : {
      "blog" : {
        "properties" : {
          "date" : {
            "type" : "date",
            "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
          },
          "text" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

字段映射

{
  "tag": {
    "type": "string",
    "index": "not_analyzed",
	"analyzer":	"english"
  }
}

type

Elasticsearch支持以下简单字段类型:

类型 表示的数据类型
String string
Whole number short, integer, long
Floating point float ,double
Boolean boolean
Date date

index

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

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

analyzer

对于 analyzed类型的字符串字段,使用 analyzer参数来指定哪一种分析器将在搜索和索引 的时候使用。默认的,Elasticsearch使用 standard分析器,但是你可以通过指定一个内建的 分析器来更改它

例如 whitespacesimpleenglish

更新映射

你可以向已有映射中增加字段,但你不能修改它。

创建映射

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

结构化查询(Query DSL)

match_all

下方两行代码是等价的

curl 'localhost:9200/_search?pretty' -d '{"query":{"match_all":{}}}'
curl 'localhost:9200/_search?pretty' -d '{}'

match

查询title字段中包含“my”

curl 'localhost:9200/_search?pretty' -d '{"query":{"match":{"title":"my"}}}'

multi_match

multi_match 查询允许你做match查询的基础上同时搜索多个字段:

{
  "multi_match": {
    "query": "full text search",
    "fields": ["title", "body"] }
}

bool

查询子句就像是搭积木一样,可以合并简单的子句为一个复杂的查询语句。

  • 叶子子句(leaf clauses)(比如 match子句)用以在将查询字符串与一个字段(或多字段)进行比较
  • 复合子句(compound)用以合并其他的子句。

例如,bool子句允许你合并其他的合法子 句,must,must_not或者should,如果可能的话:

{
  "bool": {
    "must": {"match": {"tweet": "elasticsearch"} },
    "must_not": {"match": {"name": "mary"} },
    "should": {"match": {"tweet": "fulltext"} }
  }
}

过滤

term

term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed的字符串(未经分析的文本数据类型):

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

terms

terms跟term有点类似,但terms允许指定多个匹配条件。如果某个字段指定了多个 值,那么文档需要一起去做匹配:

{
  "terms": {
    "tag": ["search", "full_text", "nosql"] 
	}
}

range

range过滤允许我们按照指定范围查找一批数据:

{
  "range": {
    "age": {
      "gte": 20,
      "lt": 30
    }
  }
}

范围操作符包含:

名称 解释
gt 大于
gte 大于等于
lt 小于
lte 小于等于

exists|missing

exists和missing过滤可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的 IS_NULL条件

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

bool

bool过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:

操作符 解释
must 多个查询条件的完全匹配,相当于and。
must_not 多个查询条件的相反匹配,相当于not。
should 至少有一个查询条件匹配,相当于 or。

这些参数可以分别继承一个过滤条件或者一个过滤条件的数组:

{
  "bool": {
    "must": {
      "term": {"folder": "inbox"}
  	},
    "must_not": {
      "term": {"tag": "spam"}
  	},
    "should": [
      {"term": {"starred": true } },
      {"term": {"unread": true } }
    ]
  }
}

带过滤的查询语句

searchAPI中只能包含query语句,所以我们需要用filtered来同时包含"query"和 "filter"子句,在外层再加入query的上下文关系

{
	"query":{
	  "filtered": {
	    "query": {"match": {"email": "businessopportunity"}},
	    "filter": {"term": {"folder": "inbox"} }
		}
	}
}

rollover

Elasticsearch 从 5.0 开始,为日志场景的用户提供了一个很不错的接口,叫 rollover。其作用是:当某个别名指向的实际索引过大的时候,自动将别名指向下一个实际索引。

创建一个开始滚动的起始索引:

curl -XPUT 'http://localhost:9200/logstash-2016.11.25-1' -d '{
"aliases":{
"logstash":{}
}
}'

然后就可以尝试发起 rollover 请求了:

curl -XPOST 'http://localhost:9200/logstash/_rollover' -d '{
"conditions":{
"max_age":"1d",
"max_docs":10000000,
"max_size":"500gb",
}
}'

上面的定义意思就是:当索引超过 1 天,或者索引内的数据量超过一千万条的时候,自动创建并指向下一个索引。

索引(index)

查看所有索引

curl -XGET 'localhost:9200/_cat/indices?h=i&s=i:desc'

健康指标只显示index名:h=i

按索引名倒排序:s=i:desc

删除索引

curl -XGET 'localhost:9200/index_name'

排序

相关性排序

_score

为了使结果可以按照相关性进行排序,我们需要一个相关性的值。在ElasticSearch的查询结果中,相关性分值会用_score字段来给出一个浮点型的数值,所以默认情况下,结果集以 _score进行倒序排列。

字段值排序

sort

单级排序
curl -XGET 'localhost:9200/_search' -d '{"query":{"filtered":{"filter":{"term":{"user_id":1}}}},"sort":{"date":{"order":"desc"}}}'

_scoremax_score字段都为null。计算_score是比较消耗性能的,而且通常主要用作排序--我们不是用相关性进行排序的时候,就不需要统计其相关性。如果你想强 制计算其相关性,可以设置track_scorestrue

注:字段值默认以顺序排列,而_score默认以倒序排列。

多级排序
curl -XGET 'localhost:9200/_search' -d '{"query":{"filtered":{"filter":{"term":{"user_id":1}}}},"sort":[{"date":{"order":"desc"}},{"location":{"order":"desc"}}]}'
字符串参数排序
GET	/_search?sort=date:desc&sort=_score&q=search
为多值字段排序
数字和日期

你可以从多个值中取出一个来进行排序,你可以使用min,max,avg或sum这些模式。比说你可以在dates字段中用最早的日期来进行排序:

{"sort": {
	"dates": {
      "order": "asc",
      "mode": "min"
    }
 } }
字符串

修改多值字段的mapping,新增的tweet.raw子字段索引方式是not_analyzed。

{"tweet": {
    "type": "string",
    "analyzer": "english",
    "fields": {
      "raw": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
 }}

现在,在给数据重建索引后,我们既可以使用tweet字段进行全文本搜索,也可以用tweet.raw字段进行排序:

警告:对analyzed字段进行强制排序会消耗大量内存。详情请查阅《字段类型简介》相关内容。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2018 dingnate Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

ElasticSearch入门 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/dingnate/elasticsearch.demo.git
git@gitee.com:dingnate/elasticsearch.demo.git
dingnate
elasticsearch.demo
elasticsearch.demo
master

搜索帮助