弹性搜索过滤器不适用于多值 json

Elastic search filter not working for multi value json

您好,我的弹性搜索索引的映射为。

"userId": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "userId": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "userName": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }

我的搜索查询也是这样的 获取:http://localhost:5000/questions/_search

Body 是

{
  "query": {
    "bool": {
      "filter": [
        { "term": { "userId.userId": "testuser@demo.com"
        }}
   
      ]
    }
  }
}

我总是得到 0 次点击。查询multivalue json.

有没有更好的值

userId.userId 字段是 text 类型。如果没有定义分析器,elasticsearch 默认使用 standard analyzer。这会将 testuser@demo.com 标记为

{
  "tokens": [
    {
      "token": "testuser",
      "start_offset": 0,
      "end_offset": 8,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "demo.com",
      "start_offset": 9,
      "end_offset": 17,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}

您需要在 userId.userId 字段上使用 "userId.userId.keyword" 字段。这使用关键字分析器而不是标准分析器(注意 userId.userId 字段后的“.keyword”)。

您获得 0 次匹配,因为 term query 始终搜索完全匹配的字词。当您使用标准分析器(默认分析器)进行搜索时,您将不会得到正确的结果

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "userId.userId.keyword": "testuser@demo.com"
          }
        }
      ]
    }
  }
}

如果要搜索多个字段,请使用 terms query

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "userId.userId.keyword": [
              "testuser@demo.com",
              "abc.com"
            ]
          }
        }
      ]
    }
  }
}

更新 1:

您可以使用 must_not 子句和术语查询来获取 userId 不等于 testuser@demo.com

的所有记录
{
  "query": {
    "bool": {
      "must_not": {
        "term": {
          "userId.userId.keyword": "testuser@demo.com"
        }
      }
    }
  }
}

术语查询 returns 在提供的 field.The 术语查询中包含一个或多个确切术语的文档与术语查询相同,只是您可以搜索多个值。

{
  "query": {
    "terms": {
      "userId.userId": [ "testuser@demo.com", "other@demo.com" ],
      "boost": 1.0
    }
  }
}