Elasticsearch across all fields with fuzzy and wildcard searching


(Palaniichuk Dmytro) #1

I use elasticsearch to match all fields in the query when typing text in the input field. I use query_string to match all fields that match a search value with a wildcard( search for any word that starts with search value ${search}*). when I want to search all field that contains cancel word and start with one letter c it works when I am typing two letter ca nothing matched, and again when I am typing three letters can it matched all cancel fields. Also How to avoid some characters and spaces (/, _, -) etc.

query

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 26,
    "max_score": 1,
    "hits": [
      {
        "_index": "call",
        "_type": "Call",
        "_id": "IP319mEBHPDr8pMVG_zn",
        "_score": 1,
        "_source": {
          "type": "type",
          "location": "location",
          "zone": "zone",
          "personName": "person_name",
          "personRoom": "person_room",
          "cancelPerson": "cancel_person",
          "cancelLocation": "cancel_location"
        }
      },
      {
        "_index": "call",
        "_type": "Call",
        "_id": "LP0V_GEBHPDr8pMVHfzb",
        "_score": 1,
        "_source": {
          "type": "Call",
          "zone": "22"
        }
      },
      {
        "_index": "call",
        "_type": "Call",
        "_id": "M_2GGWIBHPDr8pMVpPyx",
        "_score": 1,
        "_source": {
          "timestamp": {
            "seconds": "1520846631",
            "nanos": 638962100
          },
          "type": "Wandering",
          "location": "Room_222",
          "zone": "Zone_220",
          "personName": "First_Name_226 Last_Name_226 ",
          "personRoom": "Room_222",
          "cancelTimestamp": {
            "seconds": "1520846677",
            "nanos": 103338000
          },
          "responseTime": {
            "seconds": "45",
            "nanos": 464375900
          }
        }
      },
    ]
  }
}

ES search

export const get = ({index, skip = 0, pageSize = defaultPageSize, search, sort}) => new Promise(async resolve => {
        try {
            logger.silly(`getting data from elastic: index: ${index}, skip: ${skip}, pageSize: ${pageSize}`)

            let client = createClient()

            console.log(sort, 'sort')

            const params = {
                from: skip,
                size: pageSize || undefined,
                index: index.toLowerCase(),
                filter_path: 'hits.hits._source, hits.total',
            }

            if (search) {
                // params.q = `${search}*`
                params.body = {
                    query :{
                        'query_string': {
                            'query': `${search}*`,
                        },
                    },
                }
            }

            await client.search(params,
                (e, {hits: {hits:  data = [], total: totalCount} = {hits: [], total: 0}} = {}) => {

                    logger.silly(`elastic searching completed. Result: contains ${totalCount} items`)
                    console.log(data, 'data ')

                    resolve({items: data.map(t => t._source), totalCount})
                })
        } catch (e) {
            logger.error(e)
        }
    })

(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.