Elasticsearch simple learning 12: full text retrieval (List or array) of multivalued fields

Keywords: Programming

1, Description

In practical application, our code may be a list < string >, which needs to be mapped to the index of elastic search.

Many times, different students deal with different methods, some are comma separated, some are using arrays.

Which method is more reasonable?! Let's compare.

2, Example

1. Index

##Add an index
PUT test_beehive_han_v1
{
  "settings": {

        "number_of_shards": "5",
        "number_of_replicas": "1"

    }
}

##Adding an index mapping
PUT test_beehive_han_v1/test_han/_mapping 
{
	"test_han" : {
        "properties": {
          "contentTag": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              }
            },
            "analyzer": "ik_smart"
          },
          "userTag": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              }
            },
            "analyzer": "ik_smart"
          },
          "contentTagNotList": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              }
            },
            "analyzer": "ik_smart"
          },
          "userTagNotList": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              }
            },
            "analyzer": "ik_smart"
          }
        }
	}
}

##View mapping
GET test_beehive_han_v1/_mapping

2. Insert test data

##Insert data:
POST test_beehive_han_v1/test_han/1
{
	"contentTagNotList":"Square dance 2014,square dance,Square dance,The latest square dance,Square dance teaching",
	"userTagNotList":"alien,Ronaldo,Messi,C Luo,Real Madrid,Barcelona,Da Luo,Ronaldinho,Neymar,Kaka",
	"contentTag": ["Square dance 2014","square dance","Square dance","The latest square dance","Square dance teaching"],
	"userTag":["alien","Ronaldo","Messi","C Luo","Real Madrid","Barcelona","Da Luo","Ronaldinho","Neymar","Kaka"]
}

POST test_beehive_han_v1/test_han/2
{
	"contentTagNotList":"Square dance 2014,square dance",
	"userTagNotList":"alien,Ronaldo,Messi",
	"contentTag": ["Square dance 2014","square dance"],
	"userTag":["alien","Ronaldo","As long as it contains"]
}

##View data
GET test_beehive_han_v1/_search

3. Query

##Query - exact query of List, for example: those that need to contain these values at the same time should be queried
GET test_beehive_han_v1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "userTag.raw": {
              "value": "alien"
            }
          }
        },{
          "term": {
            "userTag.raw": {
              "value": "Ronaldo"
            }
          }
        },{
          "term": {
            "userTag.raw": {
              "value": "Messi"
            }
          }
        }
      ]
    }
  }
}

##Exact query of non List
GET test_beehive_han_v1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "userTagNotList.raw": {
              "value": "alien,Ronaldo,Messi"
            }
          }
        }
      ]
    }
  }
}

##Full text search of List / / "query": "West", Messi, Ronaldo
GET test_beehive_han_v1/_search
{
  "profile": "true",
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "as long as", 
            "fields": [
              "userTag"
            ]
          }
        }
      ]
    }
  }
}

## Non List full-text search "query": "Ronaldo"
GET test_beehive_han_v1/_search
{
  "profile": "true",
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "Real Madrid",
            "fields": [
              "userTagNotList"
            ]
          }
        }
      ]
    }
  }
}

4. Check word segmentation

##View word segmentation
POST test_beehive_han_v1/_analyze
{
  "field": "userTag",
  "text" : "alien,Ronaldo,Messi"
}

5. Delete index

##Delete index
DELETE /test_beehive_han_v1

Posted by drdapoo on Tue, 21 Apr 2020 09:20:07 -0700