term filter/query: no word segmentation for the search text, just take it to the inverted index to match, what you input, just match what

Course outline


1. Search for posts based on user ID, whether to hide, post ID, post date


(1) Insert some test post data


POST /forum/article/_bulk
{ "index": { "_id": 1 }}
{ "articleID " : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 2 }}
{ "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" }
{ "index": { "_id": 3 }}
{ "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" }
{ "index": { " _id": 4 }}
{ "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" }


Initially, let's start with 4 fields, because the entire es supports the json document format, so the scalability and flexibility are very good. If more fields are to be added to the document as business requirements increase in the future, then we can easily add fields at any time. But if it is in a relational database, such as mysql, we have created a table, and now we need to add some columns to the table, it is very tricky, and we must use complex syntax to modify the table structure to execute. And there may be some impact on the system code.


GET /forum/_mapping/article


{
  "forum": {
    "mappings": {
      "article": {
        "properties": {
          "articleID": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },



          "postDate": {
            "type": "date"
          },
          "userID": {
            "type": "long"
          }
        }
      }
    }
  }
}


Now in es 5.2 version, type=text, two fields will be set by default, one is The field itself, such as articleID, is word segmentation; if there is another word, it is field.keyword, articleID.keyword, which is not word segmentation by default, and will retain up to 256 characters


(2) Search for posts according to user ID


GET /forum/article/_search
{
    "query" : {
        "constant_score" : { 
            "filter" : {
                "term" : { 
                    "userID" : 1
                }
            }
        }
    }
}


term filter/query: no word segmentation for the search text, just take it to the inverted index to match, what you input, match whatever.
For example , if the search text is segmented, "helle world" --> "hello" " and "world", the two words go to the inverted index to match the
term, "hello world" --> "hello world", go directly to the inverted index to match "hello world"


(3) Search for posts without hidden


GET /forum/article/_search
{
    "query" : {
        "constant_score" : { 
            "filter" : {
                "term" : { 
                    "hidden" : false
                }
            }
        }
    }
}


(4) Search for posts based on the posting date


GET /forum/article /_search
{
    "query" : {
        "constant_score" :  { 
            "filter" : {
                "term" : { 
                    "postDate" : "2017-01-01"
                }
            }
        }
    }
}


(5)根据帖子ID搜索帖子


GET /forum/article/_search
{
    "query" : {
        "constant_score" : { 
            "filter" : {
                "term" : { 
                    "articleID" : "XHDK-A-1293-#fJ3"
                }
            }
        }
    }
}


{
  "took": 1,
  "timed_out": false,
  "_shards":{
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}


GET /forum/article/_search
{
    "query" : {
        "constant_score" : { 
            "filter" : {
                "term" : { 
                    "articleID.keyword" : "XHDK-A-1293-#fJ3"
                }
            }
        }
    }
}


{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "forum",
        "_type": "article",
        "_id": "1",
        "_score": 1,
        "_source": {
          "articleID" : "XHDK-A-1293-#fJ3",
          "userID": 1,
          "hidden": false,
          "postDate": "2017-01-01"
        }
      }
    ]
  }
}


articleID.keyword, which is built into the latest version of es The field is wordless. Therefore, when an articleID comes over, it will create two indexes, one for itself, which is to be divided into words, and put into the inverted index after the word segmentation; the other is based on articleID. Strings are put into an inverted index.


So term filter, for text filtering, you can consider using the built-in field.keyword to match. But there is a problem, 256 characters are reserved by default. So try to manually build the index yourself and specify not_analyzed. In the latest version of es, you do not need to specify not_analyzed, just set type=keyword.


(6) View the word segmentation


GET /forum/_analyze
{
  "field": "articleID",
  "text": "XHDK-A-1293-#fJ3"
}


The default is the analyzed text type field. When creating an inverted index, All articleIDs will be word-segmented. After word-segmentation, the original articleID will be gone, and only the words after word-segmentation exist in the inverted index.
term, does not segment the search text, XHDK-A-1293-#fJ3 --> XHDK-A-1293-#fJ3; but when the articleID is indexed, XHDK-A-1293-#fJ3 --> xhdk, a , 1293, fj3


(7) rebuild index


DELETE /forum


PUT /forum
{
  "mappings": {
    "article": {
      "properties": {
        "articleID":


      }
    }
  }
}


POST /forum/article/_bulk
{ "index": { "_id": 1 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 2 }}
{ "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" }
{ "index": { "_id": 3 }}
{ "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 4 }}
{ "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" }     "query" : { { GET /forum/article/_search


(8) Re-search based on post ID and post date





        "constant_score" : { 
            "filter" : {
                "term" : { 
                    "articleID" : "XHDK-A-1293-#fJ3"
                }
            }
        }
    }
}


2. Sort out the knowledge points learned


(1) term filter: according to exact Search by value, numbers, boolean, date are naturally supported
(2) text needs to be specified as not_analyzed when indexing, in order to use term query
(3) equivalent to a single where condition in SQL


select *
from forum.article
where articleID='XHDK- A-1293-#fJ3'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325943975&siteId=291194637