Skip to main content

ElasticSearch query with AND and OR criteria

ElasticSearch query with AND and OR criteria


Frequently when we will need to construct query to elasticsearch which bear AND and OR criteria similar to the following SQL statements:

Statement 1:
SELECT * FROM tblOrder WHERE orderDate=2015-01-01 14:00:00 AND customerID=29439;

Statement 2:
SELECT * FROM tblOrder WHERE orderDate=2015-01-01 14:00:00 OR customerID=29439;

Statement 3:
SELECT * FROM tblOrder WHERE orderDate <= 2015-01-01 14:00:00 AND customerID=29439;

Statement 4:
SELECT * FROM tblOrder WHERE (orderDate=2015-01-01 14:00:00 AND customerID=29439) OR customerID = 20991;

Statement 5:
SELECT * FROM tblOrder WHERE orderDate=2015-01-01 14:00:00 AND (customerID=29439 OR customerID = 20991);

In ElasticSearch, we use "must" and "should" in place of AND and OR and "bool" in place of WHERE.

Suppose in our ElasticSearch (at 179.168.0.1:9200), we have indexed documents having the following structures at index myOrder/myOrder:

{
"orderID" : xxxxx,
"customerID"  : xxxxx,
"orderDate" : "yyyyMMddTHHmmss",
"itemLines" [
  {
   "itemLineID" : xxxx,
   "productID" : yyyyy,
   "quantity" : xxxxx
  },
  ...
]
}

Below is the translation of the above SQL statements to equivalent ElasticSearch query, in the following example we use curl, and we want to get a max of 100 records in each query from start, i.e., 0):

Statement 1:
curl -XGET http://179.168.0.1:9200/myOrder/myOrder/_search d
{
"from" : 0,
"size" : 100,
"query": {
   "bool" : {
       "must" : [
          "match" : {"orderDate" : "20150101T140000" },
          "match" : {"customerID" : 29439 }
       ]
   }
}
}

Statement 2:
curl -XGET http://179.168.0.1:9200/myOrder/myOrder/_search d
{
"from" : 0,
"size" : 100,
"query": {
   "bool" : {
       "should" : [
          "match" : {"orderDate" : "20150101T140000" },
          "match" : {"customerID" : 29439 }
       ]
   }
}
}

Statement 3:
curl -XGET http://179.168.0.1:9200/myOrder/myOrder/_search d
{
"from" : 0,
"size" : 100,
"query": {
   "bool" : {
       "must" : [
          "range" : {"orderDate" : { "lte" : "20150101T140000" } },
          "match" : {"customerID" : 29439 }
       ]
   }
}
}

Statement 4:
curl -XGET http://179.168.0.1:9200/myOrder/myOrder/_search d
{
"from" : 0,
"size" : 100,
"query": {
   "bool" : {
       "should" : [
          "bool" : {
              "must" : [
                 "match" : {"orderDate" : "20150101T140000" },
                 "match" : {"customerID" : 29439 }
              ]
          },
          "match" : { "customerID " :  20991}
       ]
   }
}
}

Statement 5:
curl -XGET http://179.168.0.1:9200/myOrder/myOrder/_search d
{
"from" : 0,
"size" : 100,
"query": {
   "bool" : {
       "must" : [
          "match" : {"orderDate" : "20150101T140000" },
          "bool" : {
              "should" : [
                   "match" : { "customerID " :  20991}, 
                   "match" : {"customerID" : 29439 }
              ]
          }
       ]
   }
}
}

download file now

Popular posts from this blog

Eclipse Android on Linux Mint 13 Maya

Eclipse Android on Linux Mint 13 Maya So far, my previous blogs on Linux Mint 13 has described how to install Java and the Android SDK. If you have followed the blogs, you will now be ready for the next step towards Android software development and that is to install a Java programming environment called Eclipse. Of course, first we have to install this (if it has not been installed yet). The install process is very simple - you can use either Synaptic or the command line to carry out this job. To use Synaptic, start Synaptic, enter your password, then type eclipse into the search box. The entry for Eclipse will appear - mark it for installation, and the rest of the additional items, and then click on the Apply button. Wait for download and install process to finish, close Synaptic and you are done! To use the command line, start a terminal, and type: sudo apt-get install eclipse Type Enter when prompted, wait for the packages to be downloaded and installed, close the terminal and you ...

Easily Manage Connect To SSH Hosts With Guake Indicator

Easily Manage Connect To SSH Hosts With Guake Indicator Guake Indicator is an Ubuntu AppIndicator for Guake terminal users, useful for those who need to connect to multiple SSH hosts frequently: the indicator lets you easily manage your favorite SSH hosts and establish new SSH connections via Guake. Guake is a Quake-style drop-down terminal which can be invoked with a single keystroke and hidden by pressing the same key again. Guake Indicator features: easily connect to your SSH hosts and MySQL servers; run various commands / tasks (e.g. navigate to a folder, ping google.com and so on); GUI to add new SSH hosts or localhost tasks. The SSH hosts / commands can be added by using the Guake Indicator GUI or by editing the ~/.guake-indicator/guake-indicator.json configuration file. Install Guake Indicator in Ubuntu Guake Indicator can be installed in Ubuntu Utopic, Trusty or Saucy by using a PPA. To add the PPA and install Guake Indicator, use the following commands: sudo add-apt-repositor...

Emulator ipad

Emulator ipad Ingin mencoba dan mengalami nuansa iPad pada komputer Windows Anda ? Gunakan iPadian , simulator iPad gratis untuk Windows . ipadian iPadian : Sebuah Simulator iPad untuk Windows PC Kita telah melihat BlueStacks Pemain App yang memungkinkan Anda menjalankan aplikasi Android pada PC Windows Anda . iPadian memungkinkan Anda menjalankan aplikasi iPad pada komputer Windows Anda . iPadian adalah program yang berdiri sendiri gratis yang tidak mengubah pengaturan pada desktop Windows Anda . Ini hanya membuka layar identik dengan aplikasi layar , iPad penuh, berurusan dengan file dan program Windows di desktop Anda . Muncul dengan toko aplikasi kustom , dengan beberapa aplikasi Ekstra iPad pra -instal dukungan , modus layar penuh dan akses ke sumber informasi gratis untuk mendengarkan musik , menonton video, surfing internet , chatting, berkomunikasi di Facebook dan banyak lagi. iPadian adalah aplikasi portabel d...