cheeseandpepper
cheeseandpepper

Reputation: 405

Elasticsearch match query with terms array doesn't return partial match hits

I have 2 queries. The outcome is such that

Why does Query B with partial text not return hits? My expectation is that {"match": {"_all": "PARTIAL_OR_COMPLETE_TEXT_HERE"} would match in all 4 cases. The only difference I can tell is that the non-working query uses "terms": [] whereas the working query only uses "term": ""

Any help is much appreciated. Thanks.

Query A

{
  "size": 10000,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "_all": "RSTCPLS-1F56E8B836"
          }
        },
        {
          "term": {
            "merchant_id": 10012
          }
        },
        {
          "term": {
            "sb_status": "open"
          }
        },
        {
          "term": {
            "has_unfulfilled_quantity": "true"
          }
        },
        {
          "term": {
            "sb_payment_status": "all"
          }
        }
      ],
      "must_not": [],
      "should": [],
      "filter": [
        {
          "range": {
            "ordered_at": {
              "gte": "1992-04-01T17:01:39Z",
              "lte": "2017-05-01T10:01:39-07:00"
            }
          }
        },
        {
          "term": {
            "is_archived": "false"
          }
        }
      ]
    }
  }
}

Query B

{
  "size": 10000,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "_all": "test123"
          }
        },
        {
          "term": {
            "merchant_id": 10012
          }
        },
        {
          "terms": {
            "sb_status": [
              "open,",
              "completed"
            ]
          }
        },
        {
          "terms": {
            "last_shipment_state": [
              "created",
              "submitted",
              "processing"
            ]
          }
        },
        {
          "term": {
            "sb_payment_status": "all"
          }
        }
      ],
      "must_not": [],
      "should": [],
      "filter": [
        {
          "range": {
            "ordered_at": {
              "gte": "1992-04-01T17:03:12Z",
              "lte": "2017-05-01T10:03:12-07:00"
            }
          }
        },
        {
          "term": {
            "is_archived": "false"
          }
        }
      ]
    }
  }
}

UPDATE

Here is a more direct query that targets my nested field. I get hits for this query, but when I remove one character from the nested_items.sku, I get no hits.

{
  "size": 10000,
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "nested_items",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "nested_items.sku": "RSTCPLS-1F56E8B836"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Here is my mapping

{"orders_development_20170403084520375"=>
  {"mappings"=>
    {"order"=>
      {"_all"=>{"analyzer"=>"searchkick_index"},
       "dynamic_templates"=>
        [{"string_template"=>{"match"=>"*", "match_mapping_type"=>"string", "mapping"=>{"fields"=>{"analyzed"=>{"index"=>"analyzed", "type"=>"text"}}, "ignore_above"=>1024, "include_in_all"=>true, "type"=>"keyword"}}}],
       "properties"=>
        {"billing"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "billing_contact"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "billing_email"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "channel_id"=>{"type"=>"long"},
         "created_at"=>{"type"=>"date"},
         "display_ref"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "has_unfulfilled_quantity"=>{"type"=>"boolean"},
         "is_archived"=>{"type"=>"boolean"},
         "item_names"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "last_shipment_state"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text"}}, "include_in_all"=>true, "ignore_above"=>1024},
         "merchant_id"=>{"type"=>"long"},
         "nested_items"=>
          {"type"=>"nested",
           "properties"=>
            {"extended_total_in_cents"=>{"type"=>"long"},
             "name"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text"}}, "include_in_all"=>true, "ignore_above"=>1024},
             "quantity"=>{"type"=>"long"},
             "quantity_fulfilled"=>{"type"=>"long"},
             "sku"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text"}}, "include_in_all"=>true, "ignore_above"=>1024}}},
         "order_ref"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "ordered_at"=>{"type"=>"date"},
         "sb_order_seq"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text"}}, "include_in_all"=>true, "ignore_above"=>1024},
         "sb_payment_status"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text"}}, "include_in_all"=>true, "ignore_above"=>1024},
         "sb_status"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text"}}, "include_in_all"=>true, "ignore_above"=>1024},
         "shipping_contact"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "shipping_email"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "skus"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "status"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text"}}, "include_in_all"=>true, "ignore_above"=>1024},
         "total_in_cents"=>{"type"=>"long"},
         "updated_at"=>{"type"=>"date"},
         "warehouse_ids"=>{"type"=>"long"}}},
     "_default_"=>
      {"_all"=>{"analyzer"=>"searchkick_index"},
       "dynamic_templates"=>
        [{"string_template"=>{"match"=>"*", "match_mapping_type"=>"string", "mapping"=>{"fields"=>{"analyzed"=>{"index"=>"analyzed", "type"=>"text"}}, "ignore_above"=>1024, "include_in_all"=>true, "type"=>"keyword"}}}],
       "properties"=>
        {"billing"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "billing_contact"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "billing_email"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "display_ref"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "item_names"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "order_ref"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "shipping_contact"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "shipping_email"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024},
         "skus"=>{"type"=>"keyword", "fields"=>{"analyzed"=>{"type"=>"text", "analyzer"=>"searchkick_index"}, "word_middle"=>{"type"=>"text", "analyzer"=>"searchkick_word_middle_index"}}, "ignore_above"=>1024}}}}}}

Upvotes: 0

Views: 571

Answers (1)

cheeseandpepper
cheeseandpepper

Reputation: 405

Well I solved my problem. It was simpler than I thought. In setting up my nested mapping, I needed to add "index": "not_analyzed"

order: {
  properties: {
    nested_items: {
      type: "nested",
      properties: {
        "sku" => {type: "string", index: "not_analyzed"},
        "name" => {type: "string", index: "not_analyzed"}
      }
    }
  }
}

Hopefully this helps others who encounter similar problems.

Upvotes: 1

Related Questions