elasticsearch 在开放式搜索中查询新字段不会检索结果

nwwlzxa7  于 2022-12-11  发布在  ElasticSearch
关注(0)|答案(2)|浏览(152)

我使用的是Opensearch 2.4,在创建时我有一个包含一些字段的索引,后来我开始将新字段保存到索引中,现在当我查询新创建的字段时,没有得到任何结果
例如:查询1 POST abc/_search

{
  "query": {
    "bool": {
        "must": [
            {
                "terms": {
                    "name": [
                        "john"
                    ]
                }
            }
        ]
    }
}   
}

上面的工作正常,因为名称字段在创建索引后就存在了
查询2:POST abc/_搜索

{
  "query": {
    "bool": {
        "must": [
            {
                "terms": {
                    "lastname": [
                        "William"
                    ]
                }
            }
        ]
    }
}   
}

上面的查询不工作,虽然我有一些文件与姓氏william

vxbzzdmp

vxbzzdmp1#

When you index a new field without previously declaring it in the mapping, opensearch/elastic will generate text type and type keyword.
There are two ways for you to get results with the Term Query. First remember that Term query works with exact terms.
The first option is to use the keyword field.

{
            "terms": {
                "lastname.keyword": [
                    "William"
                ]
            }
        }

The second option is to search in the text field, but remember that when indexing the default parser is applied, then the lowecase filter leaves the token like this: william.
In this case, the query should be:

{
            "terms": {
                "lastname": [
                    "william"
                ]
            }
        }
kokeuurv

kokeuurv2#

使用**“terms”**时,必须完全匹配(包括大小写)。
因此,请确保您的文档包含William,而不是william或威廉姆斯
如果您想要更大的容差,可以探索匹配查询:
https://opensearch.org/docs/latest/opensearch/query-dsl/full-text/#match

相关问题