Elasticsearch(2)

13926 단어 Elasticsearch

1. 색인의 몇 가지 조회


1.query sting search

GET /ecummerce/product/_search

1.took :

2.timed_out

3._shards : , , primary shard( replica shard )

4.hits.total 3 document

5.hits.max_score :score document search , , 。

6.hits.hits: document


{
  "took" : 438,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "jiajieshi yagao",
          "desc" : "jiajieshi fangzhu",
          "price" : 25,
          "product" : "jiajieshi producter",
          "tags" : [
            "fengzhu"
          ]
        }
      },
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhonghua yagao",
          "desc" : "caoben zhizu",
          "price" : 40,
          "product" : "zhonghua producter",
          "tags" : [
            "qingxin"
          ]
        }
      },
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "gaolujie yagao",
          "desc" : "gaoxiao meibai",
          "price" : 30,
          "product" : "gaolujie producter",
          "tags" : [
            "fengzhu",
            "meibai"
          ]
        }
      }
    ]
  }
}

query string search r search http query string

2. yagao , :GET /ecommerce/product/_search?q=name:yagao&sort=price:desc


{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "zhonghua yagao",
          "desc" : "caoben zhizu",
          "price" : 40,
          "product" : "zhonghua producter",
          "tags" : [
            "qingxin"
          ]
        },
        "sort" : [
          40
        ]
      },
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "gaolujie yagao",
          "desc" : "gaoxiao meibai",
          "price" : 30,
          "product" : "gaolujie producter",
          "tags" : [
            "fengzhu",
            "meibai"
          ]
        },
        "sort" : [
          30
        ]
      },
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "jiajieshi yagao",
          "desc" : "jiajieshi fangzhu",
          "price" : 25,
          "product" : "jiajieshi producter",
          "tags" : [
            "fengzhu"
          ]
        },
        "sort" : [
          25
        ]
      }
    ]
  }
}

query DSL

http equest body : json , , 。

GET /ecummerce/product/_search
{
  "query":{
    "match_all":{}
  }
}

 

GET /ecummerce/product/_search
{
  "query":{
    "match_all":{}
  }
}

G

yagao , ET /ecummerce/product/_search
{
  "query":{
    "match":{
      "name":"yagao"
    }
  },
  "sort":[
    {"price":"desc"}
    ]
}

3. , 3 , 1 , 2 , 2

from:

size:

GET /ecummerce/product/_search
{
  "query":{
    "match_all":{}
  },
  "from":1,
  "size":1
}


{
  "took" : 32,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhonghua yagao",
          "desc" : "caoben zhizu",
          "price" : 40,
          "product" : "zhonghua producter",
          "tags" : [
            "qingxin"
          ]
        }
      }
    ]
  }
}

4.

GET /ecummerce/product/_search
{
  "query":{
    "match_all":{}
  },
 "_source":["name","price"]
}


{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "price" : 25,
          "name" : "jiajieshi yagao"
        }
      },
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "price" : 40,
          "name" : "zhonghua yagao"
        }
      },
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "price" : 30,
          "name" : "gaolujie yagao"
        }
      }
    ]
  }
}

3.query filter

yagao, 25


GET /ecummerce/product/_search
{
  "query":{
    "bool": {
      "must": {
          "match": {
            "name": "yagao"
          }
        },
        "filter":{
          "range":{
            "price":{
              "gt":25
            }
          }
        }
      }
    }
  }


{
  "took" : 35,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.13353139,
    "hits" : [
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "3",
        "_score" : 0.13353139,
        "_source" : {
          "name" : "zhonghua yagao",
          "desc" : "caoben zhizu",
          "price" : 40,
          "product" : "zhonghua producter",
          "tags" : [
            "qingxin"
          ]
        }
      },
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "1",
        "_score" : 0.13353139,
        "_source" : {
          "name" : "gaolujie yagao",
          "desc" : "gaoxiao meibai",
          "price" : 30,
          "product" : "gaolujie producter",
          "tags" : [
            "fengzhu",
            "meibai"
          ]
        }
      }
    ]
  }
}

4.ful-text search ( )


GET /ecummerce/product/_search
{
  "query":{
    "match": {
      "product": "yagao producter"
    }
  }
  
}

producter ,


{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 0.110377684,
    "hits" : [
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "2",
        "_score" : 0.110377684,
        "_source" : {
          "name" : "jiajieshi yagao",
          "desc" : "jiajieshi fangzhu",
          "price" : 25,
          "product" : "jiajieshi producter",
          "tags" : [
            "fengzhu"
          ]
        }
      },
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "3",
        "_score" : 0.110377684,
        "_source" : {
          "name" : "zhonghua yagao",
          "desc" : "caoben zhizu",
          "price" : 40,
          "product" : "zhonghua producter",
          "tags" : [
            "qingxin"
          ]
        }
      },
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "1",
        "_score" : 0.110377684,
        "_source" : {
          "name" : "gaolujie yagao",
          "desc" : "gaoxiao meibai",
          "price" : 30,
          "product" : "gaolujie producter",
          "tags" : [
            "fengzhu",
            "meibai"
          ]
        }
      },
      {
        "_index" : "ecummerce",
        "_type" : "product",
        "_id" : "5",
        "_score" : 0.09271725,
        "_source" : {
          "name" : "special yagao",
          "desc" : "special meibai",
          "price" : 50,
          "product" : "special gaolujie producter",
          "tags" : [
            "meibai"
          ]
        }
      }
    ]
  }
}

5.highight search( )

6.

1. , tag


GET /ecummerce/product/_search
{
  "aggs":{
    "group_by_tages":{
      "terms": {
        "field": "tags"
      }
    }
  }
  
}

PUT /ecummerce/_mapping/product
{
  "properties":{
    "tags":{
      "type":"text",
      "fielddata":true
    }
  }
}

: yagao , tag


GET /ecummerce/product/_search
{
  "query":{
    "match": {
      "name": "yagao"
    }
  },
  "aggs":{
    "group_by_tages":{
      "terms": {
        "field": "tags"
      }
    }
  }
  
}

, , , tag 。


GET /ecummerce/product/_search
{
  "size":0,
  "aggs":{
    "group_by_tages":{
      "terms": {
        "field": "tags"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
  
}

 

좋은 웹페이지 즐겨찾기