elasticsearch查詢

查詢API

GET /twitter/_search?q=user:kimchy
GET /twitter/tweet,user/_search?q=user:kimchy
GET /kimchy,elasticsearch/tweet/_search?q=tag:wow
GET /_all/tweet/_search?q=tag:wow
GET /_search?q=tag:wow
GET twitter/tweet/_search?q=user:kimchy

qhtml

The query string (maps to the query_string query, see Query String Query for more details).api

請求主體查詢

GET /twitter/tweet/_search
{
    "explain": true,
    "version": true,
    "query" : {
        "term" : { "user" : "kimchy" }
    },
    "from" : 0,
    "size" : 10,
    "sort" : [
        { "post_date" : {"order" : "asc"}},
        "user",
        { "name" : "desc" },
        { "age" : "desc" },
        "_score"
    ],
    "_source": [ "obj1.*", "obj2.*" ],
    "script_fields" : {
        "test1" : {
            "script" : "params['_source']['message']"
        }
    },
    "post_filter": { 
        "term": { "color": "red" }
    },
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "_all" : {}
        }
    }
}

Inner hits

Query DSL

全文搜索(Full text queries)

match

標準全文查詢elasticsearch

GET /_search
{
    "query": {
        "match" : {
            "message" : "this is a test"
        }
    }
}
match_phrase

短語查詢,支持分詞ide

GET /_search
    {
        "query": {
            "match_phrase" : {
                "message" : {
                    "query" : "this is a test",
                    "analyzer" : "my_analyzer"
                }
            }
        }
    }
match_phrase_prefix
GET /_search
{
    "query": {
        "match_phrase_prefix" : {
            "message" : {
                "query" : "quick brown f",
                "max_expansions" : 10
            }
        }
    }
}
multi_match

支持多字段版本post

GET /_search
{
  "query": {
    "multi_match" : {
      "query" : "this is a test",
      "fields" : [ "subject^3", "message" ] 
    }
  }
}
common_terms

中止符(stopwords)ui

GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "this is bonsai cool",
                    "cutoff_frequency": 0.001
            }
        }
    }
}
query_string

查詢解析this

GET /_search
{
    "query": {
        "query_string" : {
            "default_field" : "content",
            "query" : "this AND that OR thus"
        }
    }
}
simple_query_string

使用 SimpleQueryParser去解析查詢語句code

GET /_search
{
  "query": {
    "simple_query_string" : {
        "query": "\"fried eggs\" +(eggplant | potato) -frittata",
        "analyzer": "snowball",
        "fields": ["body^5","_all"],
        "default_operator": "and"
    }
  }
}

Term級別查詢

更底層的查詢regexp

  • Term
  • Terms
  • range
  • exists
  • prefix
  • wildcard
  • regexp
  • fuzzy
  • type
  • idshtm

    GET /_search
    {
    "query": {
    "constant_score" : {
    "filter" : {
    "terms" : { "user" : ["kimchy", "elasticsearch"]}
    }
    }
    }
    }

組合查詢(Compound queries)

  • constant_score
  • bool
  • dis_max
  • function_score
  • boosting
  • indices

    GET /_search
    {
    "query": {
    "constant_score" : {
    "filter" : {
    "term" : { "user" : "kimchy"}
    },
    "boost" : 1.2
    }
    }
    }

聯合查詢(Joining queries)

  • Nested
  • Has Child
  • Has Parent
  • Parent Id

    GET /_search { "query": { "constant_score" : { "filter" : { "term" : { "user" : "kimchy"} }, "boost" : 1.2 } } }

相關文章
相關標籤/搜索