Elasticsearch入門から入門シリーズ(四)---高度な使用を検索


1.esインデックスの別名使用
  • 開発中、業務需要の反復に伴い、これらの操作を行うと、業務に影響を与える可能性があり、ダウンタイムの調整などの問題もあります.これにより、esはこれらの問題を解決するためにインデックス別名を提供する.インデックス別名は、1つのショートカット方式またはソフト接続のように、1つまたは複数のインデックスを指すことができ、インデックス名を必要とする任意のAPIに適用することができます.別名の応用はプログラムに極めて大きい地霊活性
  • を提供した.
  • エイリアスクエリ
  • GET /nba/_alias
    
    GET /_alias
  • 別名
  • を追加
    POST /_aliases
    
    {
        "actions": [
            {
                "add": {
                    "index": "nba", 
                    "alias": "nba_v1.0"
                }
            }
        ]
    }
  • 別名を削除
  • POST /_aliases
    
    {
        "actions": [
            {
                "remove": {
                    "index": "nba", 
                    "alias": "nba_v1.0"
                }
            }
        ]
    }
    DELETE /nba/_alias/nba_v1.1
    
  • 名前変更
  • POST /_aliases
    
    {
        "actions": [
            {
                "remove": {
                    "index": "nba", 
                    "alias": "nba_v1.0"
                }
            }, 
            {
                "add": {
                    "index": "nba", 
                    "alias": "nba_v2.0"
                }
            }
        ]
    }
  • 複数のインデックスに1つのエイリアス
  • を指定
    POST /_aliases
    
    {
        "actions": [
            {
                "add": {
                    "index": "nba", 
                    "alias": "national_player"
                }
            }, 
            {
                "add": {
                    "index": "wnba", 
                    "alias": "national_player"
                }
            }
        ]
    }
  • 同じインデックスに複数の別名を指定する
  • POST /_aliases
    
    {
        "actions": [
            {
                "add": {
                    "index": "nba", 
                    "alias": "nba_v2.1"
                }
            }, 
            {
                "add": {
                    "index": "nba", 
                    "alias": "nba_v2.2"
                }
            }
        ]
    }
  • エイリアスリードインデックス
  •           ,       
    
    GET /nba_v2.1
    
    
              ,       
    
    
    GET /national_player
    
  • エイリアス書き込みインデックス
  •           ,        
    
    POST /nba_v2.1/_doc/566
    
    {
        "countryEn": "Croatia", 
        "teamName": "  ", 
        "birthDay": 858661200000, 
        "country": "    ", 
        "teamCityEn": "LA", 
        "code": "ivica_zubac", 
        "displayAffiliation": "Croatia", 
        "displayName": "         ", 
        "schoolType": "", 
        "teamConference": "  ", 
        "teamConferenceEn": "Western", 
        "weight": "108.9   ", 
        "teamCity": "   ", 
        "playYear": 3, 
        "jerseyNo": "40", 
        "teamNameEn": "Clippers", 
        "draft": 2016, 
        "displayNameEn": "Ivica Zubac", 
        "heightValue": 2.16, 
        "birthDayStr": "1997-03-18", 
        "position": "  ", 
        "age": 22, 
        "playerId": "1627826"
    }
  • エイリアスが複数のインデックスを指定する場合、インデックス
  • を書き込むように指定できます.
    POST /_aliases
    
    {
        "actions": [
            {
                "add": {
                    "index": "nba", 
                    "alias": "national_player", 
                    "is_write_index": true
                }
            }, 
            {
                "add": {
                    "index": "wnba", 
                    "alias": "national_player"
                }
            }
        ]
    }
    POST /national_player/_doc/566
    {
     "countryEn": "Croatia",
     "teamName": "  ",
     "birthDay": 858661200000,
     "country": "    ",
     "teamCityEn": "LA",
     "code": "ivica_zubac",
     "displayAffiliation": "Croatia",
     "displayName": "         ",
     "schoolType": "",
     "teamConference": "  ",
     "teamConferenceEn": "Western",
     "weight": "108.9   ",
     "teamCity": "   ",
     "playYear": 3,
     "jerseyNo": "40",
     "teamNameEn": "Clippers",
     "draft": 2016,
     "displayNameEn": "Ivica Zubac",
     "heightValue": 2.16,
     "birthDayStr": "1997-03-18",
     "position": "  ",
     "age": 22,
     "playerId": "1627826"
    }

    2.esのインデックスの再構築方法
  • ステップ
  • nba別名nbaを1つ取るlatest, nba_latestは対外用途として
  • 新規インデックスnba_2022010年1月、構造はnbaインデックスにコピーされ、業務要求に従ってフィールド
  • を修正する.
  • nbaデータをnba_に同期20220101
  • nba_へ20220101別名nbaを追加_latest,nba別名nba_を削除するlatest
  • nbaインデックス
  • を削除
  • ケース
  • 1.        nba       nba_latest  
    
    2.      (        ,jerseyNo  keyword  )
    
    PUT /nba_20220101
    
    {
        "mappings": {
            "properties": {
                "age": {
                    "type": "integer"
                }, 
                "birthDay": {
                    "type": "date"
                }, 
                "birthDayStr": {
                    "type": "keyword"
                }, 
                "code": {
                    "type": "text"
                }, 
                "country": {
                    "type": "keyword"
                }, 
                "countryEn": {
                    "type": "keyword"
                }, 
                "displayAffiliation": {
                    "type": "text"
                }, 
                "displayName": {
                    "type": "text"
                }, 
                "displayNameEn": {
                    "type": "text"
                }, 
                "draft": {
                    "type": "long"
                }, 
                "heightValue": {
                    "type": "float"
                }, 
                "jerseyNo": {
                    "type": "keyword"
                }, 
                "playYear": {
                    "type": "long"
                }, 
                "playerId": {
                    "type": "keyword"
                }, 
                "position": {
                    "type": "text"
                }, 
                "schoolType": {
                    "type": "text"
                }, 
                "teamCity": {
                    "type": "text"
                }, 
                "teamCityEn": {
                    "type": "text"
                }, 
                "teamConference": {
                    "type": "keyword"
                }, 
                "teamConferenceEn": {
                    "type": "keyword"
                }, 
                "teamName": {
                    "type": "keyword"
                }, 
                "teamNameEn": {
                    "type": "keyword"
                }, 
                "weight": {
                    "type": "text"
                }
            }
        }
    }
    
    3.      copy    
        3.1     ,      reindex      
        
        POST /_reindex
        {
             "source": {
                 "index": "nba"
             },
             "dest": {
                 "index": "nba_20220101"
             }
        }
    
        3.2    ,   reindex     ,     wait_for_completion=false      ,
       reindex       taskId
    
        POST /_reindex?wait_for_completion=false
    
        {
             "source": {
                 "index": "nba"
             },
             "dest": {
                 "index": "nba_20220101"
             }
        }
    
    4.    
    
    POST /_aliases
    
    {
        "actions": [
            {
                "add": {
                    "index": "nba_20220101", 
                    "alias": "nba_latest"
                }
            }, 
            {
                "remove": {
                    "index": "nba", 
                    "alias": "nba_latest"
                }
            }
        ]
    }
    
    5.     
    
    DELETE /nba
    
    6.         
    
    POST /nba_latest/_search
    
    {
        "query": {
            "match": {
                "displayNameEn": "james"
            }
        }
    }
    

    3.esのrefresh操作
  • 理想的な検索
  • 新しいデータはインデックスに1つずつ追加すれば検索できますが、実際の状況はそうではありません.

  • アプリケーションチェーン命令要求を使用して、まず1つのドキュメントを追加し、
  • を作成します.
    curl -X PUT localhost:9200/star/_doc/888 -H 'Content-Type:
    application/json' -d '{ "displayName": "   " }'
    curl -X GET localhost:9200/star/_doc/_search?pretty
  • 強制リフレッシュ
  • curl -X PUT localhost:9200/star/_doc/666?refresh -H 'Content-Type:
    application/json' -d '{ "displayName": "   " }'
    curl -X GET localhost:9200/star/_doc/_search?pretty
  • デフォルト更新時間の変更(デフォルトは1 s)
  • PUT /star/_settings
    {
     "index": {
     "refresh_interval": "5s"
     }
    }
  • refreshを
  • 閉じる
    PUT /star/_settings
    {
     "index": {
     "refresh_interval": "-1"
     }
    }

    4.esのハイライトクエリー
  • Highクエリ
  • POST /nba_latest/_search
    
    {
        "query": {
            "match": {
                "displayNameEn": "james"
            }
        }, 
        "highlight": {
            "fields": {
                "displayNameEn": { }
            }
        }
    }
  • ⾃定義Highクエリ
  • POST /nba_latest/_search
    
    {
        "query": {
            "match": {
                "displayNameEn": "james"
            }
        }, 
        "highlight": {
            "fields": {
                "displayNameEn": {
                    "pre_tags": [
                        "

    " ], "post_tags": [ "

    " ] } } } }

    5.esクエリーの推奨事項
  • クエリーの推奨事項は、アプリケーションにより良い検索体験を提供するためです.含む:語句検査、
  • Term suggester
  • termエントリ推奨器は、
  • POST /nba_latest/_search
    
    {
        "suggest": {
            "my-suggestion": {
                "text": "jamse hardne", 
                "term": {
                    "suggest_mode": "missing", 
                    "field": "displayNameEn"
                }
            }
        }
    }
  • Phrase suggester
  • phraseフレーズは、termに基づいて、インデックスの元の
  • POST /nba_latest/_search
    
    {
        "suggest": {
            "my-suggestion": {
                "text": "jamse harden", 
                "phrase": {
                    "field": "displayNameEn"
                }
            }
        }
    }
  • Completion suggester
  • Completion完了推奨
  • POST /nba_latest/_search
    
    {
        "suggest": {
            "my-suggestion": {
                "text": "Miam", 
                "completion": {
                    "field": "teamCityEn"
                }
            }
        }
    }