与术语数组的Elasticsearch匹配查询不会返回部分匹配命中

时间:2017-04-01 17:25:04

标签: elasticsearch

我有2个查询。结果如此

  • 具有完全文本匹配的查询A返回匹配
  • 使用部分文字匹配的查询A返回匹配
  • 使用完全文本匹配的查询B返回匹配
  • 使用部分文字 的查询B未返回匹配

为什么带有部分文本的查询B不会返回命中?我的期望是{"match": {"_all": "PARTIAL_OR_COMPLETE_TEXT_HERE"}在所有4个案例中都匹配。我可以告诉的唯一区别是,非工作查询使用"terms": []而工作查询仅使用"term": ""

非常感谢任何帮助。感谢。

查询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"
          }
        }
      ]
    }
  }
}

查询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"
          }
        }
      ]
    }
  }
}

更新

这是一个针对我的嵌套字段的更直接的查询。我得到了这个查询的点击量,但是当我从nested_items.sku删除一个字符时,我没有点击。

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

这是我的映射

{"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}}}}}}

1 个答案:

答案 0 :(得分:1)

我解决了我的问题。它比我想象的要简单。在设置嵌套映射时,我需要添加"index": "not_analyzed"

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

希望这有助于遇到类似问题的其他人。