【问题标题】:Perform exact match with Elastic Search text type field with multiple value与具有多个值的 Elastic Search 文本类型字段执行精确匹配
【发布时间】:2019-05-07 10:21:51
【问题描述】:

您好,我在弹性搜索中有数百万条记录,其中我的一个字段(textlowercase)属于“文本”类型。

现在我想在这个“文本”类型字段中搜索多个单词,我该怎么做。

问题在于,由于它是一个文本字段,因此会对其进行分析并拆分为标记。 例如:在 SQL 中我想要这样的东西

select textlowercase from table where textlowercase like '%abc%' or '%bbc%' or '%my text%'

我尝试过“未分析”并将类型更改为“关键字”并没有帮助。

我正在使用弹性搜索 7

这是我的映射:

{
  "settings": {
    "analysis": {
      "normalizer": {
        "lowercase_normalizer": {
          "type": "custom",
          "char_filter": [

          ],
          "filter": [
            "lowercase"
          ]
        }
      },
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "fbdata": {
    "mappings": {
      "properties": {
        "createdatutc": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "createdbyname": {
          "type": "keyword"
        },
        "groupname": {
          "type": "keyword"
        },
        "id": {
          "type": "keyword"
        },
        "insertedatutc": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "postid": {
          "type": "keyword"
        },
        "posttype": {
          "type": "keyword"
        },
        "posturl": {
          "type": "keyword"
        },
        "textlowercase": {
          "type": "text",
          "analyzer": "my_analyzer",
          "fielddata": true
        }
      }
    }
  }
}

这是我的查询

{
  "index": "fbdata",
  "type": "_doc",
  "body": {
    "from": 0,
    "size": 500000,
    "query": {
      "bool": {
        "should": [ {
          "match": {
            "textlowercase": "*cowmilk*"
          }
        }, {
          "match": {
            "textlowercase": "*Gaay ka doodh*"
          }
        }, {
          "match": {
            "textlowercase": "*cow ka*"
          }
        }, {
          "match": {
            "textlowercase": "*bakri ka*"
          }
        }, {
          "match": {
            "textlowercase": "*goatmilk*"
          }
        }],
        "must": [{
          "range": {
            "createdatutc": {
              "gte": "2019-01-01",
              "lt": "2019-03-31",
              "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
            }
          }
        }]
      }
    }
  }
}

【问题讨论】:

  • @VikashKumarVerma 请将此作为答案发布,以便我接受

标签: elasticsearch


【解决方案1】:

您可以使用 match_phrase 查询。

{
  "query": {
    "match_phrase": {
      "FIELD": "PHRASE"
    }
  }
}

查看更多详情 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html

【讨论】:

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