Elasticsearch与条件数组匹配查询不返回部分匹配匹配

问题描述:

我有2个查询。结果是这样的,Elasticsearch与条件数组匹配查询不返回部分匹配匹配

  • 与确切的文本匹配返回查询命中
  • 与部分文本匹配返回查询命中
  • 与确切的文本匹配返回查询乙击中
  • 查询乙与部分文本不返回命中

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

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

查询

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

查询乙

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

这里是我的目标嵌套领域更直接查询。我查询了这个查询,但是当我从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}}}}}} 
+0

你有错字#行19:''打开,“,'(报价前的逗号和逗号后面的报价)?也只是一个愚蠢的问题 - 你确定你有文件满足*查询B *这样的标准吗? –

+0

啊,是的,谢谢你找到逗号。事实证明,这并不影响结果。我已经缩小到搜索词本身。 “12345”和“单字”在嵌套字段中不会产生部分匹配。但是,“多词”和“ABC-123-DEF-456”会在嵌套字段中找到匹配部分文本的匹配。我现在正在研究我的映射以及如何确保正确分析我的嵌套字段。 – cheeseandpepper

+0

所以,我认为这是值得分享你的索引映射:)并抱歉愚蠢的问题 - 我没有看到你在这里使用嵌套字段 - 请你详细说明你在上述查询中使用它的位置? –

嗯,我解决我的问题。这比我想象的更简单。在建立我的嵌套映射,我需要添加"index": "not_analyzed"

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

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