Skip to content

Instantly share code, notes, and snippets.

@hkulekci
Last active February 2, 2023 13:05
Show Gist options
  • Save hkulekci/2428d8938701af895ec07b7e0cf82b85 to your computer and use it in GitHub Desktop.
Save hkulekci/2428d8938701af895ec07b7e0cf82b85 to your computer and use it in GitHub Desktop.

ES Requests :

DELETE msm_test

PUT msm_test
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  }
}

POST msm_test/doc/_mapping
{
  "properties": {
    "id": {
      "type": "integer"
    },
    "fuelType": {
      "type": "keyword"
    },
    "gearbox": {
      "type": "keyword"
    },
    "seats": {
      "type": "integer"
    },
    "doors": {
      "type": "integer"
    },
    "colour": {
      "type": "keyword"
    },
    "make": {
      "type": "keyword"
    },
    "model": {
      "type": "keyword"
    }
  }
}


POST msm_test/doc
{
  "id": 1,
  "fuelType": "diesel",
  "gearbox": "automatic",
  "seats": 4,
  "doors": 4,
  "colour": "yellow",
  "make": "BMW",
  "model": "1 Series"
}

POST msm_test/doc
{
  "id": 2,
  "fuelType": "diesel",
  "gearbox": "automatic",
  "seats": 4,
  "doors": 4,
  "colour": "black",
  "make": "BMW",
  "model": "1 Series"
}

POST msm_test/doc
{
  "id": 3,
  "fuelType": "diesel",
  "gearbox": "automatic",
  "seats": 4,
  "doors": 4,
  "colour": "white",
  "make": "BMW",
  "model": "2 Series"
}

POST msm_test/doc
{
  "id": 4,
  "fuelType": "petrol",
  "gearbox": "automatic",
  "seats": 4,
  "doors": 5,
  "colour": "white",
  "make": "BMW",
  "model": "2 Series"
}

POST msm_test/doc
{
  "id": 5,
  "fuelType": "petrol",
  "gearbox": "automatic",
  "seats": 4,
  "doors": 5,
  "colour": "white",
  "make": "BMW",
  "model": "3 Series"
}

POST msm_test/doc
{
  "id": 6,
  "fuelType": "petrol",
  "gearbox": "automatic",
  "seats": 4,
  "doors": 5,
  "colour": "white",
  "make": "Audi",
  "model": "A1"
}

POST msm_test/doc
{
  "id": 7,
  "fuelType": "diesel",
  "gearbox": "automatic",
  "seats": 4,
  "doors": 5,
  "colour": "white",
  "make": "Audi",
  "model": "A1"
}


POST msm_test/doc
{
  "id": 8,
  "fuelType": "petrol",
  "gearbox": "manuel",
  "seats": 4,
  "doors": 5,
  "colour": "white",
  "make": "Audi",
  "model": "A1"
}


POST msm_test/doc
{
  "id": 9,
  "fuelType": "diesel",
  "gearbox": "manuel",
  "seats": 4,
  "doors": 5,
  "colour": "black",
  "make": "Audi",
  "model": "A1"
}

POST msm_test/doc
{
  "id": 10,
  "fuelType": "diesel",
  "gearbox": "manuel",
  "seats": 4,
  "doors": 5,
  "colour": "red",
  "make": "Audi",
  "model": "A1"
}
ID Fuel Type Gearbox Seats Doors Colour Make Model
1 diesel automatic 4 4 yellow BMW 1 Series
2 diesel automatic 4 4 black BMW 1 Series
3 diesel automatic 4 4 white BMW 2 Series
4 petrol automatic 4 5 white BMW 2 Series
5 petrol automatic 4 5 white BMW 3 Series
6 petrol automatic 4 5 white Audi A1
7 diesel automatic 4 5 white Audi A1
8 petrol manuel 4 5 white Audi A1
9 diesel manuel 4 5 black Audi A1
10 diesel manuel 4 5 red Audi A1
GET msm_test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "fuelType": {
              "value": "diesel"
            }
          }
        },
        {
          "term": {
            "seats": {
              "value": "4"
            }
          }
        },
        {
          "term": {
            "make": {
              "value": "Audi"
            }
          }
        }
      ]
    }
  }
}
GET msm_test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "fuelType": {
              "value": "diesel"
            }
          }
        },
        {
          "term": {
            "seats": {
              "value": "4"
            }
          }
        },
        {
          "term": {
            "make": {
              "value": "Audi"
            }
          }
        }
      ], 
      "minimum_should_match": 2
    }
  }
}

Matched :

ID Fuel Type Gearbox Seats Doors Colour Make Model
7 diesel automatic 4 5 white Audi A1
9 diesel manuel 4 5 black Audi A1
10 diesel manuel 4 5 red Audi A1
6 petrol automatic 4 5 white Audi A1
8 petrol manuel 4 5 white Audi A1
1 diesel automatic 4 4 yellow BMW 1 Series
2 diesel automatic 4 4 black BMW 1 Series
3 diesel automatic 4 4 white BMW 2 Series

The others :

ID Fuel Type Gearbox Seats Doors Colour Make Model
4 petrol automatic 4 5 white BMW 2 Series
5 petrol automatic 4 5 white BMW 3 Series
GET msm_test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "colour": {
              "value": "white"
            }
          }
        }
      ], 
      "should": [
        {
          "term": {
            "fuelType": {
              "value": "diesel"
            }
          }
        },
        {
          "term": {
            "seats": {
              "value": "4"
            }
          }
        },
        {
          "term": {
            "make": {
              "value": "Audi"
            }
          }
        }
      ], 
      "minimum_should_match": 2
    }
  }
}
ID Fuel Type Gearbox Seats Doors Colour Make Model
7 diesel automatic 4 5 white Audi A1
6 petrol automatic 4 5 white Audi A1
8 petrol manuel 4 5 white Audi A1
3 diesel automatic 4 4 white BMW 2 Series
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment