ElasticSearch入门(2) —— 基本操作

ElasticSearch入门(2) —— 基本操作

简单操作

指定属性查询

//name 和 法外狂徒 是绝对相等的
//原因是name设置的是keyword 整体搜索才能查到
GET test3/_doc/_search?q=name:法外狂徒


{
  "took" : 115,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.1507283,
    "hits" : [
      {
        "_index" : "test3",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.1507283,
        "_source" : {
          "name" : "法外狂徒",
          "age" : 13,
          "birth" : "1997-01-05"
        }
      }
    ]
  }
}
//name 是 text类型的 就可以拆分查询
GET test3/_doc/_search?q=name:法外

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "test3",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "法外狂徒",
          "age" : 13,
          "birth" : "1997-01-05"
        }
      }
    ]
  }
}

复杂操作

_score 匹配度

模糊查看

GET test3/_doc/_search
{
  "query" : {
    "match" :{
      "name" : "狂"
    }
  }
}


{
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [ // hits 索引和文档的信息 
      {
        "_index" : "test3",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "法外狂徒",
          "age" : 13,
          "birth" : "1997-01-05"
        }
      }
    ]
  }
}

查看某些属性

GET bank/account/_search
{
  "query": {
    "match": {
      "firstname": "Amber"
    }
  },
    //结果的过滤,只要需要的信息
  "_source": ["firstname" , "lastname" , "age"]
}

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 6.5032897,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "1",
        "_score" : 6.5032897,
        "_source" : {
          "firstname" : "Amber",
          "age" : 32,
          "lastname" : "Duke"
        }
      }
    ]
  }
}

排序查询

GET bank/account/_search
{
  "query": {
    "match": {
      "firstname": "Amber"
    }
  },
  "_source": ["firstname" , "lastname" , "age"], 
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}


{
  "took" : 105,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "1",
         //当排序的值不是score时,这个值就为空了
        "_score" : null,
        "_source" : {
          "firstname" : "Amber",
          "age" : 32,
          "lastname" : "Duke"
        },
        "sort" : [
          32
        ]
      }
    ]
  }
}

分页查询

GET bank/account/_search
{
  "_source": ["firstname" , "lastname" , "age"], 
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0, //开始位置
  "size": 5 //页大小
}

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "291",
        "_score" : null,
        "_source" : {
          "firstname" : "Lynn",
          "age" : 40,
          "lastname" : "Pollard"
        },
        "sort" : [
          40
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "474",
        "_score" : null,
        "_source" : {
          "firstname" : "Obrien",
          "age" : 40,
          "lastname" : "Walton"
        },
        "sort" : [
          40
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "479",
        "_score" : null,
        "_source" : {
          "firstname" : "Cameron",
          "age" : 40,
          "lastname" : "Ross"
        },
        "sort" : [
          40
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "549",
        "_score" : null,
        "_source" : {
          "firstname" : "Jacqueline",
          "age" : 40,
          "lastname" : "Maxwell"
        },
        "sort" : [
          40
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "664",
        "_score" : null,
        "_source" : {
          "firstname" : "Hart",
          "age" : 40,
          "lastname" : "Mccormick"
        },
        "sort" : [
          40
        ]
      }
    ]
  }
}

bool值查询

GET bank/account/_search
{
  "query": {
    "bool": {
      "must": [ //must 相当于 and
        {
          "match": {
            "lastname": "Pollard"
          }
        },
        {
          "match": {
            "firstname": "Lynn"
          }
        }
      ]
    }
  }, 
  "_source": ["firstname" , "lastname" , "age"], 
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0, 
  "size": 5  
}

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "291",
        "_score" : null,
        "_source" : {
          "firstname" : "Lynn",
          "age" : 40,
          "lastname" : "Pollard"
        },
        "sort" : [
          40
        ]
      }
    ]
  }
}
GET bank/account/_search
{
  "query": {
    "bool": {
      "should": [ //should 相当于 or
        {
          "match": {
            "lastname": "Pollard"
          }
        },
        {
          "match": {
            "firstname": "Lynn"
          }
        }
      ]
    }
  }, 
  "_source": ["firstname" , "lastname" , "age"], 
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0, 
  "size": 5
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "291",
        "_score" : null,
        "_source" : {
          "firstname" : "Lynn",
          "age" : 40,
          "lastname" : "Pollard"
        },
        "sort" : [
          40
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "455",
        "_score" : null,
        "_source" : {
          "firstname" : "Lynn",
          "age" : 36,
          "lastname" : "Tran"
        },
        "sort" : [
          36
        ]
      }
    ]
  }
}

:must = and should = or must_not = not

过滤器 filter

GET bank/account/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "age": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ]
    }
  }, 
  "_source": ["firstname" , "lastname" , "age"]
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 44,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "157",
        "_score" : 0.0,
        "_source" : {
          "firstname" : "Claudia",
          "age" : 20,
          "lastname" : "Terry"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "215",
        "_score" : 0.0,
        "_source" : {
          "firstname" : "Copeland",
          "age" : 20,
          "lastname" : "Solomon"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "816",
        "_score" : 0.0,
        "_source" : {
          "firstname" : "Cornelia",
          "age" : 20,
          "lastname" : "Lane"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "905",
        "_score" : 0.0,
        "_source" : {
          "firstname" : "Schultz",
          "age" : 20,
          "lastname" : "Moreno"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "95",
        "_score" : 0.0,
        "_source" : {
          "firstname" : "Dominguez",
          "age" : 20,
          "lastname" : "Le"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "172",
        "_score" : 0.0,
        "_source" : {
          "firstname" : "Marie",
          "age" : 20,
          "lastname" : "Whitehead"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "228",
        "_score" : 0.0,
        "_source" : {
          "firstname" : "Rosella",
          "age" : 20,
          "lastname" : "Albert"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "273",
        "_score" : 0.0,
        "_source" : {
          "firstname" : "Murphy",
          "age" : 20,
          "lastname" : "Chandler"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "292",
        "_score" : 0.0,
        "_source" : {
          "firstname" : "Morrow",
          "age" : 20,
          "lastname" : "Greene"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "367",
        "_score" : 0.0,
        "_source" : {
          "firstname" : "Elaine",
          "age" : 20,
          "lastname" : "Workman"
        }
      }
    ]
  }
}

查询

// term 精确查询,使用倒排索引进行查询
// match 会使用分词器进行查询

text 和 keyword

text会被分词器解析,而keyword是一个整体,只有全相等时才能被查出来

高亮查询

GET bank/account/_search
{
  "query": {
    "match": {
      "firstname": "Marie"
    }
  },
  "highlight": {
    "fields": {
      "firstname" : {} 
    }
  }
}

{
  "took" : 183,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 6.5032897,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "172",
        "_score" : 6.5032897,
        "_source" : {
          "account_number" : 172,
          "balance" : 18356,
          "firstname" : "Marie",
          "lastname" : "Whitehead",
          "age" : 20,
          "gender" : "M",
          "address" : "704 Monaco Place",
          "employer" : "Sultrax",
          "email" : "[email protected]",
          "city" : "Dragoon",
          "state" : "IL"
        },
        "highlight" : {
          "firstname" : [
            "<em>Marie</em>"
          ]
        }
      }
    ]
  }
}

自定义标签

GET bank/account/_search
{
  "query": {
    "match": {
      "firstname": "Marie"
    }
  },
  "highlight": {
    "pre_tags": "<p>",
    "post_tags": "</p>", 
    "fields": {
      "firstname" : {} 
    }
  }
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 6.5032897,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "172",
        "_score" : 6.5032897,
        "_source" : {
          "account_number" : 172,
          "balance" : 18356,
          "firstname" : "Marie",
          "lastname" : "Whitehead",
          "age" : 20,
          "gender" : "M",
          "address" : "704 Monaco Place",
          "employer" : "Sultrax",
          "email" : "[email protected]",
          "city" : "Dragoon",
          "state" : "IL"
        },
        "highlight" : {
          "firstname" : [
              //这里
            "<p>Marie</p>"
          ]
        }
      }
    ]
  }
}

猜你喜欢

转载自blog.csdn.net/qq_40788718/article/details/107801117