【问题标题】:Elasticsearch Query String Query returns all documentsElasticsearch Query String 查询返回所有文档
【发布时间】:2018-12-08 07:02:04
【问题描述】:

我有一个名为 users 的索引

当我使用以下查询向http://localhost:9200/users/_search?pretty=true 发出请求时:

curl -X GET "localhost:9200/users/_search?pretty=true" -H 'Content-Type: application/json' -d'
{
"query": {
    "query_string": {
    "query" : "firstName: Daulet"
}
}
}'

查询返回两个具有以下名称的用户:

firstName: Daulet

firstName: Daulet Nurlanuly

如何使查询字符串查询返回带有firstName: Daulet 的文档?

我查看了 Elasticsearch 使用 Apache Lucene 的请求语法,对于严格搜索,我需要通过将请求括在引号中来执行以下操作:

firstName: "Daulet"

但是已经用引号括起来了

如何仅使用查询字符串查询来做到这一点?

** 更新 **

我在http://localhost:9200/users 发出 GET 请求时得到的响应:

{
    "users": {
        "aliases": {},
        "mappings": {
            "userentity": {
                "properties": {
                    "firstName": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "id": {
                        "type": "long"
                    },
                    "language": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "lastName": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "refresh_interval": "1s",
                "number_of_shards": "5",
                "provided_name": "users",
                "creation_date": "1530245236170",
                "store": {
                    "type": "fs"
                },
                "number_of_replicas": "1",
                "uuid": "IlE1Ynv2Q462LBttptVaTg",
                "version": {
                    "created": "5060999"
                }
            }
        }
    }
}

【问题讨论】:

    标签: apache elasticsearch lucene


    【解决方案1】:

    您是正确的,您需要用双引号将值括起来。你在正确的道路上,你只需要转义双引号并使用firstName.keyword字段而不是firstName,基本上是这样的:

    curl -X GET "localhost:9200/users/_search?pretty=true" -H 'Content-Type: application/json' -d'
    {
    "query": {
        "query_string": {
        "query" : "firstName.keyword:\"Daulet\""
    }
    }
    }'
    

    【讨论】:

    • 不幸的是,它只有在我搜索 " firstname: \"Daulet Nurlanuly\" " 时才有效。如果我尝试“ firstname: \"Daulet\" ”,它会再次返回。令人惊讶的是,它适用于“ firstname: \"Nurlanuly\" ",因为它返回用户 Daulet Nurlanuly
    • 好吧,那是因为firstName被分析了。你能用从curl -XGET localhost:9200/users 得到的结果更新你的问题吗?
    • 谢谢,成功了!很抱歉,即使现在我正在寻找您提供的解决方案,我也很难在 Elasticsearch 的文档中找到它。我在哪里可以了解更多信息?
    猜你喜欢
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    相关资源
    最近更新 更多