Elasticsearch Query String Query返回所有文档

时间:2018-06-29 06:39:37

标签: apache elasticsearch lucene

我有一个名为用户的索引

当我通过以下查询在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"
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您是正确的,需要用双引号将值引起来。您走在正确的道路上,只需要转义双引号并使用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\""
}
}
}'