与具有多个值的Elastic Search文本类型字段执行完全匹配

时间:2019-05-07 10:21:52

标签: elasticsearch

嗨,我在弹性搜索中有数以百万计的脚步,其中我的一个字段(textlowercase)的类型为“ text”。

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

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

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

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

我正在使用Elastic search 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"
            }
          }
        }]
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您可以使用match_phrase查询。

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

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