Elasticsearch 增删改查(CRUD)参考例子

 

##索引数据,插入3条。

PUT /megacorp/employee/1

{

    "first_name" : "John",

    "last_name" :  "Smith",

    "age" :        25,

    "about" :      "I love to go rock climbing",

    "interests": [ "sports", "music" ]

}

PUT /megacorp/employee/2

{

    "first_name" :  "Jane",

    "last_name" :   "Smith",

    "age" :         32,

    "about" :       "I like to collect rock albums",

    "interests":  [ "music" ]

}

##version 处理冲突,当并发更新数据时防止更新丢失。

PUT /megacorp/employee/3?version=1 

{

    "first_name" :  "Douglas",

    "last_name" :   "Fir",

    "age" :         35,

    "about":        "I like to build cabinets",

    "interests":  [ "forestry" ]

}

 

 

##查询数据

GET /megacorp/employee/1

##查询所有数据,默认返回前十条

GET /megacorp/employee/_search

{

  "query": {

    "match_all": {}

  }

}

 

 

##更新

1、重新索引,直接运行上边的插入数据即可

 

 

 

 

 

 

 

2、局部更新,存在的字段覆盖,不存在的字段添加

POST /megacorp/employee/1/_update?retry_on_conflict=5

{

   "doc" : {

      "interests" : [ "movie","music" ],

      "age": 26

   }

}

 

 

##删除

DELETE    /megacorp/employee/1

 

 

 

 

##批量查询(读)

1、索引类型随意定义

POST /_mget

{

   "docs" : [

      {

         "_index" : "megacorp",

         "_type" :  "employee",

         "_id" :    1

      },

      {

         "_index" : "megacorp",

         "_type" :  "employee",

         "_id" :    2,

         "_source": ["interests","age"]

      }

   ]

}

 

2、索引类型都一致,省事写法

POST /megacorp/employee/_mget

{

   "ids" : [ "2", "1" ]

}

 

 

 

 

3、大部分索引类型一样,偷懒写法。很自由

POST /megacorp/employee/_mget

{

   "docs" : [

      { "_id" : 2 },

       {

         "_index" : "website",

         "_type" :  "blog",

         "_id" :    123

      }

   ]

}

 

 

 

##批量写

 

POST /_bulk

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

{"title":"My first blog post","author":"Lidonghai"}

{"index":{"_index":"website","_type":"blog","_id":"124"}}

{"title":"My second blog post","author":"Liefu"}

{"update":{"_index":"website","_type":"blog","_id":"123","_retry_on_conflict":3}}

{"doc":{"title":"My updated blog post","author":"Liening"}}

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

 

 

 

 

 

 

##更灵活的多索引多类型定义查询。这个好玩

/_search

 

在所有索引的所有类型中搜索

/gb/_search

 

在索引gb的所有类型中搜索

/gb,us/_search

 

在索引gbus的所有类型中搜索

/g*,u*/_search

 

在以gu开头的索引的所有类型中搜索

/gb/user/_search

 

在索引gb的类型user中搜索

/gb,us/user,tweet/_search

 

在索引gbus的类型为usertweet中搜索

/_all/user,tweet/_search

 

示例一:

GET /megacor*,websit*/_search

{

    "query": {

        "match": {

            "_all": "Liening rock"

        }

    }

}

 

##示例二,无比灵活。类型中可以没有相应的字段都可以夸索引进行查询

GET /megacor*,websit*/_search

{

  "from":0,

  "size":10,

  "query": {

    "bool": {

      "must":     { "match": { "last_name": "Smith" }},

      "must_not": { "match": { "first_name": "Jane"  }},

 

      "should": [

                  { "match": { "interests": "sports" }},

                  { "match": { "author": "Liefu"   }}

      ]

    }

  }

}

 

 

 

 

##其它有意思的查询

1、term过滤,过滤准确值

GET /megacorp/employee/_search

{

  "query": {

    "term": {

      "age": 32

    }

  }

}

 

##常用的boost属性

GET /megacorp/employee/_search

{

  "query": {

    "term": {

      "age": {

        "value": 32,

        "boost": 10.0

      }

    }

  }

}

 

 

2、terms过滤,过滤多个准确值

GET /megacorp/employee/_search

{

  "query": {

    "terms": {

      "about": ["rock","albums"]

    }

  }

}

 

3、range过滤

  

GET /megacorp/employee/_search

{

  "query": {

    "range": {

      "age": {

        "gte": 20,

        "lt": 30

      }

    }

  }

}

 

4、bool过滤。注意 term类型过滤,过滤的term是被分析器处理过的,别踩坑

GET /megacorp/employee/_search

{

  "query": {

    "bool": {

      "must": {

        "term": {

          "last_name": "smith"

        }

      },

      "must_not": {

        "range": {

          "age": {

            "gte": 50,

            "lt": 100

          }

        }

      },

      "should": [

        {

          "term": {

            "about": "albums"

          }

        },

        {

          "term": {

            "about": "rock"

          }

        }

      ]

    }

  }

}

 

 

5、match匹配查询,先用该字段上的分词器进行分词获取term,之后再去字段上进行匹配term,有则符合无责排除,多则分高少则分低。

GET /megacorp/employee/_search

{

  "query": {

    "match": {

      "about": "rock climbing"

    }

  }

}

 

##提高下精度

GET /megacorp/employee/_search

{

  "query": {

    "match": {

      "about": {      

                "query":    "rock climbing",

                "operator": "and"

            }

    }

  }

}

 

##控制精度

GET /megacorp/employee/_search

{

  "query": {

    "match": {

      "about": {      

                "query":    "rock climbing love",

                 "minimum_should_match": "75%"

            }

    }

  }

}

 

 

6、多字段搜索,多个字段同时搜索term(经分析器分后),有则符合多则分高。此处不涉及具体评分规则

GET /megacorp/employee/_search

{

  "query": {

     "multi_match": {

        "query":    "run climbing",

        "fields":   [ "interests", "about" ]

    }

  }

}

 

7bool查询

bool 查询与 bool过滤相似,用于合并多个查询子句。不同的是,bool过滤可以直接给出是否匹配成功, 而bool查询要计算每一个查询子句的_score(相关性分值)。

must:: 查询指定文档一定要被包含。

must_not:: 查询指定文档一定不要被包含。

should:: 查询指定文档,有则可以为文档相关性加分。

 

GET /megacorp/employee/_search

{

  "from": 0,

  "size": 10,

  "query": {

    "bool": {

      "must": {

        "match": {

          "last_name": "Smith"

        }

      },

      "must_not": {

        "match": {

          "first_name": "Jane"

        }

      },

      "should": [

        {

          "match": {

            "interests": "sports"

          }

        },

        {

          "match": {

            "interests": "Liefu"

          }

        }

      ]

    }

  }

}

 

 

##控制精度,同样适用于bool过滤哟

GET /megacorp/employee/_search

{

  "query": {

    "bool": {

      "should": [

        { "match": { "about": "rock" }},

        { "match": { "about": "climbing"   }},

        { "match": { "about": "albums"   }}

      ],

      "minimum_should_match": 2

    }

  }

}

 

8、先查询在过滤,多个查询条件也很简单

GET /megacorp/employee/_search

{

  "query": {

    "filtered": {

      "query": {

        "match_all": {}

      },

      "filter": {

        "term": {

          "last_name": "smith"

        }

      }

    }

  }

}

##多个查询条件

GET /megacorp/employee/_search

{

  "query": {

    "filtered": {

      "query": {

        "bool": {

          "must": [

            {

              "match": {

                "last_name": "Smith"

              }

            },

            {

             "match": {

                "about": "rock"

              }

            }

          ]

        }

      },

      "filter": {

        "term": {

          "first_name": "john"

        }

      }

    }

  }

}

 

 

9、先过滤再查询,指定多个过滤条件也很简单。

GET /megacorp/employee/_search

{

  "query": {

    "filtered": {

      "filter": {

        "bool": {

          "must": {

            "term": {

              "last_name": "smith"

            }

          },

          "must_not": {

            "query": {

              "match": {

                "first_name": "John"

              }

            }

          }

        }

      }

    }

  }

}

 

##多个过滤条件

GET /megacorp/employee/_search

{

  "query": {

    "filtered": {

      "filter": {

        "bool": {

          "must": [

            {

              "term": {

                "last_name": "smith"

              }

            },

            {

              "term": {

                "about": "rock"

              }

            }

          ],

          "must_not": {

            "query": {

              "match": {

                "first_name": "John"

              }

            }

          }

        }

      }

    }

  }

}

 

10、验证查询,不得不承认开始的时候是比较难写上述的语句。所以验证查询也是很有用的,帮我们查看到底我们的语句是否书写正确否。如果有问题就查看下错误原因

 

GET /megacorp/employee/_validate/query?explain

{

  "query": {

    "match_all": {}

  }

}

 

##没问题

 

##有问题看下啥原因引起的错误

GET /megacorp/employee/_validate/query?explain

{

  "query": {

    "match_all": {}

  }

}

 

 

11、exists过滤器,某个字段不为空,类似于SQLis not null。不想要查询结果传参数?search_type=count。可以用来判断某个字段是否存在,如果返回条数为0则说明这个字段不存在。

GET  /megacorp/employee/_search

{

    "query" : {

        "filtered" : {

            "filter" : {

                "exists" : { "field" : "first_name" }

            }

        }

    }

}

 

12、missing过滤器,某个字段为空,类似于SQLis null.不想要查询结果传参数?search_type=count

GET /megacorp/employee/_search

{

  "query": {

    "filtered": {

      "filter": {

        "missing": {

          "field": "first_name"

        }

      }

    }

  }

}

 

 

13、控制得分,对比下方的两种查询文档的得分。是不是改变了得分。

##默认得分

GET /megacorp/employee/_search

{

  "query": {

    "bool": {

      "should": [

        {

          "match": {

            "about": "rock"

          }

        },

        {

          "match": {

            "about": "climbing"

          }

        },

        {

          "match": {

            "about": "albums"

          }

        }

      ],

      "minimum_should_match": 2

    }

  }

}

 

##改变了子句权重

GET /megacorp/employee/_search

{

  "query": {

    "bool": {

      "should": [

        {

          "match": {

            "about": "rock"

          }

        },

        {

          "match": {

            "about": {

              "query": "climbing",

              "boost": 2

            }

          }

        },

        {

          "match": {

            "about": "albums"

          }

        }

      ],

      "minimum_should_match": 2

    }

  }

}

 

 

 

########################################更高级的查询#####################

1、最佳字段查询。一个字段包含用户输入的所有词比多个字段分别包含用户的部分词更相关。

PUT /my_index/my_type/5

{

    "title": "Quick brown rabbits",

    "body":  "Brown rabbits are commonly seen."

}

 

PUT /my_index/my_type/6

{

    "title": "Keeping pets healthy",

    "body":  "My quick brown fox eats rabbits on a regular basis."

}

 

 

GET  /my_index/my_type/_search

{

    "query": {

        "bool": {

            "should": [

                { "match": { "title": "Brown fox" }},

                { "match": { "body":  "Brown fox" }}

            ]

        }

    }

}

 

 

 

GET  /my_index/my_type/_search

{

    "query": {

        "dis_max": {

            "queries": [

                { "match": { "title": "Brown fox" }},

                { "match": { "body":  "Brown fox" }}

            ]

        }

    }

}

 

##最佳字段查询调优,这个是弥补dis_max使用最高得分排序的不足弥补(可能都不能同时包含所有term,这时就需要将其他字段的分数算上点,这样结果就更相关点)

GET  /my_index/my_type/_search

{

    "query": {

        "dis_max": {

            "queries": [

                { "match": { "title": "Quick pets" }},

                { "match": { "body":  "Quick pets" }}

            ],

            "tie_breaker": 0.3

        }

    }

}

 

GET /my_index/my_type/_search

{

  "query": {

  "dis_max": {

    "queries":  [

      {

        "match": {

          "title": {

            "query": "Quick brown fox",

            "minimum_should_match": "30%"

          }

        }

      },

      {

        "match": {

          "body": {

            "query": "Quick brown fox",

            "minimum_should_match": "30%"

          }

        }

      }

    ],

    "tie_breaker": 0.3

  }

}

}

 

2、多重查询(多个字段中执行相同查询)

##最佳字段查询简写

GET /my_index/my_type/_search

{

  "query": {

 "multi_match": {

        "query":                "Quick brown fox",

        "type":                 "best_fields",

        "fields":               [ "title", "body" ],

        "tie_breaker":          0.3,

        "minimum_should_match": "30%"

    }

  }

}

 

##最佳字段查询,字段通配符

GET /my_index/my_type/_search

{

  "query": {

    "multi_match": {

      "query": "Quick brown fox",

      "fields": "*title"

    }

  }

}

 

##最佳字段查询,字段加权

GET /my_index/my_type/_search

{

  "query": {

    "multi_match": {

      "query": "Quick brown fox",

      "fields": [

        "*title^2",

        "*body"

      ]

    }

  }

}

 

 

3、最多字段查询,在多数字段中匹配到比在少数字段中匹配到更相关,就是bool匹配的简写方式(should子句匹配数量多越相关)。利用这个特性将同一字段以不同分析器分析后索引多次可以提高搜索相关性(比如不提取词干的分析器、提取词干的分析器、近义词分析器等)。

PUT /my_index

{

    "settings": { "number_of_shards": 1 },

    "mappings": {

        "my_type": {

            "properties": {

                "title": {

                    "type":     "string",

                    "analyzer": "english",

                    "fields": {

                        "std":   {

                            "type":     "string",

                            "analyzer": "standard"

                        }

                    }

                }

            }

        }

    }

}

 

PUT /my_index/my_type/1

{ "title": "My rabbit jumps" }

 

PUT /my_index/my_type/2

{ "title": "Jumping jack rabbits" }

 

 

 

 

GET /my_index/_search

{

   "query": {

        "multi_match": {

            "query":  "jumping rabbits",

            "type":   "most_fields",

            "fields": [ "title", "title.std" ]

        }

    }

}

##加点权重

GET /my_index/_search

{

   "query": {

        "multi_match": {

            "query":       "jumping rabbits",

            "type":        "most_fields",

            "fields":      [ "title^10", "title.std" ]

        }

    }

}

 

 

4、跨字段查询,这个就厉害了

首先解释下跨字段查询、最多字段查询的区别。因为两者挺像的。为什么有些情况最多字段查询不能替代跨字段查询。感性的理解跨字段查询,就是把多个字段看做一个字段然后match用户的输入字符串。

 

定义:最多字段查询被设计用来找到匹配任意单词的多数字段,跨字段查询是找到跨越所有字段的最匹配的单词。(一时蒙圈)

most_fields是以字段为中心(Field-centric),而不是以词条为中心(Term-centric)most_fields会查询最多匹配的字段(Most matching fields),而我们可能真正感兴趣的是最匹配的词条(Most matching terms)。因此最多字段不能替换跨字段查询。

 

例子:找个例子体会下。

PUT /my_index1/my_type/2

{

    "street":   "5 Poland Street",

    "city":     "London",

    "country":  "United Kingdom",

    "postcode": "W1V 3DG"

}

 

 

PUT /my_index1/my_type/3

{

    "street":   "5 Poland",

    "city":     "Poland",

    "country":  "United Kingdom",

    "postcode": "W1V 3DG"

}

 

 

##最多字段查询

GET /my_index1/my_type/_search

{

  "query": {

    "multi_match": {

      "query":   "Poland Street",

      "type":    "most_fields",

      "fields":  [ "street", "city", "country", "postcode" ]

    }

  }

}

 

解释:用户输入Poland Street,得出的查询结果是在2个字段中匹配相同单词(Poland),比在1个字段中匹配两个单词(Poland Street)的分数要高。显然这不是用户想要的结果。这就体现了最多字段匹配(Most matching fields)的不足之处,就像上边说的(最多字段查询被设计用来找到匹配任意单词的多数字段),不是找到最佳的匹配单词的。因此显然不能替换跨字段查询。

##看一下排序的效果是不是,跨字段查询比最多字段查询更合适这种场景。下方给出了加权的测试,排序顺序有变化了。

GET /my_index1/my_type/_search

{

  "query": {

    "multi_match": {

      "query":   "Poland Street",

      "type":    "cross_fields",

      "fields":  [ "street", "city", "country", "postcode" ]

    }

  }

}

 

GET /my_index1/my_type/_search

{

  "query": {

    "multi_match": {

      "query":   "Poland Street",

      "type":    "cross_fields",

      "fields":  [ "street", "city^5", "country", "postcode" ]

    }

  }

}

 

 

##查看两种查询的查询细节,如下。其中"operator": "and"项可以去掉,用and的目地就是加大两种查询的差异。更能清晰的体会到

GET /my_index1/my_type/_validate/query?explain

{

  "query": {

    "multi_match": {

      "query":   "Poland Street",

      "type":    "most_fields",

      "operator":    "and",

      "fields":  [ "street", "city", "country", "postcode" ]

    }

  }

}

 

GET /my_index1/my_type/_validate/query?explain

{

  "query": {

    "multi_match": {

      "query":   "Poland Street",

      "type":    "cross_fields",

      "operator":    "and",

      "fields":  [ "street", "city", "country", "postcode" ]

    }

  }

}

 

##跨字段查询的其他注意事项。可以自行测试,并用_validate验看结果。

**为了让cross_fields查询类型能以最佳的方式工作,所有的字段都需要使用相同的解析器。使用了相同的解析器的字段才会被组合在一起形成混合字段。如果你包含了使用不同分析器的字段,它们会以和best_fields相同的方式被添加到查询中。

 

**同样将not_analyzed字段混合到analyzed字段中是没有益处的。

 

**另外跨字段查询的另外一种替代方案就是“全字段查询”,可已将跨越的多个字段内容放到一个字段中存放。这样就可以达到跨字段查询的效果。用那种方案更合适自己就选来用。

##全字段查询的使用方式如下。看下效果和跨字段一样。

PUT /my_index1

{

    "mappings": {

        "my_type": {

            "properties": {

                "street": {

                    "type":     "string",

                    "copy_to":  "full_name"

                },

                "city": {

                    "type":     "string",

                    "copy_to":  "full_name"

                },

                 "country": {

                    "type":     "string",

                    "copy_to":  "full_name"

                },

                 "postcode": {

                    "type":     "string",

                    "copy_to":  "full_name"

                },

                "full_column": {

                    "type":     "string"

                }

            }

        }

    }

}

 

##达到了跨字段查询的效果,所以自己选择一种方案吧(跨字段查询、全字段查询)

GET /my_index1/my_type/_search

{

  "query": {

    "match": {

      "full_column": "Poland Street"

    }

  }

}

 

 

**这条补充下跨字段查询不能被最多字段查询替换的原因(最多字段查询存在的问题)

参看:https://es.xiaoleilu.com/110_Multi_Field_Search/40_Field_centric.html

它被设计用来找到匹配任意单词的多数字段,而不是找到跨越所有字段的最匹配的单词。

它不能使用operator或者minimum_should_match参数来减少低相关度结果带来的长尾效应。

每个字段的词条频度是不同的,会互相干扰最终得到较差的排序结果。(涉及到打分)

 

 

 

 

 

 

 

########################其它常用的查询################

 

 

 

##match_phrase查询,短语匹配,默认分出的term必须同时包含在字段中,并且各个term之间顺序必须和查询的顺序相同

GET /megacorp/employee/_search

{

  "query": {

    "match_phrase": {

      "about": "rock albums"

    }

  }

}

 

GET /megacorp/employee/_search

{

  "query": {

    "match_phrase": {

      "about": {

        "query":"like albums",

        "slop":3  ##各个term必须出现在文档中,但是顺序可以不一定按照查询字符串的顺序。这个slop意思是需要移动多少次term才能匹配到相应的文档(或者说移动多少次term才能和文档中的term顺序和位置一致)

        

      }

    }

  }

}

 

 

 

 

 

 

##match_phrase_prefix查询,和match_phrase查询很相近,只是最后一个词项可以只写一单词的一部分就可以匹配出(可能考虑到用户拼不完最后一个单词,这个可能比match_phrase更好用点)

GET /megacorp/employee/_search

{

  "query": {

    "match_phrase_prefix": {

      "about": {

        "query":"build cabi"

        

      }

    }

  }

}

 

 

##query_string查询。Elasticsearch支持Lucene的查询语法。

就是简化的bool查询、DisMax查询,效果依赖分词器,中文用中文分词器会好点

GET /megacorp/employee/_validate/query?explain

{

  "query": {

    "query_string": {

       "query": "-rock +climbing^3  like",  #+表示必须,-表示一定不,空表示有则加分

       "fields": ["about","last_name"],

       "minimum_should_match": "20%",  #控制匹配数量

"use_dis_max": true   ##使用DisMax查询,去掉参数使用bool查询

      

    }

  }

}

 

##ids查询

GET /megacorp/employee/_search

{

  "query": {

    "ids": {

      "values": ["1","2","3"]

    }

  }

}

 

GET /megacorp/_search

{

  "query": {

    "ids": {

      "type":"employee",

      "values": ["1","2","3"]

    }

  }

}

 

 

##prefix查询,前缀查询。找到字段中含有以"coll"前缀开头的term的文档

GET /megacorp/_search

{

  "query": {

    "prefix": {

      "about":"coll"

    }

  }

}

 

##fuzzy查询,模糊查询,此类查询特别费cpu,不建议使用。但在一些特殊场景下有用,

比如搜索 like  时用户写成了lkie。也是可以搜索出来的

GET /megacorp/employee/_search

{

  "query": {

    "fuzzy": {

      "about":{

      "value":"clbing"

      }

        

      }

  }

}

 

 

##wildcard查询,野查询,可以使用通配符

GET /megacorp/employee/_search

{

  "query": {

    "wildcard": {

      "about":{

      "value":"cl*?bing"

      }

        

      }

  }

}

 

##more_like_this查询,和最多字段查询基本一样。用处不大

GET /megacorp/employee/_search

{

  "query": {

    "more_like_this": {

      "fields":["about","last_name"],

      "like_text": "like collect smith",

      "min_term_freq": 1, ##term出现在文档中的频率不能低于这个值

      "min_doc_freq": 1 ##term出现的文档数量不能少于这个值。

        

      }

  }

}

 

 

##脚本过滤器

注:使用脚本过滤器需要配置elasticsearch.yml才可以

vim elasticsearch.yml ##添加配置

script.engine.groovy.inline.update: on

script.search: on

script.groovy.sandbox.enabled: true

 

 

GET /megacorp/employee/_search

{

  "filter": {

    "script": {

      "script": "now - doc['age'].value > 0",

      "params": {

        "now": 100

      }

    }

  }

}

 

 

 

 

##type过滤器,当查询结果有大量类型时比较有用

GET /megacorp/_search

{

  "filter": {

    "type": {

      "value":"employee"

    }

  }

}

 

##ids过滤器

GET /megacorp/_search

{

  "filter": {

    "ids": {

      "values":[1,2,3]

    }

  }

}

 

 

##not过滤器,not中指定的过滤器匹配到的记录不会被返回

GET /megacorp/employee/_search

{

  "filter": {

    "not": {

      "filter": {

        "ids": {

          "values": [

            1,

            2,

            3

          ]

        }

      }

    }

  }

}

 

 

##or过滤器,匹配or过滤器数组中任何一个过滤器都会返回

GET /megacorp/employee/_search

{

  "filter": {

    "or": [

      {

        "term": {

          "age": 36

        }

      },

            {

        "term": {

          "age": 27

        }

      }

    ]

  }

}

 

 

##and过滤器,匹配所有and数组中的过滤器才可以返回

GET /megacorp/employee/_search

{

  "filter": {

    "and": [

      {

        "term": {

          "age": 27

        }

      },

            {

        "term": {

          "last_name": "smith"

        }

      }

    ]

  }

}

 

 

##andor过滤器,and中的过滤器和or中的任何一个顾虑器同时匹配到才可以

GET /megacorp/employee/_search

{

  "filter": {

   

    "and": [

      {

        "term": {

          "age": 36

        }

      },

      {

        "or": [

          {

            "term": {

              "first_name": "smith"

            }

          },

          {

            "term": {

              "last_name": "fir"

            }

          }

        ]

      }

    ]

  }

  

}

 

 

##notandor过滤器一起使用,not中的过滤器匹配到的都不能返回

GET /megacorp/employee/_search

{

  "filter": {

    "not": {

    "filter": {

   

    "and": [

      {

        "term": {

          "age": 36

        }

      },

      {

        "or": [

          {

            "term": {

              "first_name": "smith"

            }

          },

          {

            "term": {

              "last_name": "fir"

            }

          }

        ]

      }

    ]

  }

    }

  }

}

 

 

 

##boosting查询,包括三个部分,positive(必须匹配并且分数不变)、negative(匹配到的文档要降分)、nagative_boost(降分的系数,当然也可以生分)。和之前调boost的方式差不多

GET /megacorp/_search

{

  "query": {

    "boosting": {

      "positive": {

        "term": {

          "about": "rock"

        }

      },

      "negative": {

        "term": {

          "age": 12

        }

      },

      "negative_boost": 0.2

    }

  }

}

 

 

##constant_score查询,所有返回文档得分一样

GET /megacorp/employee/_search

{

  "query": {

    "constant_score": {

      "query": {

        "term": {

          "about": "rock"

        }

      },

      "boost":2.0  ##这个参数没效果

    }

  }

}

 

 

##indices查询,多个索引进行查询时有用(别名对应多个索引、或者通配符通配多个索引)时可以区分索引执行不同的查询。indices数组中匹配到的索引执行query,未匹配到的索引执行no_match_query

GET megacorptests/_search  ##megacorptests是两个索引的别名

{

  "from":0,

  "size":100,

  "query": {

    "indices": {

      "indices": [

        "megacorp"    ##这个索引将会执行query查询,其他的索引执行no_match_query

      ],

      "query": {

        "term": {

          "about": "build"

        }

      },

      "no_match_query": {

        "term": {

          "content2": "李克强"

        }

      }

    }

  }

}

 

 

 

###############补充部分#############

 

##script_fields查询,对于查询后的结果数据,该查询可以定义只要某些字段,并且还可以对这些字段进行调整修改

GET /megacorp/employee/_search

{

  "script_fields": {

     "realage": {

      "script": "_source.age"

    },

    "fakeage": {

      "script": "doc['age'].value -8"

    },

    "about": {

      "script": "_source.about"

    }

  },

  "query": {

    "term": {

      "about": "rock"

    }

  }

}

 

##script_fields查询传参数

GET /megacorp/employee/_search

{

  "script_fields": {

     "realage": {

      "script": "_source.age"

    },

    "fakeage": {

      "script": "doc['age'].value -paramage"

      , "params": {"paramage":8}

    },

    "about": {

      "script": "_source.about"

    }

  },

  "query": {

    "term": {

      "about": "rock"

    }

  }

}

 

##定义返回的字段

 

GET /megacorp/employee/_search

{

  "fields": ["about","first_name","last_name"],

  "query": {

    "term": {

      "about": "rock"

    }

  }

}

 

 

##min_score限制结果分数,低于0.1分不要了

GET /megacorp/employee/_search

{

  "min_score":0.1,

  "query": {

    "term": {

      "about": "rock"

    }

  }

}

 

 

##version返回版本号

GET /megacorp/employee/_search

{

  "version": true,

  "query": {

    "term": {

      "about": "rock"

    }

  }

}

 

###选择合适的搜索类型,默认就可以了,其次是count类型在统计查询的时候用的多

参看:https://es.xiaoleilu.com/060_Distributed_Search/15_Search_options.html

搜索类型:

query_and_fetch(最快最简单的查询,返回每个分片的前size个文档,即size*主分片数量,不常用。因为得到的文档并不是最相关的)、

query_then_fetch(默认,仅仅返回size个最相关的文档)

dfs_query_and_fetch(与query_and_fetch相似,只是IDF分数是全局计算的而不是单个分片计算的,不建议生产环境使用。在后期Elasticsearch版本中已经去除。在数据量很多时本地的IDF值和全局的IDF很接近,完全没必要使用,白白浪费cpu资源)、

dfs_query_then_fetch(query_then_fetch相似,道理和上条一样,也是计算全局的IDF得分值)

count(只返回文档统计数量,不返回文档)、

scan(用于获取大量数据,不对文档打分排序)

 

 

##高亮,很常用的方式

GET /megacorp/employee/_search

{

  "version": true,

  "query": {

    "term": {

      "about": "rock"

    }

  },

  "highlight": {

    "fields": {

      "about": {}

    }

  }

}

 

##高亮更换默认标签

GET /megacorp/employee/_search

{

  "version": true,

  "query": {

    "term": {

      "about": "rock"

    }

  },

  "highlight": {

    "fields": {

      "about": {"pre_tags":["
"],"post_tags":["
"]}  

    }

  }

}

 

##高亮其它参数

GET /megacorp/employee/_search

{

  "version": true,

  "query": {

    "terms": {

      "about": [

        "climbing"

      ]

    }

  },

  "highlight": {

    "require_field_match":false,  ##其他字段的term也会高亮显示,但需要在fields中配置

    "fields": {

      "about": {

        "pre_tags": [

          "
"

        ],

        "post_tags": [

          "
"

        ],

        "number_of_fragments": 0  ##将返回全部字段值

      },

      "last_name": {

        "pre_tags": [

          "
"

        ],

        "post_tags": [

          "
"

        ],

        "number_of_fragments": 0

      }

    }

  }

}

 

 

 

##自动补全,直接看例子。一共三种自动补全

自动补全:prefixedgeNGram、统计。都以分词器为基础

1prefix自动补全,term中要有相应的词条才可以。下方term中必须有张三封张*’什么的

GET /my_index9/my_type9/_search

{

  "query": {

    "prefix": {

      "name": {

        "value": "张三封张"

      }

    }

  }

}

 

2edgeNGram补全和统计的使用(当然统计可以不使用分词),比prefix省资源。创建索引

PUT /my_index4

{

  "settings": {

    "index": {

      "analysis": {

        "analyzer": {

          "autocomplete": {  ##构建自定义分析器,用空格分词

            "tokenizer": "whitespace",

            "filter": [

              "lowercase",  ##,之后过滤器处理“小写”

              "engram"   ##边缘分词所有item

            ]

          }

        },

        "filter": {

          "engram": {

            "type": "edgeNGram",

            "min_gram": 3,  ##小于3个符号的边缘词不予存储,中文是3个字,英文3个字母

            "max_gram": 10 ##大于10个符号的边缘词不予存储

          }

        }

      }

    }

  },

  "mappings": {

    "my_type4": {

      "properties": {

        "name": {

          "type": "string",

          "analyzer": "autocomplete", ##name字段使用上边自定义的分析器

          "index": "analyzed",

          "search_analyzer": "autocomplete" ##查询使用自定义分词器,或者其他都可以

        },

        "country": {

          "type": "string"

        }

      }

    }

  }

}

 

加入条数据

PUT /my_index9/my_type9/6

{

    "name" : "张三封张三封",

    

    "country" :      "China"

}

 

找到所有含张三封’term的文档,然后统计所有term,这就可以看出edgeNGram的作用

GET /my_index4/my_type4/_search

{

  "size": 0,

  "query": {

    "term": {

      "name": "张三封"

    }

  },

  "aggs": {

    "all_interests": {

      "terms": { "field": "name" }

    }

  }

}

 

 

你可能感兴趣的:(Elasticsearch)