映射
mapping.json
{
"book": {
"_index": {
"enabled": true
},
"_id": {
"index": "not_analyzed",
"store": "yes"
},
"properties": {
"author": {
"type": "string"
},
"characters": {
"type": "string"
},
"copies": {
"type": "string"
},
"otitle": {
"type": "string"
},
"tags": {
"type": "string"
},
"title": {
"type": "string"
},
"year": {
"type": "long",
"ignore_malformed": false,
"index": "analyzed"
},
"available": {
"type": "boolean"
}
}
}
}
建立索引
curl -XPOST 'localhost:9200/libbrary'
curl -XPUT 'localhost:9200/library/book/_mapping' -d @mapping.json
documents.json
{ "index": {"_index": "library", "_type": "book", "_id": "1"}} { "title": "All Quiet on the Western Front","otitle": "Im Westen nichts Neues","author": "Erich Maria Remarque","year": 1929,"characters": ["Paul Bäumer", "Albert Kropp", "Haie Westhus", "Fredrich Müller", "Stanislaus Katczinsky", "Tjaden"],"tags": ["novel"],"copies": 1, "available": true, "section" : 3} { "index": {"_index": "library", "_type": "book", "_id": "2"}} { "title": "Catch-22","author": "Joseph Heller","year": 1961,"characters": ["John Yossarian", "Captain Aardvark", "Chaplain Tappman", "Colonel Cathcart", "Doctor Daneeka"],"tags": ["novel"],"copies": 6, "available" : false, "section" : 1} { "index": {"_index": "library", "_type": "book", "_id": "3"}} { "title": "The Complete Sherlock Holmes", "author": "Arthur Conan Doyle","year": 1936,"characters": ["Sherlock Holmes","Dr. Watson", "G. Lestrade"],"tags": [],"copies": 0, "available" : false, "section" : 12} { "index": {"_index": "library", "_type": "book", "_id": "4"}} { "title": "Crime and Punishment","otitle": "Преступлéние и наказáние","author": "Fyodor Dostoevsky","year": 1886,"characters": ["Raskolnikov", "Sofia Semyonovna Marmeladova"],"tags": [],"copies": 0, "available" : true}
获取文档版本版本值
{
"version": true,
"query": {
"query_string": {
"query": "title:crime"
}
}
}
搜索类型 search_type
通过指定搜索类型来选择查询在内部如何处理
query_then_fetch 返回结果最大数量=size参数的值,默认;1、执行查询得到对文档进行排序和分级所需信息,该步在所有分片上执行;2、只在相关分片上查询文档的实际内容;
query_and_fetch 返回结果最大数量=size参数的值*分片数量 查询在所有分片上并行执行(当然,任意一个主分片,只查询一个副本),所有分片返回等于size值得结果数;
dfs_query_and_fetch 类似query_and_fetch,但相比query_and_fetch包含一个额外的阶段,在初始查询中执行分布式词频计算,以得到返回文件的更精确的得分,从而上查询结果更相关;
dfs_query_then_fetch 类似query_then_fetch,但相比query_and_fetch包含一个额外的阶段;
count 只返回匹配查询的文档数
scan 另一个特殊类型的搜索类型,只有在要查询返回大量结果时才用;在发送第一个请求后,Elasticsearch响应一个滚动标识符,类似于关系型数据库中的游标;
搜索执行偏好
控制查询如何执行
控制查询在哪些分片执行
默认,Elasticsearch使用额分片和副本,既包含我们已经发送过请求的可用节点,又包括
集群中的其他节点。
_primary 只在主分片上执行检索,不适用副本;当想使用索引猴子那个最近更新的、还没复制到副本中的信息,这个很有用;
_primary_first 如果主分片可用,只在主分片上执行搜索,否则才在其他分片上执行
_local 在可能情况下,只在发送请求的节点上的可用分片上执行搜索
_only_node:node_id 只在提供标识符的节点上执行搜索
_prefer_node:node_id Elasticsearch将尝试在提供标识符的节点上执行搜索;如果该节点不可用,则视同其他的可用节点
_shards:1,2 Elasticsearch将在提供标识符法人分片上执行操作;_shards参数可以和其他首选项合并,但
_shards标识符必须在前面,比如_shards:1,2;_local
curl -XGET 'localhost:9200/library/book/_search?pretty=true&search_type=query_and_fetch' -d '
{
"query": {
"term": {
"title": "crime"
}
}
}
'
curl -XGET 'localhost:9200/library/book/_search?preference=_lcoal' -d '
{
"query": {
"term": {
"title": "crime"
}
}
}
'
boost加权
在查询中包含加权值,它将赋给所有跟它匹配的文档。
{
"query": {
"match_all": {
"boost": 2.0
}
}
}