---
# System prepended metadata

title: Solr Survey
tags: [HH]

---

# Solr Survey
###### tags: `HH`

**Solr admin web ui**

1. 測試機:
http://10.57.232.105:8983/solr/
    - spc query
    http://10.57.232.105:8983/solr/#/spc/query

2. 正式機:
http://10.160.29.105:8983/solr/
    - spc query
    http://10.160.29.105:8983/solr/#/spc/query

**Common query parameter:**
1. q(query) -> 查詢條件
   ```
   q=*:*
   q=key:value AND key:value
   q=key:value OR key:value
   #找出SPC_0>=173388
   q=SPC_0:[173388 TO *]
   #找出timestamp在2019-08-09 10:30:00 到 2019-08-09 12:30:00
   q=timestamp:[1565317800 TO 1565325000]
   #找出timestamp_iso在2019-08-09 10:30:00 到 2019-08-09 12:30:00
   timestamp_iso的時區->Asia/Taipei timezone:
   q=timestamp_iso:[2019-08-09T10:30:00Z TO 2019-08-09T12:30:00Z]
   ````
2. fq(filter query) -> 將query查回來的資料放入cache, 再進行複雜的條件過濾
   ```
   fq=key:value
   fq=key:value or key:value
   ````
3. sort -> 排序
   ```
   sort=spc_0 asc
   sort=spc_0 asc, machine_id desc
   ````
4. start -> 從第n筆開始, default是從第1筆開始
   ```
   start=11
   ````
5. rows -> 撈出的筆數上限, default是取得10筆資料
   ```
   rows=2880
   ````
6. fl(filter list) -> 搜尋特定欄位, default是全部欄位都搜尋
   ```
   fl=spc_0,timestamp
   ````
# Query data api

**目前有5個Core:**
1. spc
[http://10.57.232.105:8983/solr/#/spc/query](http://10.57.232.105:8983/solr/#/spc/query)

2. machinestatus
[http://10.57.232.105:8983/solr/#/machinestatus/query](http://10.57.232.105:8983/solr/#/machinestatus/query)

3. historyrecord
[http://10.57.232.105:8983/solr/#/historyrecord/query](http://10.57.232.105:8983/solr/#/historyrecord/query)

4. alarmrecord
[http://10.57.232.105:8983/solr/#/machinestatus/query](http://10.57.232.105:8983/solr/#/machinestatus/query)

5. dc_event
[http://10.57.232.105:8983/solr/#/dc_event/query](http://10.57.232.105:8983/solr/#/dc_event/query)



              
**Example:**
- 查詢2019-08-09 10:30:00 到 2019-08-09 12:30:00, 機台A03的模次資料

    **1. spc**
    query data api對照
    ```
    example輸入參數:

    {"sdate": "2019-08-09 10:30:00","edate": "2019-08-09 12:30:00","no": "A03","type": "_Spc"}
    ````
    solr api
    ```
    1.timestamp
    http://10.160.29.105:8983/solr/spc/select?q=machine_id:A03 AND timestamp:[1565317800 TO 1565325000]&sort=SPC_0 asc&rows=272
    2.timestamp_iso
    http://10.160.29.105:8983/solr/spc/select?q=machine_id:A03 AND timestamp_iso:[2019-08-09T10:30:00Z TO 2019-08-09T12:30:00Z]&sort=SPC_0 asc&rows=272
    ````
    **2. machinestatus**
    ```
    1.timestamp
    http://10.160.29.105:8983/solr/machinestatus/select?q=timestamp:[1565317800 TO 1565325000] AND machine_id:A03&sort=spc asc&rows=5760
    2.timestamp_iso
    http://10.160.29.105:8983/solr/machinestatus/select?q=timestamp_iso:[2019-08-09T10:30:00Z TO 2019-08-09T12:30:00Z] AND machine_id:A03&sort=spc asc&rows=5760
    ```
    **3. historyrecord**
    ```
    1.timestamp
    http://10.160.29.105:8983/solr/historyrecord/select?q=timestamp:[1565317800 TO 1565325000] AND machine_id:A03&rows=5760
    2.timestamp_iso
    http://10.160.29.105:8983/solr/historyrecord/select?q=timestamp_iso:[2019-08-09T10:30:00Z TO 2019-08-09T12:30:00Z] AND machine_id:A03&rows=5760
    ````
    **4. alarmrecord**
    ```
    1.timestamp
    http://10.160.29.105:8983/solr/alarmrecord/select?q=timestamp:[1565317800 TO 1565325000] AND machine_id:A03&rows=5760
    2.timestamp_iso
    http://10.160.29.105:8983/solr/alarmrecord/select?q=timestamp_iso:[2019-08-09T10:30:00Z TO 2019-08-09T12:30:00Z] AND machine_id:A03&rows=5760
    ````


# Insert data to Solr

1. create solr core:
    http://10.160.29.112:8983/solr/#/~cores/alarmrecord
    - copy core資料夾
      $ cd /opt/solr/server/solr/
      $ cp -r ${old_core_name} ${new_core_name}
    - 刪除 core.properties
      $ rm -rf core.properties
    - 修改 managed-schema
      $ vim managed-schema
      1. 調整 schema name="${new_core_name}"
      2. 新增field, 從id field開始調整
        ![](https://i.imgur.com/LpuutoF.jpg)
        一定要設定uniquekey <uniqueKey>id</uniqueKey>
         
      3. 回到solr admin web, 按下add core button
         http://10.160.29.112:8983/solr/#/~cores/alarmrecord
        ![](https://i.imgur.com/3AMpMDo.jpg) 
    
2. insert data to solr:
    - install pysolr package
    - python sample code:
http://10.57.232.171:8888/ap5d2/data-mgmt-api/blob/master/app/addSolrData.py
    ```
    import pysolr

    from settings import SOLR_PORT, ENV, SERVER_IP

    def addOEEData(coreName, data_collector):
        # SERVER_IP
        host = SERVER_IP
        port = SOLR_PORT

        url = 'http://' + host + ':' + port + '/solr/' + coreName
        # http://10.160.29.112:8983/solr/oee_test

        solr = pysolr.Solr(url, timeout=10)
        document = [{
            "id": "1",
            "machine_id": "A01",
            "timestamp": "1569168000",
            "timestamp_iso": "2019-09-23T16:02:00.000Z",
            "activation": "88.88",
            "performance": "99.25",
            "quality": "78.69",
        },{
            "id": "2",
            "machine_id": "A03",
            "timestamp": "1569168000",
            "timestamp_iso": "2019-09-23T16:02:00.000Z",
            "activation": "78.88",
            "performance": "95.25",
            "quality": "85.69",
        },{
            "id": "3",
            "machine_id": "A05",
            "timestamp": "1569168000",
            "timestamp_iso": "2019-09-23T16:02:00.000Z",
            "activation": "95.88",
            "performance": "90.25",
            "quality": "89.69",
        },{
            "id": "4",
            "machine_id": "A06",
            "timestamp": "1569168000",
            "timestamp_iso": "2019-09-23T16:02:00.000Z",
            "activation": "89.88",
            "performance": "96.25",
            "quality": "83.69",
        }]
        solr.add(document)

    ````
    (盡可能一次 insert 多筆，因為效能比逐筆 insert 好很多)
    - 查詢資料是否insert成功 
    http://10.160.29.112:8983/solr/#/oee_test/query
