Elasticsearch Search/filter 按数组中的出现次数或顺序

Elasticsearch Search/filter by occurrence or order in an array

我的索引中有一个 data 字段,其中

I want only doc 2 as result i.e logically where b comes before a in the array field data.

文档 1:

data = ['a','b','t','k','p']

文档 2:

data = ['p','b','i','o','a']

目前,我正在尝试在 [a,b] 上使用术语 must,然后在另一个代码片段中检查顺序。 请提出更好的解决方法。

我的理解是,唯一的方法是使用 Span Queries,但它不适用于值数组。

您需要将这些值连接到单个 text 字段中,并以 whitespace 作为分隔符,重新获取文档并在该字段上使用 Span Near 查询:

请找到以下映射、示例文档、查询和响应:

映射:

PUT my_test_index
{
  "mappings": {
    "properties": {
      "data":{
        "type": "text"
      }
    }
  }
}

示例文档:

POST my_test_index/_doc/1
{
  "data": "a b"
}

POST my_test_index/_doc/2
{
  "data": "b a"
}

跨度查询:

POST my_test_index/_search
{
    "query": {
        "span_near" : {
            "clauses" : [
                { "span_term" : { "data" : "a" } },
                { "span_term" : { "data" : "b" } }
            ],
            "slop" : 0,                  <--- This means only `a b` would return but `a c b` won't. 
            "in_order" : true            <--- This means a should come first and the b
        }
    }
}

注意 slop controls the maximum number of intervening unmatched positions permitted.

回复:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.36464313,
    "hits" : [
      {
        "_index" : "my_test_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.36464313,
        "_source" : {
          "data" : "a b"
        }
      }
    ]
  }
}

如果有帮助请告诉我!