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

q

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

请求主体查询

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

标准全文查询

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

短语查询,支持分词

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

支持多字段版本

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

停止符(stopwords)

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

查询解析

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

使用 SimpleQueryParser去解析查询语句

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

Term级别查询

更底层的查询

  • Term

  • Terms

  • range

  • exists

  • prefix

  • wildcard

  • regexp

  • fuzzy

  • type

  • ids

    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
    }
    }
    }

原文地址:https://www.cnblogs.com/lilongsy/p/6654490.html