Elasticsearch default_operator "AND" 未按预期工作

Ada*_*ert 6 elasticsearch elasticsearch-dsl elasticsearch-query

我已经使用“AND”默认运算符设置了我的查询字符串搜索。我的查询如下:

{
    "query": {
        "query_string" : {
            "query" : "Adam KT2 7AJ",
            "default_operator" : "AND"
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这能提供与下面相同的结果......但事实并非如此。

{
    "query": {
        "query_string" : {
            "query" : "Adam AND KT2 AND 7AJ",
            "default_operator" : "OR"
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然我看到 default_operator 对我的搜索结果产生了影响,但它并没有像我希望的那样工作。

例如:

查询 1:

  • Adam AND KT2 AND 7AJ, default_operator: or
  • 结果:1(如预期)
  • [1] 姓名:Adam Lambert,邮政编码 KT2 7AJ

查询2:

  • Adam KT2 7AJ, default_operator: and
  • 结果:0
  • (不是预期的。这应该产生与上面相同的结果?似乎anddefault_operator 在查询中产生与 AND 不同的结果)

查询3:

  • KT2 7AJ, default_operator: and
  • 结果:2
  • (正如预期的那样,只匹配邮政编码的两个部分)
  • [1] 名称:a , 邮编 KT2 7AJ
  • [2] 姓名:b , 邮编 KT2 7AJ

QUERY4:

  • KT2 7AJ, default_operator: or
  • 结果:5
  • (正如预期的那样,各种邮政编码的对偶匹配)
  • [1] 名称:a , 邮编 KT2 7AJ
  • [2] 姓名:b , 邮编 KT2 7AJ
  • [3] 姓名:c , 邮编 N7 7AJ
  • [4] 姓名:d , 邮编 KT2 0DJ
  • [5] 姓名:e , 邮编 KT2 0PQ

编辑

这是我的映射。您会注意到帐单地址中有一个 post_code 字段,但有附加到 shipping_address 和客户的姓名字段。

当我将查询作为 运行时Adam AND KT2 7AJ,我们得到了 where 的结果customer.name:Adam AND billing_address.post_code:KT2 7AJ,这正是我所期望的。

如果我们删除 AND,我们会得到 0 个结果。

{
    "t1_orders_1584882055": {
        "mappings": {
            "properties": {
                "__class_name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "billing_address": {
                    "properties": {
                        "address1": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "address2": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "address3": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "company": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "country_code": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "created_at": {
                            "type": "date",
                            "format": "date_time"
                        },
                        "customer_id": {
                            "type": "long"
                        },
                        "email": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "id": {
                            "type": "keyword"
                        },
                        "name": {
                            "type": "text",
                            "analyzer": "edge_ngram_analyzer"
                        },
                        "phone": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "post_code": {
                            "type": "text",
                            "fields": {
                                "text": {
                                    "type": "text"
                                }
                            },
                            "analyzer": "no_space_analyzer"
                        },
                        "redacted": {
                            "type": "boolean"
                        },
                        "region": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "town": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "updated_at": {
                            "type": "date",
                            "format": "date_time"
                        }
                    }
                },
                "billing_address_id": {
                    "type": "long"
                },
                "channel": {
                    "properties": {
                        "created_at": {
                            "type": "date"
                        },
                        "id": {
                            "type": "long"
                        },
                        "identifier": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "updated_at": {
                            "type": "date"
                        }
                    }
                },
                "channel_id": {
                    "type": "long"
                },
                "created_at": {
                    "type": "date",
                    "format": "date_time"
                },
                "customer": {
                    "properties": {
                        "created_at": {
                            "type": "date",
                            "format": "date_time"
                        },
                        "email": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "id": {
                            "type": "keyword"
                        },
                        "name": {
                            "type": "text",
                            "analyzer": "edge_ngram_analyzer"
                        },
                        "redacted": {
                            "type": "boolean"
                        },
                        "updated_at": {
                            "type": "date",
                            "format": "date_time"
                        }
                    }
                },
                "customer_id": {
                    "type": "long"
                },
                "id": {
                    "type": "keyword"
                },
                "name": {
                    "type": "text",
                    "analyzer": "edge_ngram_analyzer"
                },
                "order_id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "order_items": {
                    "properties": {
                        "created_at": {
                            "type": "date"
                        },
                        "id": {
                            "type": "long"
                        },
                        "order_id": {
                            "type": "long"
                        },
                        "qty": {
                            "type": "long"
                        },
                        "sku": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "updated_at": {
                            "type": "date"
                        }
                    }
                },
                "received_at": {
                    "type": "date",
                    "format": "date_time"
                },
                "reference_number": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "ship_by": {
                    "type": "date"
                },
                "shipping_address": {
                    "properties": {
                        "address1": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "address2": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "address3": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "company": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "country_code": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "created_at": {
                            "type": "date",
                            "format": "date_time"
                        },
                        "customer_id": {
                            "type": "long"
                        },
                        "email": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "id": {
                            "type": "keyword"
                        },
                        "name": {
                            "type": "text",
                            "analyzer": "edge_ngram_analyzer"
                        },
                        "phone": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "post_code": {
                            "type": "text",
                            "fields": {
                                "text": {
                                    "type": "text"
                                }
                            },
                            "analyzer": "no_space_analyzer"
                        },
                        "redacted": {
                            "type": "boolean"
                        },
                        "region": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "town": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "updated_at": {
                            "type": "date",
                            "format": "date_time"
                        }
                    }
                },
                "shipping_address_id": {
                    "type": "long"
                },
                "status": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "updated_at": {
                    "type": "date",
                    "format": "date_time"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

示例文档:

{
  "_source": {
    "id": 6,
    "channel_id": 1,
    "customer_id": 1,
    "shipping_address_id": 11,
    "billing_address_id": 12,
    "order_id": "1234",
    "reference_number": "1234",
    "status": "open",
    "received_at": "2020-02-24T18:03:21.000000Z",
    "ship_by": "2020-02-24T18:03:21.000000Z",
    "completed_at": null,
    "created_at": "2020-03-18T16:17:28.000000Z",
    "updated_at": "2020-03-18T16:17:28.000000Z",
    "channel": {
      "id": 1,
      "name": "Amazon",
      "identifier": "The",
      "created_at": "2020-03-18T16:17:28.000000Z",
      "updated_at": "2020-03-18T16:17:28.000000Z"
    },
    "customer": {
      "id": 1,
      "name": "Nam Test",
      "email": "Nam@Test.com",
      "redacted": false,
      "created_at": "2020-03-18T16:17:28.000000Z",
      "updated_at": "2020-03-18T16:17:28.000000Z"
    },
    "shipping_address": {
      "id": 11,
      "customer_id": 1,
      "name": "Nam Test",
      "email": "Nam@Test.COm",
      "company": "",
      "address1": "44",
      "address2": "AD CLOSE",
      "address3": "",
      "town": "KINGSTON UPON THAMES",
      "region": "",
      "post_code": "KT2 7AJ",
      "country_code": null,
      "phone": "213223123",
      "redacted": false,
      "created_at": "2020-03-18T16:17:28.000000Z",
      "updated_at": "2020-03-18T16:17:28.000000Z"
    },
    "billing_address": {
      "id": 12,
      "customer_id": 1,
      "name": "Nam Test",
      "email": "Nam@Test.COm",
      "company": "",
      "address1": "44",
      "address2": "AD CLOSE",
      "address3": "",
      "town": "KINGSTON UPON THAMES",
      "region": "",
      "post_code": "KT2 7AJ",
      "country_code": null,
      "phone": "213223123",
      "redacted": false,
      "created_at": "2020-03-18T16:17:28.000000Z",
      "updated_at": "2020-03-18T16:17:28.000000Z"
    },
    "order_items": [
      {
        "id": 6,
        "order_id": 6,
        "sku": "10-2-sk-ue",
        "qty": 1,
        "created_at": "2020-03-18T16:17:28.000000Z",
        "updated_at": "2020-03-18T16:17:28.000000Z"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

jas*_*hal 0

从elasticsearch版本6.x开始,不再使用空格分割。

说明: 采用名称和邮政编码两个字段

1. Adam AND KT2 AND 7AJ,default_operator: or
查询转换为 :-
(Name:Adam OR PostalCode:Adam) OR (Name:KT2 OR PostalCode:KT2) OR (Name:7AJ OR PostalCode:7AJ)
文本按运算符拆分(AND,OR)并且每个分割都传递到各自的字段因此它返回名称为 Adam 且邮政编码包含 7AJ 或 KT2 的所有文档(令牌匹配)

2. Adam AND KT2 AND 7AJ,default_operator:and
查询转换为:-
(姓名:Adam OR 邮政编码:Adam) AND (姓名:KT2 OR 邮政编码:KT2) AND (姓名:7AJ OR 邮政编码:7AJ)

3. Adam KT2 7AJ,default_operator:并且
查询转换为:-(
名称:(Adam AND KT2 AND 7AJ)或邮政编码:(Adam AND KT2 AND 7AJ))
文本不会在空格上分割(这是在版本 6 之前完成的)并且被认为是一个文本块。因此,仅返回在任何字段中具有所有三个标记的文档

4. KT2 7AJ,default_operator:与
第 2 点文档相同,返回在任何字段中都具有两个标记的文档。

5. KT2 7AJ, default_operator: 或
与第 2 点文档相同,返回字段中带有任何 token(OR) 的文档

链接由 github 上的 Elastic Search 团队提供了类似的说明

如果您想继续使用现有结构,您可以使用括号将相似的单词组合起来。下面的查询将文本拆分为两个标记 Adam 和单个块 KT2 7AJ,这将转换为
(Name:Adam OR PostalCode:Adam) AND (Name:(KT2 AND 7AJ) OR PostalCode:(KT2 AND 7AJ)

{
    "query": {
        "query_string" : {
            "query" : "Adam (KT2 7AJ)",
            "default_operator" : "AND"
         }
    }
}
Run Code Online (Sandbox Code Playgroud)