• 两种查询方式
  • Es 两种查询方式

    两种查询方式

    不推荐第一种方式。 url

    GET a1/_search/?q=age:18
    {
      "took" : 18,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "a1",
            "_type" : "doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "name" : "andy jack",
              "age" : 18
            }
          }
        ]
      }
    }
    

    结构化查询

    GET a1/_search
    {
      "query": {
        "match": {
          # age是字段名18是值
          "age": "18"
        }
      }
    }
    
    #添加数据
    PUT a1/doc/3
    {
      "name":"Mary",
      "desc":["可爱","漂亮"]
    }
    
    PUT a1/doc/4
    {
      "name":"Amy",
      "desc":["可爱","乖巧","阳光"]
    }
    
    #查询
    GET a1/doc/_search
    {
      "query": {
        "match": {
            "desc":"可 漂"
        }
      }
    }
    #这种情况下Es会对"可 漂" 做分词
    {
      "took" : 5,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 2,
        "max_score" : 0.5753642,
        "hits" : [
          {
            "_index" : "a1",
            "_type" : "doc",
            "_id" : "3",
            "_score" : 0.5753642,
            "_source" : {
              "name" : "Mary",
              "desc" : [
                "可爱",
                "漂亮"
              ]
            }
          },
          {
            "_index" : "a1",
            "_type" : "doc",
            "_id" : "4",
            "_score" : 0.2876821,
            "_source" : {
              "name" : "Amy",
              "desc" : [
                "可爱",
                "乖巧",
                "阳光"
              ]
            }
          }
        ]
      }
    }
    
    # 查某个具体的词
    GET a1/doc/_search
    {
      "query": {
        "match_phrase": {
            "desc":"可爱"
        }
      }
    }
    
    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 2,
        "max_score" : 0.5753642,
        "hits" : [
          {
            "_index" : "a1",
            "_type" : "doc",
            "_id" : "4",
            "_score" : 0.5753642,
            "_source" : {
              "name" : "Amy",
              "desc" : [
                "可爱",
                "乖巧",
                "阳光"
              ]
            }
          },
          {
            "_index" : "a1",
            "_type" : "doc",
            "_id" : "3",
            "_score" : 0.5753642,
            "_source" : {
              "name" : "Mary",
              "desc" : [
                "可爱",
                "漂亮"
              ]
            }
          }
        ]
      }
    }
    
    # 间隔词
    PUT a1/doc/1
    {
      "title":"中国是世界上最的国家"
    }
    #直接查中国世界是查不到的,因为它们中间隔了一个"是"字,指定slop就可以查到了
    GET a1/doc/_search
    {
      "query": {
        "match_phrase": {
          "title": {
            "query": "中国世界",
            "slop":1
          }
        }
      }
    }
    

    上一篇:Elasticsearch 基础

    下一篇:Es 之must,should filter