提问者:小点点

Elasticsearch在没有嵌套的多值字段中匹配每个单独值的查询


对于多值字段,如下所示:

PUT match_test
{
  "mappings": {
    "properties": {
      "companies": {
        "type": "text"
      }
    }
  }
}
POST match_test/_doc/1
{
  "companies": ["bank of canada", "japan games and movies", "microsoft canada"]
}

此查询返回我们在上面插入的文档:

GET match_test/_search
{
  "query": {
    "match": {
      "companies": {
        "query": "canada games",
        "operator": "and"
      }
    }
  }
}

有没有办法让弹性单独匹配列表中的每个项目?我想让文档匹配“银行”、“美国”、“银行”、“游戏”、“加拿大”,但不匹配“微软游戏”

我不想使用嵌套文档或脚本


共1个答案

匿名用户

如果你想找到相距很远但仍然在同一个数组索引上的单词,那么你可以使用position_increment_gap。

创建映射时,将字段的position_increment_gap设置为100。Elasticsearch将在每个位置自动索引数组数据,在下一个索引处的数据位置为100。然后用99编写一个match_phrase查询。

PUT match_test 
{
  "mappings": {
    "properties": {
      "companies": {
        "type": "text",
        "position_increment_gap": 100
      }
    }
  }
}

GET match_test/_search
    {
      "query": {
        "match_phrase": {
          "companies": {
            "query": "japan movies",
            "slop":99
          }
        }
      }
    }

在这里阅读更多信息https://www.elastic.co/guide/en/elasticsearch/reference/current/position-increment-gap.html