java 弹性数组,通过弹性搜索中的数组元素匹配

I have to construct quite a non-trivial (as it seems to be now) query in Elasticsearch.

Suppose I have a couple of entities, each with an array element, consisting of strings:

1). ['A', 'B']

2). ['A', 'C']

3). ['A', 'E']

4). ['A']

Mappings for array element is as follows (using dynamic templates):

{

"my_array_of_strings": {

"path_match": "stringArray*",

"mapping": {

"type": "string",

"index": "not_analyzed"

}

}

}

Json representation of entity looks like this:

{

"stringArray": [

"A",

"B"

]

}

Then I have user input:

['A', 'B', 'C'].

What I want to achieve is to find entities which contain only elements specified in input - expected results are:

['A', 'B'], ['A', 'C'], ['A'] but NOT ['A', 'E'] (because 'E' is not present in user input).

Can this scenario be implemented with Elasticsearch?

UPDATE:

Apart from the solution with using the scripts - which should work nicely, but will most likely slow down the query considerably in case when there are many records that match - I have devised another one. Below I will try to explain its main idea, without code implementation.

One considerable condition that I failed to mention (and which might have given other users valuable hint) is that arrays consist of enumerated elements, i.e. there are finite number of such elements in array. This allows to flatten such array into separate field of an entity.

Lets say there are 5 possible values: 'A', 'B', 'C', 'D', 'E'. Each of these values is a boolean field - true if it is empty (i.e. array version would contain this element ) and false otherwise.

Then each of the entities could be rewritten as follows:

1).

A = true

B = true

C = false

D = false

E = false

2).

A = true

B = false

C = true

D = false

E = false

3).

A = true

B = false

C = false

D = false

E = true

4).

A = true

B = false

C = false

D = false

E = false

With the user input of ['A', 'B', 'C'] all I would need to do is:

a) take all possible values (['A', 'B', 'C', 'D', 'E']) and subtract from them user input -> result will be ['D', 'E'];

b) find records where each of resulting elements is false, i.e. 'D = false AND E = false'.

This would give records 1, 2 and 4, as expected. I am still experimenting with the code implementation of this approach, but so far it looks quite promising. It has yet to be tested, but I think this might perform faster, and be less resource demanding, than using scripts in query.

To optimize this a little bit further, it might be possible not to provide fields which will be 'false' at all, and modify the previous query to 'D = not exists AND E = not exists' - result should be the same.

解决方案

You can achieve this with scripting, This is how it looks

{

"query": {

"filtered": {

"filter": {

"bool": {

"must": [

{

"terms": {

"name": [

"A",

"B",

"C"

]

}

},

{

"script": {

"script": "if(user_input.containsAll(doc['name'].values)){return true;}",

"params": {

"user_input": [

"A",

"B",

"C"

]

}

}

}

]

}

}

}

}

}

This groovy script is checking if the list contains anything apart from ['A', 'B', 'C'] and returns false if it does, so it wont return ['A', 'E']. It is simply checking for sublist match. This script might take couple of seconds. You would need to enable dynamic scripting, also syntax might be different for ES 2.x, let me know if it does not work.

EDIT 1

I have put both conditions inside filter only. First only those documents that have either A, B or C are returned, and then script is applied on only those documents, so this would be faster than the previous one. More on filter ordering

Hope this helps!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值