gpt4 book ai didi

elasticsearch - Elasticsearch查询的子字段

转载 作者:行者123 更新时间:2023-12-02 23:07:19 26 4
gpt4 key购买 nike

我有一些基本文件,看起来像这种格式:

{
"firstName": "John",
"lastName": "Doe",
"address": {
"street": "",
"city": "Paris",
"countryCode": "FR"
},
"searchInCountry: false
}
我正在做一个简单的查询,如果正确,如果 searchInCountryfalseaddress.countryCodeFR,应该给我我的文档:
POST /myDoc/_search
{
"query": {
"bool": {
"should": [
{ "term": {"searchInCountry": false} },
{ "term": {"address.countryCode": "FR"} }
]
}
}
}
问题是 address.countryCode上的术语不起作用。即使 address.countryCode == FR也永远不会返回我的值(value)
例如,我有:
  • 一个文档,其中searchInCountryfalseaddress.countryCodeDE
  • 一个文档,其中searchInCountrytrueaddress.countryCodeFR

  • 上面的查询只会返回我的第一个查询,因为 searchInCountryfalse,但不是第二个查询,而 address.countryCodeFR匹配
    我正在使用Elasticsearch 5.5.1,我不知道它是否可以关联(它是旧的生产服务器)
    更新
    我的映射:
    {
    "mappings":{
    "myDoc":{
    "properties":{
    "firstName":{
    "type":"text"
    },
    "lastName":{
    "type":"text"
    },
    "searchInCountry":{
    "type":"boolean"
    },
    "address":{
    "type":"nested",
    "properties":{
    "city":{
    "type":"keyword"
    },
    "countryCode":{
    "type":"text"
    },
    "street":{
    "type":"text"
    },
    }
    }
    }
    }
    }
    }
    我也对嵌套进行了测试,但是问题是相同的:
      "query": {
    "bool": {
    "should": [
    {
    "nested": {
    "path": "address",
    "query": {
    "term": { "address.countryCode": {"value": "FR"} }
    }
    }
    },
    { "term": {"searchInCountry": false} }
    ]
    }
    }
    }
    更新2和解决方案:
    嵌套是必需的,但是我还需要从 term更改为 match,因为我的映射是 text而不是 keyword。感谢@saeednasehi的帮助

    最佳答案

    因为您有嵌套的对象,所以需要定义搜索路径。您的查询可能是这样的:

    {
    "query": {
    "bool": {
    "should": [
    {
    "nested": {
    "path": "address",
    "query": {
    "term": {
    "address.countryCode": {
    "value": "FR"
    }
    }
    }
    }
    },
    {
    "term": {
    "searchInCountry": false
    }
    }
    ]
    }
    }
    }

    关于elasticsearch - Elasticsearch查询的子字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64320352/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com