【问题标题】:Multi term search not giving results多词搜索不给出结果
【发布时间】:2021-01-01 22:23:25
【问题描述】:

使用 elasticsearch 7.1.0 我有这样的对象:

{
... other fields
"entityId": "abcdef",
"anotherParameter": "something@weird-here"
}

我需要对 entityId 和 anotherParameter 进行完全匹配(大小写可以忽略)。 我通过 C# 中的 NEST 库使用此查询

GET objects/_search
{
    "from": 0,
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "entityId": {
                                        "value": "abcdef"
                                    }
                                }
                            },
                            {
                                "term": {
                                    "anotherParameter": {
                                        "value": "something@weird-here"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

不返回任何内容。 这两个字段的映射是:

"type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }

如果我省略 @ 而只是在 anotherParameter 字段中输入“某物”,我会得到正确的对象,但也会得到我不想返回的东西。 我正在寻找有关如何进行精确搜索的说明。有人吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    如果我省略 @ 而只是在 anotherParameter 字段,我得到了正确的对象

    standard analyzer 是默认分析器,如果未指定则使用该分析器。您正在获取带有something 的文档,因为anotherParameter 的文本已被分析,并生成了以下标记。

    {
      "tokens": [
        {
          "token": "something",
          "start_offset": 0,
          "end_offset": 9,
          "type": "<ALPHANUM>",
          "position": 0
        },
        {
          "token": "weird",
          "start_offset": 10,
          "end_offset": 15,
          "type": "<ALPHANUM>",
          "position": 1
        },
        {
          "token": "here",
          "start_offset": 16,
          "end_offset": 20,
          "type": "<ALPHANUM>",
          "position": 2
        }
      ]
    }
    

    所以要获得完全匹配,您可以将字段的数据类型更改为keyword 类型

    {
      "mappings": {
        "properties": {
          "entityId": {
            "type": "keyword"
          },
          "anotherParameter": {
            "type": "keyword"
          }
        }
      }
    }
    

    或者,如果您没有明确定义任何映射,那么您也可以像这样修改您的搜索查询。您需要将 .keyword 添加到这两个字段。这使用keyword 分析器而不是standard 分析器(注意字段后的“.keyword”)。试试下面的查询 - :

    {
      "from": 0,
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "entityId.keyword": {
                        "value": "abcdef"
                      }
                    }
                  },
                  {
                    "term": {
                      "anotherParameter.keyword": {
                        "value": "something@weird-here"
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    通过使用上述两种方法,您将能够进行完全匹配,但搜索也会区分大小写。

    为不区分大小写的搜索添加一个完全匹配的工作示例

    索引映射:

    {
      "settings": {
        "analysis": {
          "analyzer": {
            "my_analyzer": {
              "type": "custom",
              "filter": [
                "lowercase"
              ],
              "tokenizer": "keyword"
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "entityId": {
            "type": "text",
            "analyzer": "my_analyzer"
          },
          "anotherParameter": {
            "type": "text",
            "analyzer": "my_analyzer"
          }
        }
      }
    }
    

    搜索 API:

    {
      "from": 0,
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "entityId": "Abcdef"
                    }
                  },
                  {
                    "match": {
                      "anotherParameter": "something@weird-here"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "65534220",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.0,
            "_source": {
              "entityId": "abcdef",
              "anotherParameter": "something@weird-here"
            }
          }
        ]
    

    【讨论】:

    • 成功了!感谢您添加不区分大小写的示例 - 我实际上也需要它:)
    • @ThomasVestergaard 很高兴这对您有用 :) 感谢您接受答案,请您也为答案投票 :)
    猜你喜欢
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 2012-02-18
    • 2021-02-14
    • 2023-03-12
    • 2011-02-14
    相关资源
    最近更新 更多