小编典典

加入elasticsearch索引,同时匹配嵌套/内部对象中的字段

elasticsearch

我正在尝试通过使用术语过滤器查找来加入2个Elasticsearch索引。我提到了http://www.elasticsearch.org/blog/terms-
filter-lookup/和http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-
dsl-terms-filter。
html。这些示例在诸如“ followers”:[“ 1”,“
3”]之类的字段数组上查找,并且join对于类似数据也可以正常工作。

我的要求是在对象数组中加入一个字段。当我将上述示例扩展为包括对象数组时,我的查询失败。以下是样本数据:

PUT /users/user/2 {
   "followers" : [
  {
    "userId":"1",
    "username":"abc",
    "location":"xyz"
   },
   {
    "userId":"3",
    "username":"def",
    "location":"xyz"
   }
}
]
}

PUT /tweets/tweet/1 {
   "user" : "2"
}

PUT /tweets/tweet/2 {
   "user" : "1"
}

我现在正在尝试查找由用户2的追随者创建的推文

POST /tweets/_search {
  "query" : {
"filtered" : {
  "filter" : {
    "terms" : {
      "user" : {
        "index" : "users",
        "type" : "user",
        "id" : "2",
        "path" : "followers.userId"
      },
      "_cache_key" : "user_2_friends"
    }
  }
}
  }
}

我的搜索结果为0的上述查询。我也尝试了2种其他方法:1)在映射过程中将跟随者对象声明为嵌套对象,并在查询中使用“嵌套”;
2)尝试在将路径指定为“跟随者”之后为followers.userId添加匹配查询。没有产生结果。

术语过滤查询是否支持对象数组?解决我的问题的任何指示将大有帮助


阅读 590

收藏
2020-06-22

共1个答案

小编典典

清除索引(如果有)

curl -XDELETE "http://example.com:9200/currencylookup/"

curl -XDELETE "http://example.com:9200/currency/"

创建查找表

curl -XPUT http://example.com:9200/currencylookup/type/2 -d '
{ "conv" : [ 
{  "currency":"usd","username":"abc", "location":"USA" }, 
{  "currency":"inr", "username":"def", "location":"India" },
{  "currency":"IDR", "username":"def", "location":"Indonesia" }]
}'

让我们放一些虚拟文档

curl -XPUT "http://example.com:9200/currency/type/USA" -d '{ "amount":"100", "currency":"usd", "location":"USA" }'

curl -XPUT "http://example.com:9200/currency/type/JPY" -d '{ "amount":"50", "currency":"JPY", "location":"JAPAN" }'

curl -XPUT "http://example.com:9200/currency/type/INR" -d '{ "amount":"50", "currency":"inr", "location":"INDIA" }'

curl -XPUT "http://example.com:9200/currency/type/IDR" -d '{ "amount":"30", "currency" : "IDR", "location": "Indonesia" }'

是时候检查输出了

curl http://example.com:9200/currency/_search?pretty -d '{
   "query" : {
 "filtered" : {
   "filter" : {
     "terms" : {
       "currency" : {
         "index" : "currencylookup",
         "type" : "type",
         "id" : "2",
         "path" : "conv.currency"
       },
       "_cache_key" : "currencyexchange"
     }
   }
 }
   }
 }'

结果

# curl http://example.com:9200/currency/_search?pretty -d '{
   "query" : {
 "filtered" : {
   "filter" : {
     "terms" : {
       "currency" : {
         "index" : "currencylookup",
         "type" : "type",
         "id" : "2",
         "path" : "conv.currency"
       },
       "_cache_key" : "currencyexchange"
     }
   }
 }
   }
 }'
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "currency",
      "_type" : "type",
      "_id" : "INR",
      "_score" : 1.0,
      "_source":{ "amount":"50", "currency":"inr", "location":"INDIA" }
    }, {
      "_index" : "currency",
      "_type" : "type",
      "_id" : "USA",
      "_score" : 1.0,
      "_source":{ "amount":"100", "currency":"usd", "location":"USA" }
    } ]
  }
}

结论

大写字母是罪魁祸首。

您会看到 ‘IDR’ 是大写字母,因此匹配失败,并且即使在 ‘JPY’中 也没有查找,因为它是大写字母。

交叉匹配值必须为小写字母或数字,例如

例如:

  • abc
  • 1abc
2020-06-22