我在 flex 搜索中有此文件
{
"_index" : "master",
"_type" : "_doc",
"_id" : "q9IGdXABeXa7ITflapkV",
"_score" : 0.0,
"_source" : {
"customer_acct" : "64876457056",
"ssn_number" : "123456789",
"name" : "Julie",
"city" : "NY"
}
我想用customer_acct和ssn_number查询主索引,以检索整个文档。我想禁用评分和相关性,我使用了以下查询
curl -X GET "localhost/master/_search/?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"customer_acct": {
"value":"64876457056"
}
}
}
}'
我还需要在术语查询中包括第二个条件,即
ssn_number
,我该怎么做? ,我想关闭评分和相关性,这对Elastic Search是我的新手,我如何在上述尝试的查询中将ssn_number
的第二个条件拟合?
最佳答案
首先,您需要定义索引的正确映射。您的customer_acct
和ssn_number
是数字类型,但您将其存储为字符串。同样查看您的样本,我可以看到您必须使用long
来存储它们。然后您就可以在查询中使用过滤器上下文,因为您的结果不需要分数和相关性。阅读official ES doc中的过滤器上下文以及链接中的以下片段,以了解更多信息。
In a filter context, a query clause answers the question “Does this document match this query clause?” The answer is a simple Yes or No — no scores are calculated. Filter context is mostly used for filtering structured data,
,这正是您的用例。
1.索引映射
{
"mappings": {
"properties": {
"customer_acct": {
"type": "long"
},
"ssn_number" :{
"type": "long"
},
"name" : {
"type": "text"
},
"city" :{
"type": "text"
}
}
}
}
2.索引样本文档
{
"name": "Smithe John",
"city": "SF",
"customer_acct": 64876457065,
"ssn_number": 123456790
}
{
"name": "Julie",
"city": "NY",
"customer_acct": 64876457056,
"ssn_number": 123456789
}
3.主要搜索查询过滤不计分
{
"query": {
"bool": {
"filter": [ --> only filter clause
{
"term": {
"customer_acct": 64876457056
}
},
{
"term": {
"ssn_number": 123456789
}
}
]
}
}
}
上面的搜索查询给出以下结果:
{
"took": 186,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "so-master",
"_type": "_doc",
"_id": "1",
"_score": 0.0, --> notice score is 0.
"_source": {
"name": "Smithe John",
"city": "SF",
"customer_acct": 64876457056,
"ssn_number": 123456789
}
}
]
}
}
关于elasticsearch - 在多个条件下查询 Elasticsearch ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60405660/