美文网首页
Elasticsearch Query DSL

Elasticsearch Query DSL

作者: 歌哥居士 | 来源:发表于2019-03-30 12:33 被阅读0次
    PUT test_search_index
    {
      "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1
      }
    }
    
    POST test_search_index/doc/_bulk
    {"index":{"_id":"1"}}
    {"username":"alfred way","job":"java engineer","age":18,"birth":"1992-01-02","isMarried":false}
    {"index":{"_id":"2"}}
    {"username":"alfred","job":"java senior engineer and java specialist","age":28,"birth":"1980-05-07","isMarried":true}
    {"index":{"_id":"3"}}
    {"username":"lee","job":"java and ruby engineer","age":22,"birth":"1985-08-07","isMarried":false}
    {"index":{"_id":"4"}}
    {"username":"alfred junior way","job":"ruby engineer","age":23,"birth":"1989-08-07","isMarried":false}
    
    # match
    
    GET test_search_index/doc/_search
    {
      "query": {
        "match": {
          "username": "alfred way"
        }
      }
    }
    
    GET test_search_index/doc/_search
    {
      "profile": "true",
      "query": {
        "match": {
          "username": "alfred way"
        }
      }
    }
    
    GET test_search_index/doc/_search
    {
      "query": {
        "match": {
          "username": {
            "query": "alfred way",
            "operator": "and"
          }
        }
      }
    }
    
    
    GET test_search_index/_search
    {
      "query": {
        "match": {
          "username": {
            "query": "alfred way",
            "minimum_should_match": 2
          }
        }
      }
    }
    
    GET test_search_index/_search
    {
      "query": {
        "match": {
          "job": {
            "query": "java ruby engineer",
            "minimum_should_match": 3
          }
        }
      }
    }
    
    # match_phrase
    
    GET test_search_index/_search
    {
      "query": {
        "match_phrase": {
          "job": "java engineer"
        }
      }
    }
    
    GET test_search_index/_search
    {
      "query": {
        "match_phrase": {
          "job": {
            "query": "java engineer",
            "slop": 2
          }
        }
      }
    }
    
    # query string
    
    GET test_search_index/_search
    {
      "query": {
        "query_string": {
          "default_field": "username",
          "query": "alfred AND way"
        }
      }
    }
    
    GET test_search_index/_search
    {
      "query": {
        "query_string": {
          "fields": ["username","job"],
          "query": "alfred OR (java AND ruby)"
        }
      }
    }
    
    
    # simple query string
    
    GET test_search_index/_search
    {
      "query": {
        "simple_query_string": {
          "query": "alfred +way",
          "fields": ["username"]
        }
      }
    }
    
    
    # term query
    
    GET test_search_index/_search
    {
      "query": {
        "term": {
          "username": "alfred"
        }
      }
    }
    
    GET test_search_index/_search
    {
      "query": {
        "term": {
          "username": "alfred way"
        }
      }
    }
    
    GET test_search_index/_search
    {
      "query": {
        "terms": {
          "username": [
            "alfred",
            "way"
          ]
        }
      }
    }
    
    
    # range query
    
    GET test_search_index/_search
    {
      "query": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 30
          }
        }
      }
    }
    
    GET test_search_index/_search
    {
      "query": {
        "range": {
          "birth": {
            "gte": "1990-01-01"
          }
        }
      }
    }
    
    
    GET test_search_index/_search
    {
      "query": {
        "range": {
          "birth": {
            "gte": "now-30y"
          }
        }
      }
    }
    
    GET test_search_index/_search
    {
      "query": {
        "range": {
          "birth": {
            "gte": "2010||-20y"
          }
        }
      }
    }
    
    
    # constant score
    
    GET test_search_index/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "match": {
              "username": "alfred"
            }
          }
        }
      }
    }
    
    # bool query
    
    GET test_search_index/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "username": "alfred"
              }
            }
          ]
        }
      }
    }
    
    GET test_search_index/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "username": "alfred"
              }
            },
            {
              "match": {
                "job": "specialist"
              }
            }
          ]
        }
      }
    }
    
    GET test_search_index/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "job": "java"
              }
            }
          ],
          "must_not": [
            {
              "match": {
                "job": "ruby"
              }
            }
          ]
        }
      }
    }
    
    
    GET test_search_index/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "username": "junior"
              }
            },
            {
              "match": {
                "job": "ruby"
              }
            }
          ]
        }
      }
    }
    
    GET test_search_index/_search
    {
      "query": {
        "bool": {
          "should": [
            {"term": {"job": "java"}},
            {"term": {"job": "ruby"}},
            {"term": {"job": "specialist"}}
          ],
          "minimum_should_match": 2
        }
      }
    }
    
    # must与should一起时,must为主,should加分
    GET test_search_index/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "username": "alfred"
              }
            }
          ],
          "should": [
            {
              "term": {
                "job": "ruby"
              }
            }
          ]
        }
      }
    }
    
    
    # count api
    GET test_search_index/_count
    {
      "query": {
        "match": {
          "username": "alfred"
        }
      }
    }
    
    
    # source_filter
    # 方式一
    GET test_search_index/_search?_source=username
    # 方式二
    GET test_search_index/_search
    {
      "_source": false
    }
    
    GET test_search_index/_search
    {
      "_source": ["username", "age"]
    }
    
    GET test_search_index/_search
    {
      "_source": {
        "includes": "*i*",
        "excludes": "birth"
      }
    }
    
    

    相关文章

      网友评论

          本文标题:Elasticsearch Query DSL

          本文链接:https://www.haomeiwen.com/subject/gamfbqtx.html