根据多个条件查询 Elastic Search
Query on Elastic Search on multiple criterias
我在弹性搜索中有这个文档
{
"_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 中阅读有关过滤器上下文的更多信息以及 link.
中的以下片段
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
}
}
]
}
}
我在弹性搜索中有这个文档
{
"_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 中阅读有关过滤器上下文的更多信息以及 link.
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
}
}
]
}
}