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

{
    "query": {
        "query_string" : {
            "query" : "Adam KT2 7AJ",
            "default_operator" : "AND"
         }
    }
}

我希望这将提供与以下相同的结果...但是事实并非如此。
{
    "query": {
        "query_string" : {
            "query" : "Adam AND KT2 AND 7AJ",
            "default_operator" : "OR"
         }
    }
}

虽然我看到default_operator对我的搜索结果有所影响,但它并没有达到我想要的效果。

例如:

QUERY1:
  • Adam AND KT2 AND 7AJ,默认运算符(operator):or
  • 结果:1(按预期)
  • [1]名称:亚当·兰伯特(Adam Lambert),邮政编码KT2 7AJ

  • QUERY2:
  • Adam KT2 7AJ,默认运算符(operator):and
  • 结果:0
  • (不期望。这应该产生与上面相同的结果?似乎是and,因为default_operator与查询中的AND产生不同的结果)

  • QUERY3:
  • KT2 7AJ,默认运算符(operator):and
  • 结果:2
  • (如预期,仅匹配邮政编码的两个部分)
  • [1]名称:a,邮政编码KT2 7AJ
  • [2]名称:b,邮政编码KT2 7AJ

  • QUERY4:
  • KT2 7AJ,默认运算符(operator):or
  • 结果:5
  • (如预期的那样,在各种邮政编码上进行部分匹配)
  • [1]名称:a,邮政编码KT2 7AJ
  • [2]名称:b,邮政编码KT2 7AJ
  • [3]名称:c,邮政编码N7 7AJ
  • [4]名称:d,邮政编码KT2 0DJ
  • [5]名称:e,邮政编码KT2 0PQ

  • 编辑

    这是我的映射。您会注意到,帐单邮寄地址中有一个邮政编码字段,但shipping_address和客户都附加了名称字段。

    当我以Adam AND KT2 7AJ的形式运行查询时,我们得到的结果是customer.name:AdamANDbilling_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"
                    }
                }
            }
        }
    }
    

    范例文件:
    {
      "_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": "[email protected]",
          "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": "[email protected]",
          "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": "[email protected]",
          "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"
          }
        ]
      }
    }
    

    最佳答案

    从 Elasticsearch 版本6.x开始,不再按空格分割。

    说明:
    取两个字段Name和PostalCode

    1. Adam AND KT2 AND 7AJ,default_operator:或
    查询翻译成:-
    (名称:Adam或邮政编码:Adam)或(名称:KT2或邮政编码:KT2)或(名称:7AJ或邮政编码:7AJ)
    文本在运算符(AND,OR)上拆分,每个拆分都传递到相应的字段
    因此它将返回所有名称为Adam且邮政编码包含7AJ或KT2的文档( token 匹配)

    2. Adam AND KT2 AND 7AJ,default_operator:和
    查询翻译成:-
    (名称:Adam或邮递区号:Adam)AND(名称:KT2或邮递区号:KT2)AND(名称:7AJ或邮递区号: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上的 Elasticsearch 团队对此link进行了类似的澄清

    如果您想继续使用现有结构,则可以使用方括号将类似的单词合并在一起。下面的查询将文本分为两个 token Adam和单个块KT2 7AJ
    这将转化为
    (名称:Adam或邮政编码:Adam)AND(名称:(KT2 AND 7AJ)或邮政编码:(KT2 AND 7AJ)

    {
        "query": {
            "query_string" : {
                "query" : "Adam (KT2 7AJ)",
                "default_operator" : "AND"
             }
        }
    }
    

    关于Elasticsearch default_operator "AND"无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60794719/

    10-11 08:35