【问题标题】:Elastic search filter not working for multi value json弹性搜索过滤器不适用于多值 json
【发布时间】:2021-06-03 11:09:44
【问题描述】:

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

"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

身体是

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

我总是得到 0 次点击。查询多值 json 是否有更好的价值。

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-query


    【解决方案1】:

    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 子句和term 查询来获取所有userId 不等于testuser@demo.com 的记录

    {
      "query": {
        "bool": {
          "must_not": {
            "term": {
              "userId.userId.keyword": "testuser@demo.com"
            }
          }
        }
      }
    }
    

    【讨论】:

    • 这是有效的。谢谢 。有没有办法得到相反的结果。获取所有 userId 不等于 testuser@demo.com 的记录
    • @slaveCoder 是的,您可以使用must_not 子句来做到这一点。请浏览答案的更新部分
    【解决方案2】:

    术语查询返回在提供的字段中包含一个或多个确切术语的文档。术语查询与术语查询相同,但您可以搜索多个值。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多