我正在使用 Elasticsearch,需要根据数组第一个元素中字段的值来过滤文档。具体来说,我有一个名为 Tickers 的数组字段,我想检查该数组中第一个元素的名称字段是否与提供的列表中的任何值匹配。
以下是文档结构的示例:
{
"Tickers": [
{
"name": "AAPL",
"description": "description 0"
},
{
"name": "GOOG",
"description": "description 1"
}
]
}
我需要过滤 Tickers 数组中第一个元素的名称位于给定代码名称列表中的文档(例如,[“AAPL”、“MSFT”、“TSLA”])。
这是我的 Tickers 字段的 Elasticsearch 索引映射:
{
"Tickers":{
"properties":{
"name":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"description":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
}
}
}
}
我尝试过的
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"source": "doc['Tickers.name.keyword'].length > 0 && params.symbols.contains(doc['Tickers.name.keyword'].value)",
"params": {"symbols": ["TSLA"]}
}
}
}
]
}
}
}
您可以通过以下方式取消搜索查询脚本
使用限制过滤器将
name
字段的副本映射到新字段
PUT /first_ticker_searching
{
"settings": {
"analysis": {
"analyzer": {
"whitespace_limit_analyzer": {
"tokenizer": "whitespace",
"filter": [
"limit_filter"
]
}
},
"filter": {
"limit_filter": {
"type": "limit"
}
}
}
},
"mappings": {
"properties": {
"Tickers": {
"properties": {
"name": {
"type": "text",
"copy_to": "first_ticker",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"first_ticker": {
"type": "text",
"analyzer": "whitespace_limit_analyzer"
}
}
}
}
您和我的样本文件
PUT /first_ticker_searching/_bulk
{"create":{"_id":1}}
{"Tickers":[{"name":"AAPL","description":"description 0"},{"name":"GOOG","description":"description 1"}]}
{"create":{"_id":2}}
{"Tickers":[{"name":"APLA","description":"description 0"},{"name":"AAPL","description":"description 1"}]}
搜索
span_first
查询
GET /first_ticker_searching/_search?filter_path=hits.hits
{
"query": {
"bool": {
"filter": [
{
"span_first": {
"match": {
"span_term": {
"first_ticker": "AAPL"
}
},
"end": 1
}
}
]
}
}
}
回应。第一个文档在点击中,第二个文档不在点击中
{
"hits" : {
"hits" : [
{
"_index" : "first_ticker_searching",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.0,
"_source" : {
"Tickers" : [
{
"name" : "AAPL",
"description" : "description 0"
},
{
"name" : "GOOG",
"description" : "description 1"
}
]
}
}
]
}
}