Skip to content

Instantly share code, notes, and snippets.

@dadoonet
Last active December 21, 2015 09:49
Show Gist options
  • Save dadoonet/6287562 to your computer and use it in GitHub Desktop.
Save dadoonet/6287562 to your computer and use it in GitHub Desktop.
Trying Suggest API on multifields
curl -XDELETE "http://localhost:9200/test?pretty"
curl -XPOST "http://localhost:9200/test?pretty" -d '{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis":{
"analyzer":{
"suggest":{
"type": "custom",
"tokenizer": "standard",
"filter": [ "standard", "lowercase", "suggest_shingle" ]
}
},
"filter":{
"suggest_shingle":{
"type": "shingle",
"min_shingle_size": 2,
"max_shingle_size": 5,
"output_unigrams": true
}
}
}
}
}
}'
curl -XPOST "http://localhost:9200/test/test/_mapping?pretty" -d '{
"test": {
"properties" : {
"title": {
"path": "just_name",
"type": "multi_field",
"fields":
{
"title": {
"type": "string"
},
"suggest": {
"type": "string",
"index": "analyzed",
"similarity": "BM25",
"analyzer": "suggest"
}
}
}
}
}
}'
curl -XPOST "http://localhost:9200/test/test?pretty" -d '{
"title": "Just testing the suggestions api"
}'
curl -XPOST "http://localhost:9200/test/test?pretty&refresh" -d '{
"title": "An other title"
}'
curl -XGET "http://localhost:9200/test/test/_search?pretty" -d '{
"suggest": {
"text": "tetsting sugestion",
"phrase":{
"phrase":{
"field": "suggest",
"max_errors": 5
}
}
}
}'
@clintongormley
Copy link

Your mapping was incorrect - bad indentation. Also you shouldn't use just_namein this case.

Try this:

curl -XDELETE "http://localhost:9200/test?pretty"
curl -XPUT "http://localhost:9200/test?pretty" -d '
{
   "settings": {
      "index": {
         "number_of_shards": 1,
         "number_of_replicas": 0,
         "analysis": {
            "analyzer": {
               "suggest": {
                  "type": "custom",
                  "tokenizer": "standard",
                  "filter": [
                     "standard",
                     "lowercase",
                     "suggest_shingle"
                  ]
               }
            },
            "filter": {
               "suggest_shingle": {
                  "type": "shingle",
                  "min_shingle_size": 2,
                  "max_shingle_size": 5,
                  "output_unigrams": true
               }
            }
         }
      }
   },
   "mappings": {
      "test": {
         "properties": {
            "title": {
               "type": "multi_field",
               "fields": {
                  "title": {
                     "type": "string"
                  },
                  "suggest": {
                     "type": "string",
                     "index": "analyzed",
                     "similarity": "BM25",
                     "analyzer": "suggest"
                  }
               }
            },
            "title2": {
               "type": "multi_field",
               "fields": {
                  "title2": {
                     "type": "string",
                     "index": "analyzed",
                     "similarity": "BM25",
                     "analyzer": "suggest"
                  },
                  "untouched": {
                     "type": "string"
                  }
               }
            }
         }
      }
   }
}'


curl -XPOST "http://localhost:9200/test/test?pretty" -d '{
  "title": "Just testing the suggestions api",
  "title2": "Just testing the suggestions api"
}'

curl -XPOST "http://localhost:9200/test/test?pretty&refresh" -d '{
  "title": "An other title",
  "title2": "An other title"
}'

curl -XGET "http://localhost:9200/test/test/_search?pretty" -d '{
  "size":0,
  "suggest": {
    "text": "tetsting sugestion",
    "phrase":{
      "phrase":{
        "field": "title.suggest",
        "max_errors": 5
      }
    }
  }
}'

curl -XGET "http://localhost:9200/test/test/_search?pretty" -d '{
  "size":0,
  "suggest": {
    "text": "tetsting sugestion",
    "phrase":{
      "phrase":{
        "field": "suggest",
        "max_errors": 5
      }
    }
  }
}'

curl -XGET "http://localhost:9200/test/test/_search?pretty" -d '{
  "size":0,
  "suggest": {
    "text": "tetsting sugestion",
    "phrase":{
      "phrase":{
        "field": "title2",
        "max_errors": 5
      }
    }
  }
}'

@clintongormley
Copy link

Your mapping was incorrect - bad indentation. Also you shouldn't use just_namein this case.

Try this:

curl -XDELETE "http://localhost:9200/test?pretty"
curl -XPUT "http://localhost:9200/test?pretty" -d '
{
   "settings": {
      "index": {
         "number_of_shards": 1,
         "number_of_replicas": 0,
         "analysis": {
            "analyzer": {
               "suggest": {
                  "type": "custom",
                  "tokenizer": "standard",
                  "filter": [
                     "standard",
                     "lowercase",
                     "suggest_shingle"
                  ]
               }
            },
            "filter": {
               "suggest_shingle": {
                  "type": "shingle",
                  "min_shingle_size": 2,
                  "max_shingle_size": 5,
                  "output_unigrams": true
               }
            }
         }
      }
   },
   "mappings": {
      "test": {
         "properties": {
            "title": {
               "type": "multi_field",
               "fields": {
                  "title": {
                     "type": "string"
                  },
                  "suggest": {
                     "type": "string",
                     "index": "analyzed",
                     "similarity": "BM25",
                     "analyzer": "suggest"
                  }
               }
            },
            "title2": {
               "type": "multi_field",
               "fields": {
                  "title2": {
                     "type": "string",
                     "index": "analyzed",
                     "similarity": "BM25",
                     "analyzer": "suggest"
                  },
                  "untouched": {
                     "type": "string"
                  }
               }
            }
         }
      }
   }
}'


curl -XPOST "http://localhost:9200/test/test?pretty" -d '{
  "title": "Just testing the suggestions api",
  "title2": "Just testing the suggestions api"
}'

curl -XPOST "http://localhost:9200/test/test?pretty&refresh" -d '{
  "title": "An other title",
  "title2": "An other title"
}'

curl -XGET "http://localhost:9200/test/test/_search?pretty" -d '{
  "size":0,
  "suggest": {
    "text": "tetsting sugestion",
    "phrase":{
      "phrase":{
        "field": "title.suggest",
        "max_errors": 5
      }
    }
  }
}'

curl -XGET "http://localhost:9200/test/test/_search?pretty" -d '{
  "size":0,
  "suggest": {
    "text": "tetsting sugestion",
    "phrase":{
      "phrase":{
        "field": "suggest",
        "max_errors": 5
      }
    }
  }
}'

curl -XGET "http://localhost:9200/test/test/_search?pretty" -d '{
  "size":0,
  "suggest": {
    "text": "tetsting sugestion",
    "phrase":{
      "phrase":{
        "field": "title2",
        "max_errors": 5
      }
    }
  }
}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment