gpt4 book ai didi

elasticsearch - 当子元素是字符串数组时,搜索elasticsearch查询

转载 作者:行者123 更新时间:2023-12-02 22:51:32 25 4
gpt4 key购买 nike

我以以下格式在elasticsearch中创建了一个文档

curl -XPUT "http://localhost:9200/my_base.main_candidate/" -d'
{
"specific_location": {
"location_name": "Mumbai",
"location_tags": [
"Mumbai"
],
"tags": [
"Mumbai"
]
}
}'

我的要求是搜索包含给定选项之一的location_tags,例如[“孟买”,“浦那”]。我该怎么做呢?

我试过了:
curl -XGET "http://localhost:9200/my_base.main_candidate/_search" -d '
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"terms": {
"specific_location.location_tags" : ["Mumbai"]
}
}
}
}
}'

这没有用。

我得到了这个输出:
{
"took": 72,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

最佳答案

有几种解决方法。也许最直接的方法是搜索mumbai而不是Mumbai

如果我创建的索引没有映射,

curl -XDELETE "http://localhost:9200/my_base.main_candidate/"
curl -XPUT "http://localhost:9200/my_base.main_candidate/"

然后添加一个文档:
curl -XPUT "http://localhost:9200/my_base.main_candidate/doc/1" -d'
{
"specific_location": {
"location_name": "Mumbai",
"location_tags": [
"Mumbai"
],
"tags": [
"Mumbai"
]
}
}'

然后使用小写字母来运行查询
curl -XPOST "http://localhost:9200/my_base.main_candidate/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"terms": {
"specific_location.location_tags": [
"mumbai"
]
}
}
}
}
}'

我得到了预期的文档:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "my_base.main_candidate",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"specific_location": {
"location_name": "Mumbai",
"location_tags": [
"Mumbai"
],
"tags": [
"Mumbai"
]
}
}
}
]
}
}

这是因为,由于未使用显式映射,因此Elasticsearch使用默认值,这意味着 location_tags字段将使用 standard analyzer进行分析,这会将术语转换为小写。因此,术语 Mumbai不存在,但是 mumbai存在。

如果要在查询中使用大写字母,则需要设置一个明确的映射,该映射告诉Elasticsearch不要分析 location_tags字段。也许是这样的:
curl -XDELETE "http://localhost:9200/my_base.main_candidate/"

curl -XPUT "http://localhost:9200/my_base.main_candidate/" -d'
{
"mappings": {
"doc": {
"properties": {
"specific_location": {
"properties": {
"location_tags": {
"type": "string",
"index": "not_analyzed"
},
"tags": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}'

curl -XPUT "http://localhost:9200/my_base.main_candidate/doc/1" -d'
{
"specific_location": {
"location_name": "Mumbai",
"location_tags": [
"Mumbai"
],
"tags": [
"Mumbai"
]
}
}'

curl -XPOST "http://localhost:9200/my_base.main_candidate/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"terms": {
"specific_location.location_tags": [
"Mumbai"
]
}
}
}
}
}'

这是上面所有方便的代码:

http://sense.qbox.io/gist/74844f4d779f7c2b94a9ab65fd76eb0ffe294cbb

[编辑:顺便说一下,我在测试上述代码时使用了Elasticsearch 1.3.4]

关于elasticsearch - 当子元素是字符串数组时,搜索elasticsearch查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27400713/

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