ES官网:https://www.elastic.co/cn/products/elasticsearch
github地址:www.github.com/elastic/elasticsearch
elasticsearch_dsl语法: https://elasticsearch-dsl.readthedocs.io/en/latest/
match_all 查询简单的匹配所有文档
match 全文搜索还是精确查询
multi_match 查询可以在多个字段上执行相同的 match 查询
range 查询找出那些落在指定区间内的数字或者时间:
term 查询被用于精确值匹配,这些精确值可能是数字、时间、布尔或者那些 not_analyzed 的字符串
terms 查询和 term 查询一样,但它允许你指定多值进行匹配
exists 查询和 missing 查询被用于查找那些指定字段中有值 (exists) 或无值 (missing) 的文档
constant_score 它将一个不变的常量评分应用于所有匹配的文档,它被经常用于你只需要执行一个 filter 而没有其它查询(例如,评分查询)的情况下
验证查询:validate, 加上expain参数查询不合法原因
组合查询:
must 文档 必须 匹配这些条件才能被包含进来。
must_not 文档 必须不 匹配这些条件才能被包含进来。
should 如果满足这些语句中的任意语句,将增加 _score ,否则,无任何影响。它们主要用于修正每个文档的相关性得分。
filter 必须 匹配,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档
精确值查找:
curl -X DELETE "localhost:9200/my_store?pretty"
curl -X POST "localhost:9200/my_store/products/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index": { "_id": 1 }}
{ "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "QQPX-R-3956-#aD8" }
'
term精确检索price
curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"price" : 20
}
}
}
}
}
'
bool过滤器:must所有的语句都 必须(must) 匹配,与 AND 等价。must_not所有的语句都 不能(must not) 匹配,与 NOT 等价。should至少有一个语句要匹配,与 OR 等价。
curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query":{
"bool":{
"should":[
{
"term":{
"price":20
}
},
{
"term":{
"productID":"XHDK-A-1293-#fJ3"
}
}
],
"must_not":{
"term":{
"price":30
}
}
}
}
}
'
curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query":{
"bool":{
"should":[
{
"terms":{
"productID":["KDKE", "9947"]
}
},
{
"bool":{
"must":[
{
"terms":{
"productID":["JODL", "1937"]
}
},
{
"term":{
"price":30
}
}
]
}
}
]
}
}
}'
查找多个精确值:terms
curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"constant_score" : {
"filter" : {
"terms" : {
"price" : [20, 30]
}
}
}
}
}
'
等效的表达式:
curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"bool" : {
"must" : {
"terms" : {
"price" : [20, 30]
}
}
}
}
}
范围查询
curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"constant_score" : {
"filter" : {
"range" : {
"price" : {
"gte" : 20,
"lt" : 40
}
}
}
}
}
}
'
等价表达式:
curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d
{
"query": {
"bool": {
"must": {
"range" : {
"price" : {
"gte" : 20,
"lte" : 30
}
}
}
}
}
}
'
处理null值: exists
curl -X POST "localhost:9200/my_index/posts/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index": { "_id": "1" }}
{ "tags" : ["search"] }
{ "index": { "_id": "2" }}
{ "tags" : ["search", "open_source"] }
{ "index": { "_id": "3" }}
{ "other_field" : "some data" }
{ "index": { "_id": "4" }}
{ "tags" : null }
{ "index": { "_id": "5" }}
{ "tags" : ["search", null] }
'
curl -X GET "localhost:9200/my_index/posts/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"constant_score" : {
"filter" : {
"exists" : { "field" : "tags" }
}
}
}
}
'
全文搜索
基于词项的查询 和 基于全文的查询
匹配查询: match
curl -X DELETE "localhost:9200/my_index?pretty"
curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
{ "settings": { "number_of_shards": 1 }}
'
curl -X POST "localhost:9200/my_index/my_type/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index": { "_id": 1 }}
{ "title": "The quick brown fox" }
{ "index": { "_id": 2 }}
{ "title": "The quick brown fox jumps over the lazy dog" }
{ "index": { "_id": 3 }}
{ "title": "The quick brown fox jumps over the quick dog" }
{ "index": { "_id": 4 }}
{ "title": "Brown fox brown dog" }
'
单个词查询
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"title": "QUICK!"
}
}
}
'
检查字段类型 。
标题 title 字段是一个 string 类型( analyzed )已分析的全文字段,这意味着查询字符串本身也应该被分析。
分析查询字符串 。
将查询的字符串 QUICK! 传入标准分析器中,输出的结果是单个项 quick 。因为只有一个单词项,所以 match 查询执行的是单个底层 term 查询。
查找匹配文档 。
用 term 查询在倒排索引中查找 quick 然后获取一组包含该项的文档,本例的结果是文档:1、2 和 3 。
为每个文档评分 。
用 term 查询计算每个文档相关度评分 _score ,这是种将词频(term frequency,即词 quick 在相关文档的 title 字段中出现的频率)和反向文档频率(inverse document frequency,即词 quick 在所有文档的 title 字段中出现的频率),以及字段的长度(即字段越短相关度越高)相结合的计算方式
多词查询
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"title": "BROWN DOG!"
}
}
}
'
提高精度:
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"title": {
"query": "BROWN DOG!",
"operator": "and"
}
}
}
}
'
组合查询
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": { "match": { "title": "quick" }},
"must_not": { "match": { "title": "lazy" }},
"should": [
{ "match": { "title": "brown" }},
{ "match": { "title": "dog" }}
]
}
}
}
'
控制精度:
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{ "match": { "title": "brown" }},
{ "match": { "title": "fox" }},
{ "match": { "title": "dog" }}
],
"minimum_should_match": 2
}
}
}
'
#### 布尔查询:[多词 `match` 查询](https://www.elastic.co/guide/cn/elasticsearch/guide/current/match-multi-word.html "多词查询")只是简单地将生成的 `term` 查询包裹在一个 `bool` 查询中。如果使用默认的 `or` 操作符,每个 `term` 查询都被当作 `should` 语句;
等价或查询
{
"match":{
"title":"brown fox"
}
}
等价表达式:
{
"bool":{
"should":[
{
"term":{
"title":"brown"
}
},
{
"term":{
"title":"fox"
}
}
]
}
}
等价与查询
{
"match": {
"title": {
"query": "brown fox",
"operator": "and"
}
}
}
等价表达式:
{
"bool": {
"must": [
{ "term": { "title": "brown" }},
{ "term": { "title": "fox" }}
]
}
}
查询语句增加权重:boost
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query":{
"bool":{
"must":{
"match":{
"title": "quick"
}
},
"must_not":{
"match":{
"title": "lazy"
}
},
"should":[
{
"match":{
"title":{
"query": "brown",
"boost": 2
}
}
},
{
"match":{
"title":{
"query": "dog",
"boost": 3
}
}
}
]
}
}
}
'
多字段查询
多字符串查询
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool" : {
"should": [
{
"match" : {
"title": {
"query": "pets",
"operator": "and"
}
}
},
{
"match": {
"body": {
"query": "brown",
"boost": 2
}
}
}
]
//"minimum_should_match": 1
}
}
}
'
multi_match: 能在多个字段上反复执行相同查询提供了一种便捷方式
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query":
{
"multi_match": {
"query": "Quick brown fox",
"type": "best_fields",
"fields": [ "title", "body" ],
"tie_breaker": 0.3,
"minimum_should_match": "30%"
}
}
}
'
最佳字段查询:dismax 和queries组合,分离最大化查询(Disjunction Max Query)指的是: 将任何与任一查询匹配的文档作为结果返回,但只将最佳匹配的评分作为查询的评分结果返回
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"dis_max": {
"queries":
[
{ "match": { "body": "Brown fox" }},
{ "match": { "title": "Brown fox" }}
]
}
}
}
'
近似匹配
短语匹配:彼此邻近搜索词的查询方法时,就会想到 match_phrase 查询
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query":{
"match_phrase":{
"title":"quick brown fox"
}
}
}
'
混合起来:slop参数,match_phrase 查询词条相隔多远时仍然能将文档视为匹配,相隔多远的意思是为了让查询和文档匹配你需要移动词条多少次
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query":{
"match_phrase":{
"title":{
"query":"quick fox",
"slop":1
}
}
}
}
'
多值字段:position_increment_gap
curl -X PUT "localhost:9200/my_index/groups/1?pretty" -H 'Content-Type: application/json' -d'
{
"names": [ "John Abraham", "Lincoln Smith"]
}
'
curl -X GET "localhost:9200/my_index/groups/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match_phrase": {
"names": "Abraham Lincoln"
}
}
}
'
Elasticsearch对以上数组分析生成了与分析单个字符串 John Abraham Lincoln Smith 一样几乎完全相同的语汇单元, 解决方案
curl -X DELETE "localhost:9200/my_index/groups/?pretty"
curl -X PUT "localhost:9200/my_index/_mapping/groups?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"names": {
"type": "text",
"position_increment_gap": 100
}
}
}
'
邻近度提高相关度
curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": {
"match": {
"title": {
"query": "quick brown fox",
"minimum_should_match": "30%"
}
}
},
"should": {
"match_phrase": {
"title": {
"query": "quick brown fox",
"slop": 50
}
}
}
}
}
}
'
部分匹配
curl -X PUT "localhost:9200/your_index/address/1?pretty" -H 'Content-Type: application/json' -d'
{ "postcode": "W1V 3DG" }
'
curl -X PUT "localhost:9200/your_index/address/2?pretty" -H 'Content-Type: application/json' -d'
{ "postcode": "W2F 8HW" }
'
curl -X PUT "localhost:9200/your_index/address/3?pretty" -H 'Content-Type: application/json' -d'
{ "postcode": "W1F 7HW" }
'
curl -X PUT "localhost:9200/your_index/address/4?pretty" -H 'Content-Type: application/json' -d'
{ "postcode": "WC1N 1LZ" }
'
curl -X PUT "localhost:9200/your_index/address/5?pretty" -H 'Content-Type: application/json' -d'
{ "postcode": "SW5 0BE" }
'
前缀查询
curl -X GET "localhost:9200/my_index/address/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"prefix": {
"postcode": "W1"
}
}
}
'
通配符和正则表达式查询
查询时输入即搜索
索引时优化
查询时权重提升
boost, 提升查询语句的重要性
GET /_search {
"query":{
"bool":{
"should":[
{
"match":{
"title":{
"query":"quick brown fox",
"boost":2
}
}
},
{
"match":{
"content":"quick brown fox"
}
}
]
}
}
}
indices_boost, 提升查询索引库的权重
GET /docs_2014_*/_search
{
"indices_boost":{
"docs_2014_10":3,
"docs_2014_09":2
},
"query":{
"match":{
"text":"quick brown fox"
}
}
}
修改查询结构修改相关度
postive和negative
{
"query":{
"boosting":{
"positive":{
"match":{
"text":"apple"
}
},
"negative":{
"match":{
"text":"pie tart fruit crumble tree"
}
},
"negative_boost":0.5
}
}
}
constant_score忽略tf-idf
网友评论