Elasticsearch: filter => query

 

put wares
{
  "settings":{
    "number_of_shards": 1,
    "number_of_replicas":0
  },
  "mappings":{
    "properties":{
      "id":{
        "type":"integer"
      },
      "title":{
        "type": "keyword"
      },
      "price":{
        "type":"double"
      },
      "create_at":{
        "type":"date"
      },
      "description":{
        "type":"text",
        "analyzer": "ik_max_word"
      }
    }
  }
}

get _cat/indices?v

get wares/_mapping

delete orders


# 手动指定_id
post wares/_doc/1
{
  "id": 1,
  "title": "小浣熊",
  "price": 0.5,
  "create_at": "2022-11-02",
  "description": "小浣熊真好吃"
}

post wares/_doc/2
{
  "id": 2,
  "title": "鱼豆腐",
  "price": 4.8,
  "create_at": "2022-11-02",
  "description": "鱼豆腐很不错,真好吃rtyu uiop"
}


# 自动生成_id
post wares/_doc
{
  "id": 3,
  "title": "日本豆",
  "price": 1.8,
  "create_at": "2022-11-02",
  "description": "日本豆很不错"
}

post wares/_doc
{
  "id": 4,
  "title": "红烧排骨鱼翅",
  "price": 7.8,
  "create_at": "2022-10-02",
  "description": "红烧排骨鱼翅, 这个菜很独特"
}

post wares/_doc
{
  "id": 4,
  "title": "红烧排骨鱼翅",
  "price": 7.8,
  "create_at": "2022-10-02",
  "description": "红烧排骨鱼翅, 这个菜很独特,好吃好吃"
}

get valian/_search
{
  "query":{
    "term":{
      "description": {
        "value": "好吃"
      }
    }
  }
}

get valian/_search
{
  "query":{
    "bool":{
      "must": [
        {
          "term":{
            "description": {"value": "好吃"}
          }
        }
      ]
    }
  }
}

# filter term
get valian/_search
{
  "query":{
    "bool":{
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "term":{
            "description": {"value": ""}
          }
        }
      ]
    }
  }
}

# filter terms
get valian/_search
{
  "query":{
    "bool":{
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "terms":{
            "description": ["好吃","日本"]
          }
        }
      ]
    }
  }
}

# filter range
get valian/_search
{
  "query":{
    "bool":{
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "range": {
            "price": {
              "gte": 0,
              "lte": 4
            }
          }
        }
      ]
    }
  }
}

# filter exists
get valian/_search
{
  "query":{
    "bool":{
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "exists": {
            "field": "price"
          }
        }
      ]
    }
  }
}

# filter ids
get valian/_search
{
  "query":{
    "bool":{
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "ids":{
            "values": [1,2]
          }
        }
      ]
    }
  }
}




get valian/_search
{
  "query":{
    "bool":{
      "must": [
        {
          "term":{
            "description": {"value": "好吃"}
          }
        }
      ],
      "filter": [
        {
          "term":{
            "description": {"value": ""}
          }
        }
      ]
    }
  }
}

 

posted @ 2022-03-19 10:15  ascertain  阅读(23)  评论(0编辑  收藏  举报