如何使用Elasticsearch获取精确值以及具有空值的值

时间:2015-05-05 13:53:18

标签: elasticsearch

标题可能听起来有点矛盾,但会在下面进一步说明。

以下是示例数据

"_index": "sample.document",
"_type": "document",
"_id": "54eecc5b8014c225ec734259",
"_score": 2.1047661,
"_source": {
    "systemHeader": {
        "summaryName": "New Big Dropdown - Template",
        "keyID": [
            "542dda8d6803fa98058b4568",
            "543c99dd2dafa7c211b38546"
        ],
        "systemType": "template",
        "summaryDescriptionRule": "This is a Case for works",
        "summaryNameRule": "Case - {{CaseID}} {{Description}}",
        "summaryDescription": "This is the template to demonstrate a big dropdown",
        "date": "1970-01-01T00:00:00"
    }
}
"_index": "sample.document2",
"_type": "document",
"_id": "54eecc5b8014c225ec734260",
"_score": 2.1047661,
"_source": {
    "systemHeader": {
        "summaryName": "New Big Dropdown - Template",
        "keyID": [],
        "systemType": "template",
        "summaryDescriptionRule": "This is a Case for works",
        "summaryNameRule": "Case - {{CaseID}} {{Description}}",
        "summaryDescription": "This is the template to demonstrate a big dropdown",
        "date": "1970-01-01T00:00:00"
    }
}
"_index": "sample.document3",
"_type": "document",
"_id": "54eecc5b8014c225ec734261",
"_score": 2.1047661,
"_source": {
    "systemHeader": {
        "summaryName": "New Big Dropdown - Template",
        "keyID": [
            "542dda8d6803fa98058b4570",
            "543c99dd2dafa7c211b38571"
        ],
        "systemType": "template",
        "summaryDescriptionRule": "This is a Case for works",
        "summaryNameRule": "Case - {{CaseID}} {{Description}}",
        "summaryDescription": "This is the template to demonstrate a big dropdown",
        "date": "1970-01-01T00:00:00"
    }
}
"_index": "sample.document4",
"_type": "document",
"_id": "54eecc5b8014c225ec734262",
"_score": 2.1047661,
"_source": {
    "systemHeader": {
        "summaryName": "New Big Dropdown - Template",
        "systemType": "template",
        "summaryDescriptionRule": "This is a Case for works",
        "summaryNameRule": "Case - {{CaseID}} {{Description}}",
        "summaryDescription": "This is the template to demonstrate a big dropdown",
        "date": "1970-01-01T00:00:00"
    }
}

我想要发生的是我可以使用systemHeader.keyID进行查询,但我希望得到一个值为#34; 542dda8d6803fa98058b4568"还有那个无值的那个,所以让我们说,例如当我运行一个查询时,那些应该返回的是样本数据1,2和4.目标是keyIDs是用户对数据的特定访问权限。如果用户具有与特定数据匹配的keyID,则返回数据。如果数据没有accessKey,则意味着任何用户都可以立即访问它,无论用户是否具有访问密钥。最后,如果用户没有与结果相关的keyID,则不应该使用不同的keyID返回该数据。我希望我有道理。

但我似乎无法获得理想的结果。这是我的弹性代码:

{
    "query" : {
        "filtered" : {
            "filter" : {
                "or": [{
                    "missing" : {
                        "field" : "systemHeader.keyID"
                }},{
                "term": {
                    "systemHeader.keyID": ["542dda8d6803fa98058b4568"]
                }}]
            },
            "query" : {
                "match" : {
                    "systemHeader.systemType" : "template"
                }
            }
        }
    }
}

当我运行此代码时,会发生什么,它只返回样本数据4.它甚至不返回样本数据2,最糟糕的是它没有返回样本数据1.我可以改变什么我的查询,以便它可以返回样本1,2和4.我需要你的帮助。感谢。

编辑:示例4很奇怪,它在systemHeader下没有keyID,因为我假设我有多条记录。最近实现了KeyID,因此我有多条与样本4类似的记录,这就是为什么我要将样本4作为返回结果包含的原因。

1 个答案:

答案 0 :(得分:0)

当我在一个简单的测试中运行您在此处发布的内容时,它会返回您想要的文档,因此您的问题似乎在其他地方。

我注意到唯一的问题是您的"term"过滤器应该是"terms"过滤器,但我怀疑是否会导致您的问题(可能取决于您的ES版本)。

您的映射中是否有任何非默认值可以解释它?也许如果你发布你的映射它会有所帮助。另外,您使用的是什么版本的ES?我使用ES 1.5.1来进行下面的操作。

我刚创建了一个简单的索引:

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   }
}

然后将您的四份文件编入索引:

PUT /test_index/doc/54eecc5b8014c225ec734259
{
   "systemHeader": {
      "summaryName": "New Big Dropdown - Template",
      "keyID": [
         "542dda8d6803fa98058b4568",
         "543c99dd2dafa7c211b38546"
      ],
      "systemType": "template",
      "summaryDescriptionRule": "This is a Case for works",
      "summaryNameRule": "Case - {{CaseID}} {{Description}}",
      "summaryDescription": "This is the template to demonstrate a big dropdown",
      "date": "1970-01-01T00:00:00"
   }
}

PUT /test_index/doc/54eecc5b8014c225ec734260
{
   "systemHeader": {
      "summaryName": "New Big Dropdown - Template",
      "keyID": [],
      "systemType": "template",
      "summaryDescriptionRule": "This is a Case for works",
      "summaryNameRule": "Case - {{CaseID}} {{Description}}",
      "summaryDescription": "This is the template to demonstrate a big dropdown",
      "date": "1970-01-01T00:00:00"
   }
}

PUT /test_index/doc/54eecc5b8014c225ec734261
{
   "systemHeader": {
      "summaryName": "New Big Dropdown - Template",
      "keyID": [
         "542dda8d6803fa98058b4570",
         "543c99dd2dafa7c211b38571"
      ],
      "systemType": "template",
      "summaryDescriptionRule": "This is a Case for works",
      "summaryNameRule": "Case - {{CaseID}} {{Description}}",
      "summaryDescription": "This is the template to demonstrate a big dropdown",
      "date": "1970-01-01T00:00:00"
   }
}

PUT /test_index/doc/54eecc5b8014c225ec734262
{
   "systemHeader": {
      "summaryName": "New Big Dropdown - Template",
      "systemType": "template",
      "summaryDescriptionRule": "This is a Case for works",
      "summaryNameRule": "Case - {{CaseID}} {{Description}}",
      "summaryDescription": "This is the template to demonstrate a big dropdown",
      "date": "1970-01-01T00:00:00"
   }
}

然后运行您的搜索查询并找回您想要的三个文档:

POST /test_index/_search
{
   "query": {
      "filtered": {
         "filter": {
            "or": [
               {
                  "missing": {
                     "field": "systemHeader.keyID"
                  }
               },
               {
                  "term": {
                     "systemHeader.keyID": [
                        "542dda8d6803fa98058b4568"
                     ]
                  }
               }
            ]
         },
         "query": {
            "match": {
               "systemHeader.systemType": "template"
            }
         }
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 0.7768564,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "54eecc5b8014c225ec734259",
            "_score": 0.7768564,
            "_source": {
               "systemHeader": {
                  "summaryName": "New Big Dropdown - Template",
                  "keyID": [
                     "542dda8d6803fa98058b4568",
                     "543c99dd2dafa7c211b38546"
                  ],
                  "systemType": "template",
                  "summaryDescriptionRule": "This is a Case for works",
                  "summaryNameRule": "Case - {{CaseID}} {{Description}}",
                  "summaryDescription": "This is the template to demonstrate a big dropdown",
                  "date": "1970-01-01T00:00:00"
               }
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "54eecc5b8014c225ec734260",
            "_score": 0.7768564,
            "_source": {
               "systemHeader": {
                  "summaryName": "New Big Dropdown - Template",
                  "keyID": [],
                  "systemType": "template",
                  "summaryDescriptionRule": "This is a Case for works",
                  "summaryNameRule": "Case - {{CaseID}} {{Description}}",
                  "summaryDescription": "This is the template to demonstrate a big dropdown",
                  "date": "1970-01-01T00:00:00"
               }
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "54eecc5b8014c225ec734262",
            "_score": 0.7768564,
            "_source": {
               "systemHeader": {
                  "summaryName": "New Big Dropdown - Template",
                  "systemType": "template",
                  "summaryDescriptionRule": "This is a Case for works",
                  "summaryNameRule": "Case - {{CaseID}} {{Description}}",
                  "summaryDescription": "This is the template to demonstrate a big dropdown",
                  "date": "1970-01-01T00:00:00"
               }
            }
         }
      ]
   }
}

以下是我一起使用的代码:

http://sense.qbox.io/gist/120d1809768204fff2ce17134ffd81a766ed9056