elastic search mapping对match query及term query的影响

elastic search的mapping是非常重要的,mapping就是index的type的元数据,每个type都有一个自己的mapping,决定了数据类型、建立倒排索引的行为以及进行搜索的行为。

举个例子来说:

mapping的操作

新建一个mapping

PUT /test-website
{
  "mappings": {
    "test-article": {
      "properties": {
        "author_id": {
          "type": "long"
        },
        "title": {
          "type": "text",
          "analyzer": "english"
        },
        "content": {
          "type": "text"
        },
        "post_date": {
          "type": "date"
        },
        "publisher_id":{
          "type": "text",
          "index": false
        }
      }
    }
  }
}

其中"index":false 说明此字段不参与搜索

默认"type":"text"是使用standard分词器的

注意:只能在创建index时手动建立mapping,或者新增field mapping,但是不能update field mapping

现在新增一个fieldmapping,raw-keyword,"type":"keyword"意味着不分词,为exact value字段

PUT /test-website/_mapping/test-article
{
      "properties": {
        "raw-keyword": {
          "type": "keyword"
        }
      }
}

现在查看一下mapping的情况

GET test-website/_mapping/test-article

得到如下结果

{
  "test-website": {
    "mappings": {
      "test-article": {
        "properties": {
          "author_id": {
            "type": "long"
          },
          "content": {
            "type": "text"
          },
          "post_date": {
            "type": "date"
          },
          "publisher_id": {
            "type": "text",
            "index": false
          },
          "raw-keyword": {
            "type": "keyword"
          },
          "title": {
            "type": "text",
            "analyzer": "english"
          }
        }
      }
    }
  }
}

接下来新增两条数据

PUT /test-website/test-article/2
{
  "post_date": "2017-01-02",
  "title": "my second article",
  "content": "this is my second article in this website",
  "author_id": 11400,
    "raw-keyword": "hello",
  "publisher_id":"bbb"
}
PUT /test-website/test-article/3
{
  "post_date": "2017-01-03",
  "title": "my third article",
  "content": "this is my third article in this website",
  "author_id": 11400,
  "raw-keyword": "hello abc",
  "publisher_id":"aaa"
}

进行match搜索

除raw-keyword及publisher_id字段外,其他都可以通过分词进行match搜索

进行term搜索

对raw-keyword字段可以进行term搜索,可以看到是exact value匹配的

GET test-website/test-article/_search
{
  "query": {
    "term": {
      "raw-keyword": {
        "value": "hello"
      }
    }
  }
}

可以看到搜索hello 并没有将hello abc搜索出来

对no index字段进行搜索

GET test-website/test-article/_search
{
  "query": {
    "term": {
      "raw-keyword": {
        "value": "hello abc"
      }
    }
  }
}

GET test-website/test-article/_search
{
  "query": {
    "match": {
      "publisher_id": "aaa"
    }
  }
}

结果会报错

Cannot search on field [publisher_id] since it is not indexed.

通过以上在kibana上的尝试,可以看到mapping对搜索的影响

猜你喜欢

转载自blog.csdn.net/u013905744/article/details/80851788