lqshow / notes

Cheat Sheet
10 stars 2 forks source link

Elasticsearch Query DSL #15

Open lqshow opened 6 years ago

lqshow commented 6 years ago

空搜索

返回集群中所有索引下的所有文档

curl -XGET 'localhost:9200/_search?pretty'
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query": {
        "match_all": {}
    }
}
'

简单查询

curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}
'

复杂查询

curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}
'

全文搜索(在全文属性【独立单词(分词) or关系】上搜索并返回相关性最强的结果)

curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}
'

短语搜索(紧邻搜索)

curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}
'

高亮搜索

curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}
'

检索记录(通过文档ID)

curl -XGET 'http://localhost:9200/xdp-fusion-index/Patients/bb-25948f88457ce8d62caa0ae516462229?pretty'

精确查找(关键字后加.keyword)

curl -X GET \
-d '{
    "query" : {
        "bool": {
            "filter":{
               "term": {
                    "bbPatientId.keyword":"bb-4cd56a7f-57f1-40c9-9983-96169f717b7d"
                }
            }
        }
    }
}' \
http://localhost:9200/xdp-fusion-index/Patients/_search

范围查找(range)

curl -X GET \
-d '{
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "mergedDemo.BIRTH_DATE.value":{
                            "format":"yyyy-MM-dd",
                            "gte":"1967-01-01",
                            "lt":"1972-01-01"
                        }
                    }
                }
            ]
        }
    }
}' \
http://localhost:9200/xdp-fusion-index/Patients/_search

一个关键字过滤内容

terms

curl -X GET \
-d '{
    "query":{
        "bool":{
            "must":[
                {
                    "terms":{
                        "bbPatientId": ["bb-25948f88457ce8d62caa0ae516462229"]
                    }
                }
            ]
        }
    }
}' \
http://localhost:9200/xdp-fusion-index/Patients/_search

term

curl -X GET \
-d '{
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "bbPatientId": "bb-25948f88457ce8d62caa0ae516462229"
                    }
                }
            ]
        }
    }
}' \
http://localhost:9200/xdp-fusion-index/Patients/_search

紧邻搜索(match_phrase)

curl -X GET \
-d '{
    "query":{
        "bool":{
            "must":[
                {
                    "match_phrase":{
                        "bbPatientId":"bb-4cd56a7f-57f1-40c9-9983-96169f717b7d"
                    }
                }
            ]
        }
    }
}' \
http://localhost:9200/xdp-fusion-index/Patients/_search

模糊匹配(wildcard)

curl -X GET \
-d '{
    "query":{
        "bool":{
            "must":[
                {
                    "wildcard": {
                            "mergedDemo.ID_NO.value": "*" + value + "*"
                        }
                }
            ]
        }
    }
}' \
http://localhost:9200/xdp-fusion-index/Patients/_search

验证查询

curl -XGET 'localhost:9200/gb/tweet/_validate/query?explain&pretty' -H 'Content-Type: application/json' -d'
{
   "query": {
      "match" : {
         "tweet" : "really powerful"
      }
   }
}
'

多关键字or查询

curl -XGET 'localhost:9200/accounts/person/_search'  -d '
{
    "query":{
        "match":{
            "desc":"软件 系统"
        }
    }
}
'

多关键字and查询

curl -XGET 'localhost:9200/accounts/person/_search'  -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "desc": "软件" } },
        { "match": { "desc": "系统" } }
      ]
    }
  }
}'