title: Elasticsearch: Span_near and substring match
body: <p>I'm new to elasticsearch.
  I want to implement functionality of span near that also takes care of substring match after exact phrase match and exact word sequence match.</p>
  
  <p>For example:</p>
  
  <p>documents I have on index:</p>
  
  <ol>
  <li>men's cream</li>
  <li>men's wrinkle cream</li>
  <li>men's advanced wrinkle cream</li>
  <li>women's cream</li>
  <li>women's wrinkle cream</li>
  <li>women's advanced wrinkle cream</li>
  </ol>
  
  <p>If I <strong>search for "men's cream"</strong>, I want result in the same sequence as shown above.
  <strong>Expected search result:</strong></p>
  
  <ol>
  <li>men's cream --> exact phrase match</li>
  <li>men's wrinkle cream  --> search term sequence with <code>slop 1</code></li>
  <li>men's advanced wrinkle cream --> search term sequence with <code>slop 2</code></li>
  <li>women's cream --> substring near to exact phrase match</li>
  <li>women's wrinkle cream --> substring search term sequence with <code>slop 1</code></li>
  <li>women's advanced wrinkle cream --> substring search term sequence with <code>slop 2</code></li>
  </ol>
  
  <p><strong>I can achieve first 3 results with <code>span_near</code> having nested <code>span_terms</code> with <code>slop = 2</code> and <code>in_order = true</code>.<br/> 
  I'm not able to achieve it for remaining 4 to 6 because, span_near is having nested span_terms does not support <code>wildcard</code>, in this example "<em>men's cream</em>" OR "<em>men's</em> <em>cream</em>".
  Is there any way I can achieve it using ELASTICSEARCH?</strong></p>
  
  <p><strong>UPDATES</strong><br/>
  My index:</p>
  
  <pre><code>{
    "bluray": {
      "settings": {
        "index": {
          "uuid": "4jofvNfuQdqbhfaF2ibyhQ",
          "number_of_replicas": "1",
          "number_of_shards": "5",
          "version": {
            "created": "1000199"
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>Mapping:</p>
  
  <pre><code>{
    "bluray": {
      "mappings": {
        "movies": {
          "properties": {
            "genre": {
              "type": "string"
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>I'm running following query:</p>
  
  <pre><code>POST /bluray/movies/_search
  {
    "query": {
      "bool": {
        "should": [
          {
            "span_near": {
              "clauses": [
                {
                  "span_term": {
                    "genre": "women"
                  }
                },
                {
                  "span_term": {
                    "genre": "cream"
                  }
                }
              ],
              "collect_payloads": false,
              "slop": 12,
              "in_order": true
            }
          },
          {
            "custom_boost_factor": {
              "query": {
                "match_phrase": {
                  "genre": "women cream"
                }
              },
              "boost_factor": 4.1
            }
          },
          {
            "match": {
              "genre": {
                "query": "women cream",
                "analyzer": "standard",
                "minimum_should_match": "99%"
              }
            }
          }
        ]
      }
    }
  }
  </code></pre>
  
  <p>It is giving me following result:</p>
  
  <pre><code>"took": 3,
     "timed_out": false,
     "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
     },
     "hits": {
        "total": 6,
        "max_score": 0.011612939,
        "hits": [
           {
              "_index": "bluray",
              "_type": "movies",
              "_id": "u9aNkZAoR86uAiW9SX8szQ",
              "_score": 0.011612939,
              "_source": {
                 "genre": "men's cream"
              }
           },
           {
              "_index": "bluray",
              "_type": "movies",
              "_id": "cpTyKrL6TWuJkXvliibVBQ",
              "_score": 0.009290351,
              "_source": {
                 "genre": "men's wrinkle cream"
              }
           },
           {
              "_index": "bluray",
              "_type": "movies",
              "_id": "rn_SFvD4QBO6TJQJNuOh5A",
              "_score": 0.009290351,
              "_source": {
                 "genre": "men's advanced wrinkle cream"
              }
           },
           {
              "_index": "bluray",
              "_type": "movies",
              "_id": "9a31_bRpR2WfWh_4fgsi_g",
              "_score": 0.004618556,
              "_source": {
                 "genre": "women's cream"
              }
           },
           {
              "_index": "bluray",
              "_type": "movies",
              "_id": "q-DoBBl2RsON_qwLRSoh9Q",
              "_score": 0.0036948444,
              "_source": {
                 "genre": "women's advanced wrinkle cream"
              }
           },
           {
              "_index": "bluray",
              "_type": "movies",
              "_id": "TxzCP8B_Q8epXtIcfgEw3Q",
              "_score": 0.0036948444,
              "_source": {
                 "genre": "women's wrinkle cream"
              }
           }
        ]
     }
  }
  </code></pre>
  
  <p>Which is not correct at all. Why would it search for men first when I have searched for women.</p>
  
  <p>Note: searching for "men's cream" is still returning better results but not following search term sequence.</p>