Elasticsearch: Term Query vs Match Query

Given the sample data, consider the following two queries:

Query 1

GET bedmaster/_search
{
    "query" : {
        "term" : { "Parameter" : "HR" }
    }

}

Query 2

GET bedmaster/_search
{
    "query" : {
        "match" : { "Parameter" : "HR" }
    }

}

While clearly the documents with Parameter: HR exist, Query 1 does not return anything but Query 2 returns results as expected. Why? This is because “The term query finds documents that contain the exact term specified in the inverted index.”, see the guide.

The content in a document is tokenized into terms and reverted indexed, which means Elasticsearch knows in which document the term being queried appears. When term is used in a query, one needs to provide the tokenized content in order for the match, in this case, lowercased “HR”, see Query 3.

Query 3

GET bedmaster/_search
{
    "query" : {
        "term" : { "Parameter" : "hr" }
    }

}

By default the string field is analyzed, which means the content will go through some types of analyzer to get tokenized for full text search. term query does not care about this process but just tries to match the tokens therefore the content provided in a term query needs to be tokenized as well. On the other hand, match query, in Query 2, will send the query clause to the analyzer first thus it sees the hits even with the uppercased “HR”.

Advertisements

Sample XML Data (1)

<?xml version="1.0"?>
<!--BedMasterEx Version 4.3-->
<BedMasterEx>
<VitalSignInfo>
            <Location>K2ICU_BED01</Location>
            <Label>W027</Label>
            <Started>10/07/15 15:02</Started>
            <Stopped>10/08/15 16:08</Stopped>
            <Interval>1 Minute</Interval>
            <Duration>1.01:06:25</Duration>
            <Averaged>False</Averaged>
            <Comment >test comment </Comment>
      </VitalSignInfo>
  <VitalSigns CollectionTime="10/7/15 15:02:09">
            <VitalSign>
                  <Parameter>HR</Parameter>
                  <Value UnitOfMeasure="Bpm">128</Value>
                  <AlarmLimitLow Label="HR LO">50</AlarmLimitLow>
                  <AlarmLimitHigh Label="HR HI">150</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>PVC</Parameter>
                  <Value UnitOfMeasure="Bpm">0</Value>
                  <AlarmLimitHigh Label="PVC HI">10</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>ST-AVR</Parameter>
                  <Value UnitOfMeasure="mm">-0.5</Value>
                  <AlarmLimitLow Label="ST-AVR LO">0</AlarmLimitLow>
                  <AlarmLimitHigh Label="ST-AVR HI">10</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>ST-AVL</Parameter>
                  <Value UnitOfMeasure="mm">0.9</Value>
                  <AlarmLimitLow Label="ST-AVL LO">0</AlarmLimitLow>
                  <AlarmLimitHigh Label="ST-AVL HI">10</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>ST-AVF</Parameter>
                  <Value UnitOfMeasure="mm">-0.4</Value>
                  <AlarmLimitLow Label="ST-AVF LO">0</AlarmLimitLow>
                  <AlarmLimitHigh Label="ST-AVF HI">10</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>ST-I</Parameter>
                  <Value UnitOfMeasure="mm">0.8</Value>
                  <AlarmLimitLow Label="ST-I LO">0</AlarmLimitLow>
                  <AlarmLimitHigh Label="ST-I HI">10</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>ST-II</Parameter>
                  <Value UnitOfMeasure="mm">0.2</Value>
                  <AlarmLimitLow Label="ST-II LO">0</AlarmLimitLow>
                  <AlarmLimitHigh Label="ST-II HI">10</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>ST-III</Parameter>
                  <Value UnitOfMeasure="mm">-1</Value>
                  <AlarmLimitLow Label="ST-III LO">0</AlarmLimitLow>
                  <AlarmLimitHigh Label="ST-III HI">10</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>ST-V</Parameter>
                  <Value UnitOfMeasure="mm">-0.1</Value>
                  <AlarmLimitLow Label="ST-V LO">0</AlarmLimitLow>
                  <AlarmLimitHigh Label="ST-V HI">10</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>ST-V1</Parameter>
                  <Value UnitOfMeasure="mm">-0.1</Value>
                  <AlarmLimitLow Label="ST-V1 LO">-2.0</AlarmLimitLow>
                  <AlarmLimitHigh Label="ST-V1 HI">2.0</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>RESP</Parameter>
                  <Value UnitOfMeasure="BrMin">28</Value>
                  <AlarmLimitLow Label="RESP LO">8</AlarmLimitLow>
                  <AlarmLimitHigh Label="RESP HI">30</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>APNEA</Parameter>
                  <Value UnitOfMeasure="">0</Value>
                  <AlarmLimitLow Label="APNEA LO">8</AlarmLimitLow>
                  <AlarmLimitHigh Label="APNEA HI">30</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>AR1-S</Parameter>
                  <Value UnitOfMeasure="mmHg">85</Value>
                  <AlarmLimitLow Label="AR1-S LO">76</AlarmLimitLow>
                  <AlarmLimitHigh Label="AR1-S HI">200</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>AR1-D</Parameter>
                  <Value UnitOfMeasure="mmHg">43</Value>
                  <AlarmLimitLow Label="AR1-D LO">37</AlarmLimitLow>
                  <AlarmLimitHigh Label="AR1-D HI">90</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>AR1-M</Parameter>
                  <Value UnitOfMeasure="mmHg">56</Value>
                  <AlarmLimitLow Label="AR1-M LO">40</AlarmLimitLow>
                  <AlarmLimitHigh Label="AR1-M HI">120</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>AR1-R</Parameter>
                  <Value UnitOfMeasure="Bpm">128</Value>
                  <AlarmLimitLow Label="AR1-R LO">50</AlarmLimitLow>
                  <AlarmLimitHigh Label="AR1-R HI">150</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>SPO2-R</Parameter>
                  <Value UnitOfMeasure="Bpm">127</Value>
                  <AlarmLimitLow Label="SPO2-R LO">50</AlarmLimitLow>
                  <AlarmLimitHigh Label="SPO2-R HI">130</AlarmLimitHigh>
            </VitalSign>
            <VitalSign>
                  <Parameter>SPO2-%</Parameter>
                  <Value UnitOfMeasure="%">95</Value>
                  <AlarmLimitLow Label="SPO2-% LO">90</AlarmLimitLow>
                  <AlarmLimitHigh Label="SPO2-% HI">101</AlarmLimitHigh>
            </VitalSign>
      </VitalSigns>
            
</BedMasterEx>

Elasticsearch Cheat Sheet

Create index

ES can automatically create index during data ingestion but also allow one to create index in advance, where settings such as shards and replicas can be explicitly specified.

PUT /index_name
{
  "settings": {
    "number_of_shards" : 2,
    "number_of_replicas" : 1
  }
}

References

Queries

Assuming there is an index test that includes two types: list and other, one can perform some simple queries to get insight of the data.

List all the indices in an Elasticsearch instance and check cluster health.

GET /_cat/indices
GET /_cat/health

List mappings and settings of test index.

GET /test

List all documents in test index.

GET /test/_search

List matching documents in test

GET test/list/_search
{
  "query": {
    "match": {
      "tags": "elasticsearch" 
    }
  }
}